-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
135 lines (122 loc) · 3.55 KB
/
Copy pathapp.js
File metadata and controls
135 lines (122 loc) · 3.55 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const routes = require('./app/routes');
const Store = require('./app/store');
const {counter,store} = Store;
/**
* API for Creating Plans
* - [X] Create Plan
* - [X] Get Plan
* - [X] Add Task to Plan
* - [X] Update Task from Plan
* - [X] Get Task
* - [X] Remove Task from Plan
*/
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const port = 3000;
app.get('/', (request, response) => {
response.send('NodeJS Keynote');
});
routes(app);
// ==============================================
// PLAN
// ==============================================
// GET PLAN
app.get('/plans/:planId', (request, response) => {
const planId = parseInt(request.params.planId);
const plan = store.find((plan) => plan.id === planId);
if (plan) {
response.json(plan);
} else {
response.status(404)
.send('Sorry, can\'t find that.');
}
});
// ==============================================
// TASK
// ==============================================
// CREATE TASK
app.post('/plans/:planId/tasks', (request, response) => {
const planId = parseInt(request.params.planId);
const plan = store.find((plan) => plan.id === planId);
if (!plan) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
const task = {
id: counter.tasks,
name: request.body.name,
estimate: request.body.estimate
};
plan.tasks.push(task);
counter.tasks += 1;
store[store.indexOf((plan) => plan.id === planId)] = plan;
response.json(task);
});
// UPDATE TASK
app.patch('/plans/:planId/tasks/:taskId', (request, response) => {
const planId = parseInt(request.params.planId);
const plan = store.find((plan) => plan.id === planId);
const planIndex = store.indexOf(plan);
if (!plan) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
const taskId = parseInt(request.params.taskId);
let task = plan.tasks.find((task) => task.id === taskId);
const taskIndex = plan.tasks.indexOf(task);
if (!task) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
task = {...task, ...request.body};
plan.tasks[taskIndex] = task;
store[planIndex] = plan;
response.json(task);
});
// GET TASK
app.get('/plans/:planId/tasks/:taskId', (request, response) => {
const planId = parseInt(request.params.planId);
const plan = store.find((plan) => plan.id === planId);
if (!plan) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
const taskId = parseInt(request.params.taskId);
const task = plan.tasks.find((task) => task.id === taskId);
if (!task) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
response.json(task);
});
// DELETE TASK
app.delete('/plans/:planId/tasks/:taskId', (request, response) => {
const planId = parseInt(request.params.planId);
const plan = store.find((plan) => plan.id === planId);
const planIndex = store.indexOf(plan);
if (!plan) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
const taskId = parseInt(request.params.taskId);
const task = plan.tasks.find((task) => task.id === taskId);
const taskIndex = plan.tasks.indexOf(task);
if (!task) {
response.status(404)
.send('Sorry, can\'t find that.');
return;
}
plan.tasks.splice(taskIndex, 1);
store[planIndex] = plan;
response.status(204)
.send('Task deleted!');
});
app.listen(port, () => console.log(`NodeJS Keynote API is running on port ${port}`));