-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
152 lines (138 loc) · 5 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//const express = require('express')
const express = require('serverless-express/express')
const app = express()
const bodyParser = require('body-parser')
//const port = 3000
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
// Setup Mongo
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert')
const url = process.env.MONGO_URL
const dbName = 'todolist'
const client = new MongoClient(url)
var ObjectID = require('mongodb').ObjectID;
// Get all records
app.get(['/documents/all', '/api/app/documents/all'], (req, res, next) => {
client.connect(function (err) {
console.log("Responding to a GET /documents/all")
assert.equal(null, err)
const db = client.db(dbName)
const collection = db.collection('items');
collection.find({}).toArray((err, result) => {
if (err) {
res.status(400).send({ 'error': err })
}
if (result === undefined || result.length === 0) {
res.status(400).send({ 'error': 'No documents in database' })
} else {
res.status(200).send(result)
console.log(result)
}
})
})
})
// Get specific record
app.get(['/documents/id', '/api/app/documents/id'], (req, res, next) => {
client.connect(function (err) {
console.log("Responding to a GET /documents/id")
console.log(req.query)
assert.equal(null, err)
const db = client.db(dbName)
const collection = db.collection('items');
if (req.query.id != undefined){
var o_id = new ObjectID(req.query.id)
collection.findOne({_id: o_id}, (err, result) => {
if (err) {
res.status(400).send({ 'error': err })
}
if (result === undefined) {
res.status(400).send({ 'error': 'No document matching that id was found' })
} else {
res.status(200).send(result)
console.log(result)
}
})
}
else {
res.status(400).send({ 'error': 'No id parameter specified' })
}
})
})
//Insert a document
app.post(['/documents/new', '/api/app/documents/new'], (req, res, next) => {
console.log("Responding to a POST /documents/new")
console.log(req.body)
client.connect(function (err) {
assert.equal(null, err)
const db = client.db(dbName)
const collection = db.collection('items');
collection.insertOne({
"created":Date.now(), "updated":Date.now(), "description":req.body.description, "duedate":req.body.duedate
}, (err, result) => {
if (err) {
res.status(400).send({ 'error': err })
}
res.status(200).send(result)
console.log(result)
})
})
})
//Delete a document
app.delete(['/documents/id', '/api/app/documents/id'], (req, res, next) => {
client.connect(function (err) {
console.log("Responding to a DELETE /documents/id")
console.log(req.query)
assert.equal(null, err)
const db = client.db(dbName)
const collection = db.collection('items');
if (req.query.id != undefined){
var o_id = new ObjectID(req.query.id)
collection.deleteOne({_id: o_id}, (err, result) => {
if (err) {
res.status(400).send({ 'error': err })
}
res.status(200).send(result)
console.log(result)
})
}
else {
res.status(400).send({ 'error': 'No id parameter specified' })
}
})
})
//Update a document
app.patch(['/documents/id', '/api/app/documents/id'], (req, res, next) => {
client.connect(function (err) {
console.log("Responding to a PATCH /documents/id")
console.log(req.query)
assert.equal(null, err)
const db = client.db(dbName)
const collection = db.collection('items');
if (req.query.id != undefined){
var o_id = new ObjectID(req.query.id)
collection.updateOne({_id: o_id},
{$set:
{
updated: Date.now(),
description: req.query.description,
duedate: req.query.duedate
}
},
(err, result) => {
if (err) {
res.status(400).send({ 'error': err })
}
res.status(200).send(result)
console.log(result)
})
}
else {
res.status(400).send({ 'error': 'No id parameter specified' })
}
})
})
// Azure's default URI path for this is /api/app and it sends that through so we need to cater for that
app.get(['/', '/api/app'], (req, res) => res.send('todolist v1.0.0'))
//app.listen(port, () => console.log(`Example app listening on port ${port}!`))
module.exports = app