-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
355 lines (325 loc) · 9.23 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import express from 'express';
import cors from 'cors';
const app = express();
const environment = process.env.NODE_ENV || 'development';
const configuration = require('./knexfile')[environment];
const database = require('knex')(configuration);
app.locals.title = 'picasso palette picker';
app.use(cors());
app.use(express.json());
app.get('/', (request, response) => {
response.send("We're going to test all the routes!");
});
app.get('/api/v1/users/:id/catalogs', async (request, response) => {
try {
const catalogs = await database('catalogs')
.where('user_id', request.params.id)
.select();
if (catalogs.length) {
response.status(200).json(catalogs);
} else {
return response.status(404).send({ error: 'Catalogs not found' });
}
} catch (error) {
response.status(500).json({ error });
}
});
app.get(
'/api/v1/users/:userId/catalogs/:catalogId',
async (request, response) => {
try {
const { catalogId, userId } = request.params;
const catalog = await database('catalogs')
.where('id', catalogId)
.where('user_id', userId)
.select();
if (catalog.length) {
response.status(200).json(catalog);
} else {
return response.status(404).send({ error: 'Catalog not found' });
}
} catch (error) {
response.status(500).json({ error });
}
}
);
app.get(
'/api/v1/users/:userId/catalogs/:catalogId/palettes',
async (request, response) => {
try {
const { catalogId } = request.params;
const palettes = await database('palettes')
.where('catalog_id', catalogId)
.select();
if (palettes.length) {
response.status(200).json(palettes);
} else {
return response.status(404).send({ error: 'No palettes were found' });
}
} catch (error) {
response.status(500).json({ error });
}
}
);
app.get(
'/api/v1/users/:userId/catalogs/:catalogId/palettes/:paletteId',
async (request, response) => {
try {
const palette = await database('palettes')
.where('id', request.params.paletteId)
.select();
if (palette.length) {
response.status(200).json(palette);
} else {
return response.status(404).send({ error: 'Cannot get palette' });
}
} catch (error) {
response.status(500).json(error);
}
}
);
app.get('/api/v1/searchdatabase/?', async (request, response) => {
try {
const itemFromDatabase = await database(`${request.query.database}`).where(
'id',
request.query.id
);
if (itemFromDatabase.length) {
response.status(200).json(itemFromDatabase);
} else {
let returnWord = request.query.database.split('');
returnWord.pop();
return response
.status(404)
.send({ error: `${returnWord.join('')} not found` });
}
} catch {
response.status(500).json({ error: '500: Internal Server Error' });
}
});
app.get('/api/v1/users/:userId/palettes', async (request, response) => {
const { userId } = request.params;
try {
const catalogs = await database('catalogs').where('user_id', userId)
const allReducedPalettes = await catalogs.reduce( async (acc, catalog) => {
if (!acc.length) {
acc = []
const palettes = await database('palettes').where('catalog_id', catalog.id)
acc = [...palettes]
} else {
const palettes = await database('palettes').where('catalog_id', catalog.id)
acc = [...acc, ...palettes]
}
return acc
}, [])
if (allReducedPalettes.length) {
response.status(200).json(allReducedPalettes);
} else {
return response
.status(404)
.send({ error: `No Palettes found with api /api/v1/users/:userId/palettes` });
}
} catch {
response.status(500).json({ error: '500: Internal Server Error' });
}
});
app.post('/api/v1/login', async (request, response) => {
try {
const { email, password } = request.body;
const currentLogin = await database('users')
.where('email', email)
.select();
if (currentLogin.length && password === currentLogin[0].password) {
const { firstName, id } = currentLogin[0];
return response.status(200).send({ firstName, id });
} else if (currentLogin.length) {
return response.status(404).send({ error: 'Incorrect Password' });
} else {
return response.status(404).send({ error: 'Email not found' });
}
} catch (error) {
response.status(500).json(error);
}
});
app.post('/api/v1/users', async (request, response) => {
const newUser = request.body;
for (let requiredParameter of [
'firstName',
'lastName',
'email',
'password'
]) {
if (!newUser[requiredParameter]) {
return response.status(422).send({
error: `Expected format: {
"firstName": <String>,
"lastName": <String>,
"email": <String>,
"password": <String>,
}. You're missing a "${requiredParameter}" property.`
});
}
}
try {
const emailExists = await database('users').where('email', newUser.email);
if (emailExists.length) {
return response.status(422).send({
error: 'The request could not be completed due to email already in use'
});
}
} catch {
response.status(500).json({ error: '500: Internal Server Error' });
}
try {
const newAddedUser = await database('users').insert(newUser, 'id');
response
.status(201)
.send({ firstName: newUser.firstName, id: newAddedUser[0] });
} catch {
response.status(500).json({ error: '500: Internal Server Error' });
}
});
app.post('/api/v1/users/:userId/catalogs', async (request, response) => {
const newCatalog = request.body;
for (let requiredParameter of ['catalogName', 'user_id']) {
if (!newCatalog[requiredParameter]) {
return response.status(422).send({
error: `Expected format: { catalogName: <string>, user_id: <integer> }. You are missing a ${requiredParameter} property.`
});
}
}
try {
const catalogs = await database('catalogs').insert(newCatalog, 'id');
if (catalogs.length) {
const { catalogName } = newCatalog;
response.status(201).send({ catalogName, id: catalogs[0] });
} else {
response
.status(404)
.send({ error: 'The catalog could not be submitted' });
}
} catch (error) {
response.status(500).json({ error });
}
});
app.post('/api/v1/users/:userId/catalogs/:catalogId/palettes', async (request, response) => {
const newPalette = request.body;
for (let requiredParameter of [
'paletteName',
'catalog_id',
'colors'
]) {
if (!newPalette[requiredParameter]) {
return response.status(422).send({
error: `Expected format: { paletteName: <string>, catalog_id: <integer>, colors: <array of objects> }. You are missing a ${requiredParameter} property.`
});
}
}
try {
const palettes = await database('palettes').insert(newPalette, 'id');
if (palettes.length) {
const { paletteName } = newPalette;
return response.status(201).send({ paletteName, id: palettes[0] });
} else {
response
.status(404)
.send({ error: 'The catalog could not be submitted' });
}
} catch (error) {
response.status(500).json({ error });
}
}
);
app.patch(
'/api/v1/users/:userId/catalogs/:catalogId',
async (request, response) => {
try {
const { newName } = request.body;
const catalog = await database('catalogs').where(
'id',
request.params.catalogId
);
if (catalog.length) {
await database('catalogs')
.where('id', request.params.catalogId)
.update({ catalogName: newName });
return response.status(200).send({ newName });
} else {
return response
.status(404)
.send({ error: 'Catalog not found - unable to update catalog name' });
}
} catch (error) {
response.status(500).json(error);
}
}
);
app.patch(
'/api/v1/users/:userId/catalogs/:catalogId/palettes/:paletteId',
async (request, response) => {
try {
const { catalogId, paletteId } = request.params;
const newPatch = request.body;
const palette = await database('palettes')
.where('id', paletteId)
.where('catalog_id', catalogId);
if (palette.length) {
await database('palettes')
.where('id', paletteId)
.where('catalog_id', catalogId)
.update(newPatch);
return response.status(200).send(newPatch);
} else {
return response.status(404).send({
error: 'Palette not found - unable to update palette'
});
}
} catch (error) {
response.status(500).json(error);
}
}
);
app.delete(
'/api/v1/users/:userId/catalogs/:catalogId/palettes/:paletteId',
async (request, response) => {
try {
const { catalogId, paletteId } = request.params;
const palettes = await database('palettes')
.where('id', paletteId)
.where('catalog_id', catalogId)
.del();
if (palettes === 0) {
return response.status(204).json();
}
response
.status(202)
.json(`Palette ${paletteId} was successfully removed`);
} catch (error) {
response.status(500).json({ error });
}
}
);
app.delete(
'/api/v1/users/:userId/catalogs/:catalogId',
async (request, response) => {
const { userId, catalogId } = request.params;
try {
await database('palettes')
.where('catalog_id', catalogId)
.del();
const catalog = await database('catalogs')
.where('id', catalogId)
.where('user_id', userId)
.del();
if (catalog === 0) {
return response.status(204).json();
}
response
.status(202)
.json(`Catalog ${catalogId} was successfully removed`);
} catch (error) {
response.status(500).json({ error });
}
}
);
export default app;