-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (111 loc) · 3.32 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
const express = require('express');
const app = express();
const { db } = require('./dbConnection');
const fetch = require('isomorphic-fetch');
const { hashPassword, authenticationMiddleware } = require('./authenticationController');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const { addItemToCart } = require('./cartController');
app.use(async (req, res, next) => {
if (req.url.startsWith("/carts")) {
return authenticationMiddleware(req, res, next);
}
await next();
});
app.get('/inventory/:itemName', async (req ,res) => {
const { itemName } = req.params;
const response = await fetch(`http://recipepuppy.com/api?i=${itemName}`);
const { title, href, results: recipes } = await response.json();
const inventoryItem = await db
.select()
.from('inventory')
.where({ itemName })
.first();
res.status(200).json({
...inventoryItem,
info: `Data obtained from ${title} - ${href}`,
recipes
});
});
app.put('/users/:username', async (req, res) => {
const { username } = req.params;
const { email, password } = req.body;
const userAlreadyExists = await db
.select()
.from("users")
.where({ username })
.first();
if (userAlreadyExists) {
return res.status(409).json({ message: `${username} already exists.` });
}
await db("users").insert({
username,
email,
passwordHash: hashPassword(password)
});
res.status(201).json({ message: `${username} created successfully` });
});
app.post('/carts/:username/items', async (req, res) => {
const { username } = req.params;
const { item, quantity } = req.body;
let newItems;
for (let i = 0; i < quantity; i++) {
try {
newItems = await addItemToCart(username, item);
} catch (err) {
return res.status(400).json({ message: err.message });
}
}
res.status(200).json(newItems);
});
app.post('/carts/:username/items/:item', (req, res) => {
try {
const { username, item } = req.params;
const newItems = addItemToCart(username, item);
res.status(201).json(newItems);
} catch (err) {
res.status(err.code).json(err.message);
}
});
app.delete('/carts/:username/items/:item', async (req, res) => {
const { username, item } = req.params;
const user = await db
.select()
.from("users")
.where({ username })
.first();
if (!user) {
return res.status(404).json({ message: 'User not found.' });
}
const itemEntry = await db
.select()
.from("carts_items")
.where({ userId: user.id, itemName: item })
.first();
if (!itemEntry || itemEntry.quantity === 0) {
return res.status(400).json({ message: `${item} is not in the cart` });
}
await db("carts_items")
.decrement("quantity")
.where({ userId: user.id, itemName: item });
const inventoryEntry = await db
.select()
.from("inventory")
.where({ itemName: item })
.first();
if (inventoryEntry) {
await db("inventory")
.increment("quantity")
.where({ userId: itemEntry.userId, itemName: item });
} else {
await db("inventory").insert({ itemName: item, quantity: 1 });
}
const responseBody = await db
.select("itemName", "quantity")
.from("carts_items")
.where({ userId: user.id });
res.status(200).send(responseBody);
});
module.exports = {
app,
};