-
Notifications
You must be signed in to change notification settings - Fork 0
/
contoller.js
63 lines (53 loc) · 1.47 KB
/
contoller.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
var fs=require('fs');
//callback functions
exports.getAll=function(req, res){
var path="./products.json";
fs.readFile(path, (err, data)=>{
if(data){
var products=JSON.parse(data);
res.send(products);
}
else{
res.send("data not available")
}
})
};
exports.getById=function(req, res){
var producdId=req.params.id;
var path="./products.json";
fs.readFile(path, (err, data)=>{
if(data){
var products=JSON.parse(data);
var product=products.find((p=>(p.id ==producdId)))
res.send(product);
}
else{
res.send("data not available")
}
})
};
exports.insert=function(req, res){
fs.readFile(path, (err, data)=>{
if(data){
var products=JSON.parse(data);
var newProduct=JSON.parse(req.body);
products.push(newProduct);
fs.writeFile(path,products,(err)=>{
if(err){
res.send("file IO problem")
}
else
res.send("new product inserted into collection.")
} )
}
else{
res.send("data not available")
}
})
};
exports.update=function(req, res){
res.send("existing product updated");
};
exports.delete=function(req, res){
res.send("product is removed from list");
};