-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
152 lines (136 loc) · 4.59 KB
/
server.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Allan Cao
// Student ID 101218998
//const http = require("http");
const fs = require("fs");
//const url = require("url");
//const pug = require("pug");
const express = require("express");
let app = express();
app.use(express.static("public"));
//app.use(express.urlencoded({extended:false}));
app.use(express.json());
app.set("views", "./views");
app.set("view engine", "pug");
/*
app.use(function(req,res,next) {
console.log("Method: ", req.method, ", URL:" , req.url);
//console.log("Path: ", req.path);
console.log("Query Params: ", req.query);
console.log("Body: ", req.body);
next();
});
*/
let vendors = {};
let vendorMaxId = -1;
let vendorMaxSupplyId = {};
//Just a isEmpty helper function
function isEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
//Loading vendors from the .json files
function loadVendors() {
fs.readdir("./vendors", function (err, files) {
files.forEach(file => {
if(!file.endsWith(".json")) return;
let vendor = require("./vendors/" + file)
vendors[vendor.id] = vendor;
vendorMaxId = Math.max(vendorMaxId, vendor.id); //Finding the max id for vendors at the moment
let maxSupplyId = -1;
for(let cat in vendor.supplies) {
for(let sid in vendor.supplies[cat]) {
maxSupplyId = Math.max(maxSupplyId, sid); //Finding the max id for supplies at the moment
}
}
vendorMaxSupplyId[vendor.id] = maxSupplyId;
//console.log(`load vendor ${vendor.id} ${vendor.name} from vendors/${file} file`);
})
});
}
loadVendors(); //Calling the function
//Rendering the home page
app.get("/", function(req,res,next){
res.render("pages/index", {})
})
// Rendering the list of vendors
app.get("/vendors", function(req,res,next){
let array = [];
for(let id in vendors) {
array.push(vendors[id]);
}
if(req.header('Accept').includes("application/json")) { //if the client requests a json then we send a json
res.json({vendors: array});
} else {
res.render("pages/vendors.pug", {vendors : array}) //if the client request an html then we render the pug file
}
})
// Rendering the vendor page
app.get("/vendors/:VendorID", function(req,res,next){
let vendor = vendors[req.params.VendorID];
if(vendor==null) {
res.status(404).send("unknown vendor id " + req.params.VendorID);
} else if(req.header('Accept').includes("application/json")) {
res.json(vendor);
} else {
res.render("pages/vendor.pug", {vendor : vendor})
}
})
//Rendering the add vendor page
app.get("/addVendor", function(req,res,next){
res.render("pages/addVendor", {});
})
//Adding a vendor
app.post("/vendors", function(req,res,next){
let newVendor = req.body;
if(!(newVendor.name && newVendor.delivery_fee > 0 && newVendor.min_order > 0)){
res.status(400).send("Invalid Input");
return;
}
let found = false;
for(let id in vendors) {
if(vendors[id].name == newVendor.name) {
found = true;
break;
}
}
if(found) {
res.status(400).send("existing vendor name");
return;
}
newVendor.id = ++vendorMaxId;
newVendor.supplies = {};
vendors[newVendor.id] = newVendor;
vendorMaxSupplyId[newVendor.id] = -1;
res.json(newVendor); //json rep of new vendor
})
//Changing vendor information
app.put("/vendors/:vendorID", function(req,res,next){
let vendor = vendors[req.params.vendorID];
if(vendor == null){
res.status(404).send("Invalid vendorID")
return;
}
let changes = req.body;
for(let key in changes){
if(key == "name" || key == "min_order" || key == "delivery_fee"){
vendor[key] = changes[key];
}else if(key == "supplies"){
for(let cate in changes.supplies) {
if(!vendor.supplies.hasOwnProperty(cate)) {
vendor.supplies[cate] = {};
}
for(let sid in changes.supplies[cate]) {
let item = changes.supplies[cate][sid];
let supplyId = ++vendorMaxSupplyId[vendor.id];
vendor.supplies[cate][supplyId] = item;
}
}
}
}
res.status(200).send("ok");
})
app.listen(3000);
console.log("Server listening at http://localhost:3000");