-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
197 lines (176 loc) · 4.66 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
195
196
197
const path = require('path');
const mongoose = require('mongoose');
const express = require('express');
const cors = require('cors');
const app = express();
const morgan = require('morgan');
const { urlencoded, json } = require('body-parser');
const PORT = process.env.PORT || 3000;
/**
* todo creates a new Mongoose Schema that defines the
*/
const todo = new mongoose.Schema({
id: {
type: Number,
required: true
},
item: {
type: String,
required: true
},
completed: {
type: Boolean,
required: true
}
});
const Todo = mongoose.model('todo', todo);
app.use(morgan('dev'));
app.use(urlencoded({extended: true}));
app.use(json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors())
app.get('/', (req, res) => {
res.render('index', { title: 'Express' });
});
/**
* This route will get all of the records found in the database
*/
app.get('/todos', async (req, res) => {
try {
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
app.post('/todo', async (req, res) => {
const todoCreated = req.body;
try{
const todo = await Todo.create(todoCreated);
console.log(`Created todo: ${todo}`);
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
/**
* Update one record
*/
app.put('/todo/:id/:completed', async (req, res) => {
const updateId = req.params.id;
const completed = req.params.completed;
try{
const findTodo = await Todo.updateOne(
{ _id: updateId },
{ completed: completed},
{ new: true }
);
console.log(`Updated todo. New todo: ${findTodo}`);
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
/**
* Update multiple records as completed or not
*/
app.put('/todos/selected/:completed', async (req, res) => {
const completed = req.params.completed;
const updateTodos = req.body;
try{
for(let i = 0; i < updateTodos.length; i++) {
updateTodos[i].completed = completed;
const findTodo = await Todo.updateOne(
{ _id: updateTodos[i]._id},
{ completed: completed },
{ new: true }
);
console.log(`Updated todo. New todo: ${findTodo}`);
}
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
/**
* Update all records as completed or not
*/
app.put('/todos/all/:completed', async (req, res) => {
const completed = req.params.completed;
try{
const findTodo = await Todo.updateMany(
{},
{ completed: completed }
);
console.log(`Updated todo. New todo: ${findTodo}`);
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
/**
* Delete one record
*/
app.delete('/todo/:id', async (req, res) => {
const deleteId = req.params.id;
console.log(`Received id: ${deleteId}`);
try {
const deletedId = await Todo.findOneAndRemove({
_id: deleteId
});
console.log(`Successfully deleted record with id: ${deleteId}`);
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
/**
* Delete multiple records
*/
app.delete('/todos/selected', async (req, res) => {
const updateTodos = req.body;
try {
for(let i = 0; i < updateTodos.length; i++) {
const todoId = updateTodos[i]._id;
const deletedId = await Todo.findOneAndRemove(todoId);
console.log(`Successfully deleted record with id: ${todoId}`);
}
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
/**
* Delete all records
*/
app.delete('/todos/all', async (req, res) => {
try {
const deletedId = await Todo.deleteMany({});
console.log(`Successfully deleted all records`);
res.status(200).json(await Todo.find({}).lean().exec());
} catch (e) {
console.log(e);
res.status(500).send();
}
});
const connect = () => {
return mongoose.connect('mongodb://localhost:27017/todos');
};
connect()
.then(async connection => {
app.listen(PORT, (err) => {
if (err) {
throw new Error();
} else {
console.log('App listening on port: ' + PORT);
}
});
})
.catch(e => console.error(e));
module.exports = app;