forked from manar007/frontend-tech-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
154 lines (136 loc) · 3.56 KB
/
server.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
'use strict';
const app = require('express')();
const bodyParser = require('body-parser');
const tasksContainer = require('./tasks.json');
app.use(bodyParser.json());
/**
* GET /tasks
*
* Return the list of tasks with status code 200.
*/
app.get('/tasks', (req, res) => {
return res.status(200).json(tasksContainer);
});
/**
* Get /task/:id
*
* id: Number
*
* Return the task for the given id.
*
* If found return status code 200 and the resource.
* If not found return status code 404.
* If id is not valid number return status code 400.
*/
app.get('/task/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
if (!Number.isNaN(id)) {
const task = tasks.Container.find((item) => item.id === id);
if (task !== null) {
return res.status(200).json({
task,
});
} else {
return res.status(404).json({
message: 'Not found.',
});
}
} else {
return res.status(400).json({
message: 'Bad request.',
});
}
});
/**
* PUT /task/update/:id/:title/:description
*
* id: Number
* title: string
* description: string
*
* Update the task with the given id.
* If the task is found and update as well, return a status code 204.
* If the task is not found, return a status code 404.
* If the provided id is not a valid number return a status code 400.
*/
app.put('/task/update/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
if (!Number.isNaN(id)) {
const task = tasksContainer.tasks.find(item => item.id === id);
if (task !== null) {
task.title = req.body.title;
task.description = req.body.description;
task.isDone = req.body.isDone;
return res.status(204).json({
message: 'Updated successfully',
});
;
} else {
return res.status(404).json({
message: 'Not found',
});
}
} else {
return res.status(400).json({
message: 'Bad request',
});
}
});
/**
* POST /task/create/:title/:description
*
* title: string
* description: string
*
* Add a new task to the array tasksContainer.tasks with the given title and description.
* Return status code 201.
*/
app.post('/task/create', (req, res) => {
// Find last item and increase id. Delete method splice issue will be fixed with this.
const lastItem = tasksContainer.tasks[tasksContainer.tasks.length - 1];
const nextId = lastItem ? lastItem.id + 1 : 0;
const task = {
id: nextId,
title: req.body.title,
description: req.body.description,
isDone: false,
};
tasksContainer.tasks.push(task);
return res.status(201).json({
message: 'Resource created',
});
});
/**
* DELETE /task/delete/:id
*
* id: Number
*
* Delete the task linked to the given id.
* If the task is found and deleted as well, return a status code 204.
* If the task is not found, return a status code 404.
* If the provided id is not a valid number return a status code 400.
*/
app.delete('/task/delete/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
if (!Number.isNaN(id)) {
const task = tasksContainer.tasks.find(item => item.id === id);
if (task !== null) {
const taskIndex = tasksContainer.tasks;
tasksContainer.tasks.splice(taskIndex, 1);
return res.status(200).json({
message: 'Updated successfully',
});
} else {
return es.status(404).json({
message: 'Not found',
});
}
} else {
return res.status(400).json({
message: 'Bad request',
});
}
});
app.listen(9001, () => {
process.stdout.write('the server is available on http://localhost:9001/\n');
});