-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmongoDao.js
More file actions
117 lines (106 loc) · 3.61 KB
/
mongoDao.js
File metadata and controls
117 lines (106 loc) · 3.61 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
var express = require('express');
var router = express.Router();
var mongo = require('mongodb');
var mongoClient = mongo.MongoClient;
// REST API V2 calls go here.
router.get('/api/v2/entries.json', function(req, res) {
mongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
if (err) {
throw err;
}
db.collection('entries').find({},{subject:1}).toArray(function(err, result) {
if (err) {
throw err;
}
res.status(200).json(result);
db.close();
});
});
});
// Create
router.post('/api/v2/entries.json', function(req, res){
// Store new entry and return id.
console.log(req.body);
// {"subject":"Something else","content":"This is the contents for 'Something else'"}
var newObj = {};
newObj.subject = req.body.subject;
newObj.content = req.body.content;
mongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
if (err) {
throw err;
}
db.collection('entries').insert(newObj, function(err, result) {
if (err) {
throw err;
}
res.status(201).json(result.ops[0]._id);
db.close();
});
});
});
// Read
router.get('/api/v2/entries/:id.json', function(req, res){
var id = new mongo.ObjectId(req.params.id);
mongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
if (err) {
throw err;
}
console.log(`Checking mongodb for _id:${id}`);
//find({_id:ObjectId("56fab77f6ab3ead947e97973")})
db.collection('entries').find({_id:id}).toArray(function(err, result) {
if (err) {
console.log(`Reading _id ${id} failed: ${err}`)
throw err;
}
console.log(`Reading _id succeeded with result: ${result[0]}`)
res.status(201).json(result[0]);
db.close();
});
});
});
// Update
router.put('/api/v2/entries/:id.json', function(req, res){
var object = {};
var id = new mongo.ObjectId(req.params.id);
var subject = req.body.subject;
var content = req.body.content;
object._id = id;
object.subject = subject;
object.content = content;
mongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
if (err) {
throw err;
}
db.collection('entries').update({_id:id}, object, function(err, result) {
if (err) {
console.log(`Updating _id ${id} failed: ${err}`)
throw err;
}
console.log(`Updating _id succeeded with result: ${result}`)
res.sendStatus(204);
db.close();
});
});
});
// Delete
router.delete('/api/v2/entries/:id', function(req, res){
var id = new mongo.ObjectId(req.params.id);
mongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
if (err) {
throw err;
}
console.log(`Checking mongodb for _id:${id}`);
//find({_id:ObjectId("56fab77f6ab3ead947e97973")})
db.collection('entries').remove({_id:id}, function(err, result) {
if (err) {
console.log(`Deleting _id ${id} failed: ${err}`)
throw err;
}
console.log(`Deleting _id succeeded with result: ${result}`)
res.sendStatus(204);
db.close();
});
});
});
module.exports = router;
// END REAST API V2 CALLS.