-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (175 loc) · 5.26 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import bodyParser from 'body-parser';
import express from 'express';
import db from './db/db';
import mondodb from 'mongodb';
// Set up the express app
const assert = require('assert');
const app = express();
const ttlLimit = 20;
const mongoData = mondodb.MongoClient;
const mongoUrl = "mongodb://fcloudtest1:Test123@ds035693.mlab.com:35693/fcloudtest";
let tempDb;
let currDBLen;
let collectionDB;
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h * 60 * 60 * 1000));
return this;
}
function fnGetRandomString(len, callBack) {
let randomString = Math.random().toString(36).substring(len);
isExists(randomString,function(resp){
if(!resp){
randomString = Math.random().toString(36).substring(len);
}
callBack(randomString, resp);
});
}
function isExists(randomString,callBack) {
collectionDB.find({'randomString': randomString}).toArray(function(err, res) {
if(res.length < 1){
callBack(false);
}else{
callBack(true);
}
});
}
//connecting to the database
const client = new mongoData(mongoUrl, {
useNewUrlParser: true
});
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");
tempDb = client.db('fcloudtest');
collectionDB = tempDb.collection('fclouddata');
//client.close();
});
// Parse incoming requests data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
// get all todos
app.get('/api/v1/todos/all', (req, res) => {
collectionDB.find().toArray(function(err, results) {
// send HTML file populated with quotes here
res.status(200).send({
success: 'true',
message: 'todos retrieved successfully',
todos: results
})
});
});
app.get('/api/v1/todos/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
collectionDB.find({
'id': id
}).toArray(function(err, results) {
if (results.length < 1) {
return res.status(404).send({
success: 'false',
message: 'todo does not exist',
});
} else {
updateDocument({
'id': id
}, {
createdAt: new Date(),
ttl: new Date().addHours(3)
}, collectionDB, function(results) {
res.status(200).send({
success: 'true',
message: 'todo retrieved successfully',
todos: results
})
});
}
});
});
//update the data
app.post('/api/v1/todos/post/', (req, res) => {
if (!req.body.title) {
return res.status(400).send({
success: 'false',
message: 'title is required'
});
} else if (!req.body.description) {
return res.status(400).send({
success: 'false',
message: 'description is required'
});
}
collectionDB.countDocuments(function(reject, dbCount) {
console.log('comes here' , dbCount);
if (dbCount + 1 > ttlLimit) {
//do something here
} else {
fnGetRandomString(5, function(resp, boll){
const todo = {
id: dbCount + 1,
createdAt: new Date(),
ttl: new Date().addHours(3),
title: req.body.title,
description: resp,
}
collectionDB.insertOne(todo, (err, result) => {
if (err) return console.log(err);
else {
console.log('saved to database');
return res.status(201).send({
success: 'true',
message: 'todo added successfully',
todo
})
}
});
});
}
});
});
const updateDocument = function(currData, newData, collection, callback) {
// Update document where a is 2, set b equal to 1
collection.updateOne(currData, {
$set: newData
}, function(err, result) {
console.log("Updated the document from:", currData, " \nto:", newData);
callback(result);
});
}
//delete the data
app.post('/api/v1/todos/del/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
collectionDB.deleteOne({"id": id}, function(err, res) {
if(res.result.n > 0){
return {
success: 'true',
message: 'Todo ' + id + ' deleted successfuly',
};
}else{
return {
success: 'false',
message: 'todo does not exist',
};
}
});
});
app.post('/api/v1/todos/del/all', (req, res) => {
const id = parseInt(req.params.id, 10);
db.map((todo) => {
if (todo.id === id) {
db.splice(index, 1);
return res.status(200).send({
success: 'true',
message: 'Todo ' + id + ' deleted successfuly',
});
}
});
return res.status(404).send({
success: 'false2',
message: 'todo does not exist',
});
});
const PORT = 5123;
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`)
});