-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (147 loc) · 4.1 KB
/
app.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
153
const express = require('express');
const db = require('./db/db.js');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Get all menu sections
app.get('/menusection', (req, res) => {
db.getAllMenuSections((err, result) => {
if (err) {
console.log("err");
console.log(err);
res.status(400).send({
success: false
});
}
else {
res.status(200).send({
MenuSection: result
});
}
});
});
//Get Menu Section by id
app.get('/menusection/:id', (req, res) => {
const id = parseInt(req.params.id);
db.getMenuSectionById(id, (err, result) => {
if (err) {
console.log("err");
console.log(err);
res.status(400).send({
success: false
});
}
if (result != undefined && result.length != 0) {
res.status(200).send({
MenuSection: result
});
}
else {
res.status(404).send({
success: false,
message: 'id not found'
});
}
});
});
// Add menu section, body.name = name
app.post('/menusection', (req, res) => {
console.log(req.body);
if (!req.body.name) {
res.status(400).send({
success: false,
message: 'name is missing'
});
} else {
const name = req.body.name;
db.addMenuSection(name, (err, result) => {
if (err) {
console.log("err");
console.log(err);
res.status(400).send({
success: false
});
}
else {
res.status(200).send({
success: true,
MenuSection: result
});
}
});
}
});
// Edit menu section with id to new name
app.post('/menusection/:id', (req, res) => {
console.log(req.body);
const id = parseInt(req.params.id);
if (!req.body.name) {
res.status(400).send({
success: false,
message: 'name is missing'
});
} else {
const name = req.body.name;
db.editMenuSection(id, name, (err, result) => {
if (err) {
console.log("err");
console.log(err);
res.status(400).send({
success: false
});
} else if (result != undefined && result.length != 0) {
res.status(200).send({
success: true,
MenuSection: result
});
}
else {
res.status(404).send({
success: false,
message: 'id not found'
});
}
});
}
});
// Delete menu section by ID, gets entry with id first to check if it exists
app.delete('/menusection/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
db.getMenuSectionById(id, (err, result) => {
if (err) {
console.log("err");
console.log(err);
res.status(400).send({
success: false
});
}
if (result != undefined && result.length != 0) {
db.deleteMenuSection(id, (err, result) => {
console.log(result);
if (err) {
console.log("err");
console.log(err);
res.status(400).send({
success: false
});
}
else {
res.status(200).send({
success: true
});
}
});
}
else {
res.status(404).send({
success: false,
message: 'id not found'
});
}
});
});
const PORT = 5000;
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`)
});