generated from yaakov-hatam/cellphones-restful-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdal.js
More file actions
70 lines (62 loc) · 1.41 KB
/
dal.js
File metadata and controls
70 lines (62 loc) · 1.41 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
const fs = require('fs');
const filePath = "phones.json";
function readAll(cb){
fs.readFile(filePath, function(e, data){
if(e){
cb(e);
}
else{
cb(null, data.toString());
}
})
}
function readOne(id, cb){
}
function add(obj, cb){
fs.readFile(filePath, function(e, data){
if(e){
cb(e);
}
else{
phones = JSON.parse(data.toString());
phones.push(obj);
fs.writeFile(filePath, JSON.stringify(phones), function(e){
if(e){
cb(e);
}
else{
cb(null);
}
})
}
})
}
function deleteOne(id, cb){
fs.readFile(filePath, function(e, data){
if(e){
cb(e);
}
else{
let phones = JSON.parse(data.toString());
let idx = phones.findIndex(i => i.id == id);
phones.splice(idx, 1);
fs.writeFile(filePath, JSON.stringify(phones), function(e){
if(e){
cb(e);
}
else{
cb(null,data);
}
})
}
})
}
function updateOne(id, cb){
}
module.exports = {
readAll: readAll,
readOne: readOne,
add: add,
deleteOne: deleteOne,
updateOne: updateOne
}