-
Notifications
You must be signed in to change notification settings - Fork 238
/
app.js
209 lines (165 loc) · 4.71 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
const express = require("express");
const logger = require("morgan");
const jsonServer = require("json-server");
const url = require("url");
const env = process.env.NODE_ENV || "development";
const { flatten } = require("ramda");
const server = jsonServer.create();
const router =
env === "test"
? jsonServer.router("db.test.json")
: jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(logger("dev"));
server.use(express.json());
server.use(express.urlencoded({ extended: false }));
require("./server/routes/")(server, express);
function isAuthorized(req) {
if (req.headers.authorization) {
const user_and_password = new Buffer.from(
req.headers.authorization.split(" ")[1],
"base64"
).toString();
const user = user_and_password.split(":")[0];
const pw = user_and_password.split(":")[1];
return (
user === "admin" &&
((env === "test" && pw === "admin_test") || pw === "admin")
);
} else {
return false;
}
}
const bookRouteNeedsAuth = (req) =>
req.url.match(/books/) && (req.method === "DELETE" || req.method === "PUT");
const userRouteNeedsAuth = (req) =>
req.url.match(/users/) && req.method === "DELETE";
server.use((req, res, next) => {
if (
(bookRouteNeedsAuth(req) || userRouteNeedsAuth(req)) &&
!isAuthorized(req)
) {
res.sendStatus(401);
} else {
next();
}
});
server.use((req, res, next) => {
if (req.method === "POST") {
if ((req.path === "/books" || req.path === "/books/") && !req.body.title) {
res.status(500).send({ error: "Title cannot be null" });
return;
}
req.body.createdAt = new Date().toISOString();
req.body.updatedAt = new Date().toISOString();
next();
} else if (req.method === "PUT") {
req.body.updatedAt = new Date().toISOString();
req.method = "PATCH";
next();
} else {
next();
}
});
function fullUrl(req) {
return url.format({
protocol: req.protocol,
host: req.get("host"),
pathname: req.originalUrl,
});
}
router.render = (req, res) => {
if (req.method === "DELETE") {
res.status(204);
}
if (req.method === "POST" && res.statusCode === 201) {
res.locals.data.links = [
{
rel: "self",
href: `${fullUrl(req)}/${res.locals.data.id}`,
},
];
}
res.jsonp(res.locals.data);
};
server.post("/users", (req, res, next) => {
const db = router.db;
const collection = db.defaults({ wishlists: [] }).get("wishlists");
const newWishlist = collection
.insert({ name: `${req.body.firstName}'s List`, books: [] })
.write();
req.body.wishlistId = newWishlist.id;
next();
});
server.post("/wishlists/:wishlistId/books/:bookId", (req, res) => {
const db = router.db;
const wishlist = db
.defaults({ wishlists: [] })
.get("wishlists")
.find({ id: parseInt(req.params.wishlistId, 10) })
.value();
wishlist.books = wishlist.books || [];
const bookId = parseInt(req.params.bookId, 10);
const indexOf = wishlist.books.indexOf(bookId);
if (indexOf < 0) {
wishlist.books.push(bookId);
}
db.write();
res.sendStatus(204);
});
const getBooksForWishlist = (wishlistId, db) => {
const wishlist = db
.defaults({ wishlists: [] })
.get("wishlists")
.find({ id: wishlistId })
.cloneDeep()
.value();
const fullBooks = wishlist.books
? wishlist.books.map((b) =>
db.get("books").find({ id: b }).cloneDeep().value()
)
: [];
wishlist.books = fullBooks;
return wishlist;
};
server.get("/wishlists/:wishlistId/books", (req, res) => {
const wishlist = getBooksForWishlist(
parseInt(req.params.wishlistId),
router.db
);
res.status(200).jsonp(wishlist);
});
server.get("/households/:householdId/wishlistBooks", (req, res) => {
const db = router.db;
const users = db
.get("users")
.filter({ householdId: parseInt(req.params.householdId, 10) })
.cloneDeep()
.value();
const wishlists = users.map((u) => getBooksForWishlist(u.wishlistId, db));
const books = wishlists.map((w) => w.books.map((b) => b));
res.status(200).jsonp(flatten(books));
});
server.get("/books/search", (req, res) => {
const db = router.db;
const books = db
.get("books")
.filter(
(r) =>
(req.query.title
? r.title.toLowerCase().includes(req.query.title.toLowerCase())
: true) &&
(req.query.author
? r.author.toLowerCase().includes(req.query.author.toLowerCase())
: true)
)
.value();
res.status(200).send(books);
});
server.use(router);
server.get("*", (req, res) => res.status(404).send());
server.listen(process.env.PORT || 3000, () => {
console.log(`Server running: ${process.env.PORT || 3000}`);
});
module.exports = server;