-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrudAss.js
More file actions
135 lines (120 loc) · 4.53 KB
/
Copy pathcrudAss.js
File metadata and controls
135 lines (120 loc) · 4.53 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
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
const goodsDb = require("./db/dbGoods.json");
const http = require("http");
const fs = require("fs");
const PORT = 4040;
// Create the goods into the database
const server = http.createServer((req, res) => {
const {url,method} = req;
if (url === "/create-goods" && method === "POST") {
let body = "";
req.on("data", (chunks) => {
console.log("i am chunks:", chunks)
body += chunks
console.log("i am raw body:", body)
});
req.on("end", () => {
const data = JSON.parse(body);
const uuid = require("uuid").v4();
const goods = {
id: uuid,
name: data.name,
inStock: data.inStock,
unit: data.unit,
unitPrice: data.unitPrice,
totalPrice: data.unit * data.unitPrice,
};
goodsDb.push(goods);
console.log(goodsDb);
fs.writeFile("./db/dbGoods.json", JSON.stringify(goodsDb, null, 2), "utf8", (error, data) => {
if (error) {
res.writeHead(400, {"content-type": "text/plain"});
res.end("Bad request")
} else {
res.writeHead(201, {"content-type": "application/json"})
res.end(JSON.stringify({
message: "goods added Successfully",
data: goods
}))
}
})
});
}
// Read the entered goods in the database
else if(url.startsWith("/goods") && method === "GET") {
if (goodsDb.length < 1) {
res.writeHead(404, {"content-type": "text/plain"});
res.end("No goods found")
} else {
res.writeHead(200, {"content-type": "application/json"})
res.end(JSON.stringify({
message: "All goods below",
total: goodsDb.length,
data: goodsDb
}))
}
}
// To find a particular good using the Unique ID Code
else if (url.startsWith("/getgoods") && method === "GET"){
const id = url.split("/")[2];
const goods = goodsDb.find((e) => e.id === id);
if (!goods) {
res.writeHead(404, {"content-type": "text/plain"})
res.end("Goods not found");
} else {
res.writeHead(200, {"content-type": "application/json"})
res.end(JSON.stringify({
message: "Goods below",
data: goods
}))
}
}
// Update the details of the goods in the database with it's unique ID
else if (url.startsWith("/update-goods") && method === "PATCH"){
let body = "";
req.on("data", (chunks) => {
body += chunks;
});
req.on("end", () => {
const update = JSON.parse(body);
console.log(update);
const id = url.split("/")[2];
const goods = goodsDb.find((f) => f.id === id);
Object.assign(goods, update);
const index = goodsDb.findIndex((e) => e.id === goods.id);
if (index !== -1) {
goodsDb[index] = goods
};
fs.writeFile("./db/dbGoods.json", JSON.stringify(goodsDb, null, 2), "utf8", (error, data) => {
if (error) {
res.writeHead(500, {"content-type": "text/plain"})
res.end("Error updating goods");
} else {
res.writeHead(200, {"content-type": "application/json"});
res.end(JSON.stringify({
message: "Goods updated successfully",
data: goods
}))
}
})
})
}
// To delete goods from database
else if (url.startsWith("/delete-goods") && method === "DELETE") {
const id = url.split("/")[2];
const result = goodsDb.filter((e) => e.id !== id);
fs.writeFile("./db/dbGoods.json", JSON.stringify(result, null, 2), "utf-8", (error, data) => {
if (error) {
res.writeHead(500, {"content-type": "text/plain"});
res.end("Error deleting goods");
} else {
res.writeHead(200, {"content-type": "application/json"});
res.end(JSON.stringify({
message: "Goods deleted succesfully"
}))
}
})
}
});
server.listen(PORT, () => {
console.log("Server is running on Port:", PORT);
});