-
Notifications
You must be signed in to change notification settings - Fork 0
/
jellyBean API
146 lines (128 loc) · 3.55 KB
/
jellyBean API
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
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4001;
app.use(express.static('public'));
const jellybeanBag = {
mystery: {
number: 4
},
lemon: {
number: 5
},
rootBeer: {
number: 25
},
cherry: {
number: 3
},
licorice: {
number: 1
}
};
// Add your logging function here:
const logRequest = (verb) => {
console.log(`${verb} Request Received`);
}
app.get('/beans/', (req, res, next) => {
logRequest('GET');
res.send(jellybeanBag);
console.log('Response Sent');
});
app.post('/beans/', (req, res, next) => {
logRequest('POST');
let queryData = '';
req.on('data', (data) => {
queryData += data;
});
req.on('end', () => {
const body = JSON.parse(queryData);
const beanName = body.name;
if (jellybeanBag[beanName] || jellybeanBag[beanName] === 0) {
return res.status(400).send('Bag with that name already exists!');
}
const numberOfBeans = Number(body.number) || 0;
jellybeanBag[beanName] = {
number: numberOfBeans
};
res.send(jellybeanBag[beanName]);
console.log('Response Sent');
});
});
app.get('/beans/:beanName', (req, res, next) => {
logRequest('GET');
const beanName = req.params.beanName;
if (!jellybeanBag[beanName]) {
console.log('Response Sent');
return res.status(404).send('Bean with that name does not exist');
}
res.send(jellybeanBag[beanName]);
console.log('Response Sent');
});
app.post('/beans/:beanName/add', (req, res, next) => {
logRequest('POST');
const beanName = req.params.beanName;
if (!jellybeanBag[beanName]) {
return res.status(404).send('Bean with that name does not exist');
}
let queryData = '';
req.on('data', (data) => {
queryData += data;
});
req.on('end', () => {
const numberOfBeans = Number(JSON.parse(queryData).number) || 0;
jellybeanBag[beanName].number += numberOfBeans;
res.send(jellybeanBag[beanName]);
console.log('Response Sent');
});
});
app.post('/beans/:beanName/remove', (req, res, next) => {
logRequest('POST');
const beanName = req.params.beanName;
if (!jellybeanBag[beanName]) {
return res.status(404).send('Bean with that name does not exist');
}
let queryData = '';
req.on('data', (data) => {
queryData += data;
});
req.on('end', () => {
const numberOfBeans = Number(JSON.parse(queryData).number) || 0;
if (jellybeanBag[beanName].number < numberOfBeans) {
return res.status(400).send('Not enough beans in the jar to remove!');
}
jellybeanBag[beanName].number -= numberOfBeans;
res.send(jellybeanBag[beanName]);
console.log('Response Sent');
});
});
app.delete('/beans/:beanName', (req, res, next) => {
logRequest('DELETE');
const beanName = req.params.beanName;
if (!jellybeanBag[beanName]) {
return res.status(404).send('Bean with that name does not exist');
}
jellybeanBag[beanName] = null;
res.status(204).send();
console.log('Response Sent');
});
app.put('/beans/:beanName/name', (req, res, next) => {
logRequest('PUT');
const beanName = req.params.beanName;
if (!jellybeanBag[beanName]) {
return res.status(404).send('Bean with that name does not exist');
}
let queryData = '';
req.on('data', (data) => {
queryData += data;
});
req.on('end', () => {
const newName = JSON.parse(queryData).name;
jellybeanBag[newName] = jellybeanBag[beanName];
jellybeanBag[beanName] = null;
res.send(jellybeanBag[newName]);
console.log('Response Sent');
});
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});