-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcontacts.js
More file actions
95 lines (91 loc) · 3.21 KB
/
Copy pathcontacts.js
File metadata and controls
95 lines (91 loc) · 3.21 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
const express = require('express');
const router = express.Router();
let db = require('../db_contacts.json');
router.route('/contacts')
.all( (req, res, next)=>{
console.log('all mw');
next();
})
.get((req, res)=>{
let viewDb = JSON.parse( JSON.stringify(db) );
let queryParams = req.query;
if( 'search' in queryParams ){
viewDb = viewDb.filter((contact)=>{
return contact.first_name.toLowerCase().includes(queryParams.search.toLowerCase()) ||
contact.last_name.toLowerCase().includes(queryParams.search.toLowerCase());
});
}
if( 'limit' in queryParams && 'page' in queryParams){
let limit = parseInt(queryParams.limit), page = parseInt(queryParams.page);
if( !isNaN( limit ) && !isNaN( page ) ){
const indexStart = limit * (page - 1);
viewDb = viewDb.splice(indexStart, limit);
}
}
res.json(viewDb);
})
.post((req, res)=>{
let newContact = req.body;
let foundedContact = db.find((contact) => contact.id === newContact.id );
if( foundedContact ){
res.status(500);
res.send(`id ${newContact.id} ya utilizado `);
}else{
db.push(newContact);
res.sendStatus(201);
}
})
.put((req, res)=>{
let replaceContact = req.body;
let foundedContact = db.find((contact) => contact.id === replaceContact.id );
if( !foundedContact ){
res.status(500);
res.send(`id ${replaceContact.id} no existe para reemplazar contacto`);
}else{
db = db.map( contact => {
if(contact.id === replaceContact.id) return replaceContact;
else return contact;
});
res.sendStatus(200);
}
})
.patch((req, res)=>{
let replaceContact = req.body;
let foundedContact = db.find((contact) => contact.id === replaceContact.id );
if( !foundedContact ){
res.status(500);
res.send(`id ${replaceContact.id} no existe para reemplazar atributos`);
}else{
db = db.map( contact => {
if(contact.id === replaceContact.id){
for(let key in replaceContact){
contact[key] = replaceContact[key];
}
return contact;
}else return contact;
});
res.sendStatus(200);
}
});
router.get('/contacts/:id', (req, res)=>{
res.json(db.find((user)=> +req.params.id === user.id ));
});
router.delete('/contacts/:id', (req, res)=>{
const deleteId = parseInt(req.params.id);
if( isNaN(deleteId) ){
res.status(500);
res.send('el id debe ser integer');
}else{
let foundedContact = db.find((contact) => contact.id === deleteId );
if( !foundedContact ){
res.status(404);
res.send(`id ${deleteId} no existe para eliminar`);
}else{
db = db.filter( contact => {
return contact.id !== deleteId;
});
res.sendStatus(200);
}
}
});
module.exports = router;