-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
104 lines (93 loc) · 3.55 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const autoIncrement = require('mongoose-auto-increment');
const PORT = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
//-------------------------------------Connect to Database
mongoose.connect('mongodb://localhost:27017/db', { useNewUrlParser: true });
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'Connection error:'));
db.once('open', () => {
console.log(`Database has been connected successfully`);
});
//-------------------------------------Schema
autoIncrement.initialize(db);
Schema = mongoose.Schema;
userSchema = new Schema({
_id: Schema.Types.ObjectId,
name: String,
email: String
});
mongoose.model('User', userSchema);
userSchema.plugin(autoIncrement.plugin, {
model: 'User', field: '_id',
startAt: 0,
incrementBy: 1
})
//-------------------------------------CRUD
let User = mongoose.model('user', userSchema);
app.get('/find', (req, res) => {
User.find()
.then(users => {
res.status(200).json(users);
}).catch(err => {
res.status(500).json({ message: err.message || "Some error occurred while retrieving users." });
});
});
app.get('/find/:userId', (req, res) => {
User.findById(req.params.userId)
.then(users => {
res.status(200).json(users);
}).catch(err => {
res.status(500).json({ message: err.message || "Some error occurred while retrieving users." });
});
});
app.post('/create', (req, res) => {
User.nextCount((err, count) => {
let user = new User({ name: req.body.name, email: req.body.email });
user.save()
.then(data => {
res.status(201).json(data);
}).catch(err => {
res.status(500).json({ message: err.message || "Some error occurred while creating the User." });
});
});
});
app.put('/update/:userId', (req, res) => {
if (!req.body) {
return res.status(400).json({ message: "User content can not be empty" });
}
User.findByIdAndUpdate(req.params.userId, req.body)
.then(user => {
if (!user) {
return res.status(404).json({ message: "User not found with id " + req.params.userId });
}
res.status(200).json({ message: "User updated successfully!" });
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).json({ message: "User not found with id " + req.params.userId });
}
return res.status(500).json({ message: "Error updating user with id " + req.params.userId + err });
});
});
app.delete('/delete/:userId', (req, res) => {
User.findByIdAndRemove(req.params.userId)
.then(user => {
if (!user) {
return res.status(404).json({ message: "User not found with id " + req.params.userId });
}
res.status(200).json({ message: "User deleted successfully!" });
}).catch(err => {
if (err.kind === 'ObjectId' || err.name === 'NotFound') {
return res.status(404).json({ message: "User not found with id " + req.params.userId });
}
return res.status(500).json({ message: "Could not delete user with id " + req.params.userId });
});
});
//-------------------------------------
app.listen(PORT, () => {
console.log(`Server is up and running on port: ${PORT}`);
});