-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
320 lines (250 loc) · 8.18 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const express = require("express");
const cors = require("cors");
let data = require("./data/gamestores.js");
const searchUtils = require("./utils/searchUtils.js");
const app = express();
const PORT = process.env.PORT || 8081;
app.use(cors());
app.use(express.json());
// get all stores
app.get('/', (req, res) => {
return res.json(data);
});
// get featured stores
app.get('/featuredstores', (req, res) => {
const featured = [...data];
featured.length = 3;
return res.json(featured);
});
// get store by id
app.get('/store/:id', (req, res) => {
const store = data.find(store => store.id === req.params.id);
return res.json(store);
});
// simple search (search bar)
app.post('/simplesearch', (req, res) => {
if (req.body.searchString){
const { searchString } = req.body;
const filteredData = data.filter( store => {
if (searchUtils.nameMatch(store,searchString)
|| searchUtils.cityMatch(store,searchString)
|| searchUtils.stateMatch(store,searchString)
|| searchUtils.zipMatch(store,searchString)){
return store;
};
const productFound = store.productsServices.find(product => {
if (searchUtils.productMatch(product, searchString)){
return product;
}
});
if(productFound){
return store;
};
});
return res.json(filteredData);
}
return res.json({});
});
// advanced search
app.post('/advancedsearch', (req, res) => {
console.log('route /advancedsearch requested')
console.log('logging test: ', req.body)
const { storeName, city, state, zip, mtg, dnd, ygo, pok, soc} = req.body;
const filteredData = data.filter( store => {
if (searchUtils.nameMatch(store,storeName)
&& searchUtils.cityMatch(store,city)
&& searchUtils.stateMatch(store,state)
&& searchUtils.zipMatch(store,zip)){
return store;
}
})
console.log('filteredData = ', filteredData)
return res.json(filteredData);
});
// create event
app.post('/createevent', (req, res) => {
console.log('route /createevent requested');
const { newEvent, storeID } = req.body;
console.log('logging test: ', newEvent);
let storetoreturn = {};
data.forEach( store => {
if (store.id === storeID){
newEvent.id = store.events[store.events.length - 1].id + 1;
store.events.push(newEvent);
storetoreturn = store;
}
});
return res.json(storetoreturn);
});
// create product
app.post('/createproduct', (req, res) => {
console.log('route /createproduct requested');
const { newProduct, storeID } = req.body;
console.log('logging test: ', newProduct);
let storetoreturn = {};
data.forEach( store => {
if (store.id === storeID){
newProduct.id = store.productsServices[store.productsServices.length - 1].id + 1;
store.productsServices.push(newProduct);
storetoreturn = store;
}
});
return res.json(storetoreturn);
});
// create image
app.post('/createimage', (req, res) => {
console.log('route /createimage requested');
const { newImage, storeID } = req.body;
console.log('logging test: ', newImage);
let storetoreturn = {};
data.forEach( store => {
if (store.id === storeID){
store.gallery.push(newImage);
storetoreturn = store;
}
});
return res.json(storetoreturn);
});
// create store
app.post('/createstore', (req, res) => {
console.log('route /createstore requested');
const { newStore } = req.body;
console.log('logging test: ', newStore.name);
// create new id for store
let newStoreID = +data[data.length - 1].id + 1;
newStoreID = newStoreID.toString();
console.log('logging test: newStoreID = ', newStoreID);
// assign ID
newStore.id = newStoreID;
// add to data array
data.push(newStore);
return res.json(newStore);
});
// edit event
app.put('/editevent', (req, res) => {
console.log('route /editevent requested');
const { edittedEvent, storeID } = req.body;
console.log('logging test: ', edittedEvent);
let storetoreturn = {};
data.forEach( (store, i) => {
if (store.id === storeID){
store.events.forEach( (event, j) => {
if (event.id === edittedEvent.id){
data[i].events[j] = edittedEvent;
}
})
storetoreturn = data[i];
}
});
console.log('saved store is: ', storetoreturn);
return res.json(storetoreturn);
});
// edit product
app.put('/editproduct', (req, res) => {
console.log('route /editproduct requested');
const { edittedProduct, storeID } = req.body;
console.log('logging test: ', edittedProduct);
let storetoreturn = {};
data.forEach( (store, i) => {
if (store.id === storeID){
store.productsServices.forEach( (product, j) => {
if (product.id === edittedProduct.id){
data[i].productsServices[j] = edittedProduct;
}
})
storetoreturn = data[i];
}
});
console.log('saved store is: ', storetoreturn);
return res.json(storetoreturn);
});
// edit store
app.put('/editstore', (req, res) => {
console.log('route /editstore requested');
const { edittedStore, storeID } = req.body;
console.log('logging test: ', edittedStore);
let storetoreturn = {};
data.forEach( (store, i) => {
if (store.id === storeID) {
data[i] = edittedStore;
storetoreturn = data[i];
}
})
console.log('saved store is: ', storetoreturn);
return res.json(storetoreturn);
});
// delete store
app.delete('/deletestore', (req,res) => {
console.log('route /deletestore requested');
console.log('logging test 1: ', req.body);
const { storeID } = req.body;
data = data.filter( store => {
if (store.id !== storeID) {
return store;
}
})
return res.json(data);
});
// delete event
app.delete('/deleteevent', (req,res) => {
console.log('route /deleteevent requested');
console.log('logging test 1: ', req.body);
const { eventID, storeID } = req.body;
let storetoreturn = {};
console.log('logging test 2: ', eventID);
data.forEach( store => {
if (store.id === storeID){
store.events = store.events.filter(event => {
if(event.id !== eventID){
return event;
}
})
storetoreturn = store;
}
})
console.log('saved store is: ', storetoreturn);
return res.json(storetoreturn);
});
// delete product
app.delete('/deleteproduct', (req,res) => {
console.log('route /deleteproduct requested');
console.log('logging test 1: ', req.body);
const { productID, storeID } = req.body;
let storetoreturn = {};
console.log('logging test 2: ', productID);
data.forEach( store => {
if (store.id === storeID){
store.productsServices = store.productsServices.filter(product => {
if(product.id !== productID){
return product;
}
})
storetoreturn = store;
}
})
console.log('saved store is: ', storetoreturn);
return res.json(storetoreturn);
});
// delete image
app.delete('/deleteimage', (req,res) => {
console.log('route /deleteimage requested');
console.log('logging test 1: ', req.body);
const { imageToDelete, storeID } = req.body;
let storetoreturn = {};
console.log('logging test 2: ', imageToDelete);
data.forEach( store => {
if (store.id === storeID){
store.gallery = store.gallery.filter(image => {
if(image.id !== imageToDelete.id){
return image;
}
})
storetoreturn = store;
}
})
console.log('saved store is: ', storetoreturn);
return res.json(storetoreturn);
});
app.listen(PORT, () => {
console.log('app now listening on ' + PORT);
});