-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js.js
99 lines (90 loc) · 2.46 KB
/
index.js.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
const express = require('express');
const app = express();
const port = 3000;
const bodyParser = require('body-parser');
// middleware
app.use(bodyParser.json());
var data = [];
var id = 0;
// return all todos
function getTodo(req, res){
let sizeData = data.length;
if(sizeData == 0){
res.status(200).send("data is empty!!")
}
else{
res.send(data);
}
}
function addTodo(req, res){
let temp_data = req.body;
// loop to check any data format is ok or not
let check = 0;
let size = Object.keys(temp_data).length;
var ideal_keys = ['title', 'completed', 'description'];
// deals when data entered is {}
if(size === 0){
res.status(200).send("entered data is empty");
}
for(let word of ideal_keys){
if(word in temp_data){
check = 1;
break;
}
}
// deals if none keyword is entered
if (check === 0){
res.send(`enter data in following format:-
{
"title":_____,
"completed":______,
"description":________,
}
`)
}
else{
// give unique id
temp_data["id"] = id++;
data.push(temp_data);
res.send("data is added");
}
// Todo -> give them unique id
}
// getting single todo
function getSingleTodo(req, res){
let id_temp = req.params.n;
let ans = data[id_temp];
res.send(ans);
}
// setting update
function updateTodo(req , res){
let new_obj = req.body;
let id_temp = parseInt(req.params.n);
// adding id to new updated obj
new_obj["id"] = id_temp;
data[id_temp] = new_obj;
res.send("updated successfully");
}
// delete todo
function deleteTodo(req, res){
let temp_id = req.params.n;
let deleted = delete data[temp_id];
// to replacing the deleted index
for(let i = temp_id ; i <data.length-1; i++){
data[i] = data[i+1]
data[i]["id"] = i;
}
// delete last element
let lst_dlt = delete data[data.length-1];
res.send("element deleted successfully");
}
// setting up handlers
app.get('/todos/', getTodo);
app.get('/todos/:n', getSingleTodo);
app.post('/todos', addTodo);
app.put('/todos/:n', updateTodo)
app.delete('/todos/:n', deleteTodo);
function started(){
console.log(`localhost:3000/todos has started ${port}`)
}
app.listen(port, started);