-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (127 loc) · 3.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
const express = require('express');
const mongoose = require('mongoose');
const { ObjectId } = require('mongodb');
const PORT = 3000;
const DB_URL = "mongodb://...";
const Schema = mongoose.Schema;
const app = express();
// const jsonParser = express.json();
app.use(express.static('views'));
const TaskSchema = new Schema(
{
name: {
type: String,
required: true,
maxlength: 32
},
description: {
type: String,
maxlength: 128
},
dateStart: {
type: Date,
default: Date.now()
},
dateEnd: {
type: Date,
default: Date.now()
}
},
{ versionKey: false }
);
app.use(express.json());
const Task = mongoose.model('Task', TaskSchema);
const main = async function () {
try {
await mongoose.connect(DB_URL);
app.listen(PORT);
}
catch (err) {
return console.log(err);
}
}
main();
// SELECT *
app.get('/api/tasks', async function (request, response) {
try {
const tasks = await Task.find({});
tasks.forEach(element => {
element.id = element._id.toString();
});
response.send(tasks);
}
catch (err) {
response.sendStatus(500);
}
});
// SELECT by "name"
app.get('/api/tasks/:name', async function (request, response) {
try {
const task = await Task.findOne({ name: request.params.name });
if (task)
response.send(task);
else
response.send(new Task({ name: request.params.name, description: "Was not found!", }));
}
catch (err) {
response.sendStatus(500);
}
});
// INSERT
app.post('/api/tasks', async function (request, response) {
if (!request.body)
return response.sendStatus(400);
try {
const task = new Task({
name: request.body.name,
description: request.body.description,
dateStart: request.body.dateStart,
dateEnd: request.body.dateEnd
});
task.save();
response.status(201);
response.send(task);
}
catch (err) {
response.sendStatus(500);
}
});
// UPDATE
app.put('/api/tasks/:id', async function (request, response) {
if (!request.body)
return response.sendStatus(400);
try {
const newTask = {
name: request.body.name,
description: request.body.description,
dateStart: request.body.dateStart,
dateEnd: request.body.dateEnd
};
const task = await Task.findOneAndUpdate({ _id: new ObjectId(request.params.id) }, newTask, { new: true });
if (task)
response.send(task);
else
response.sendStatus(404);
}
catch (err) {
response.sendStatus(500);
}
});
// DELETE
app.delete('/api/tasks/:id', async function (request, response) {
try {
const id = new ObjectId(request.params.id);
const task = await Task.findByIdAndDelete({ _id: id });
if (task)
response.send(task);
else
response.sendStatus(404);
}
catch (err) {
response.sendStatus(500);
}
});
process.on('SIGINT', async function () {
await mongoose.disconnect();
process.exit();
})