-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (183 loc) · 5.7 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
require("dotenv").config();
const express = require("express");
const { getGames, setGames, removeGame } = require("./lib/games");
const {
getWishlist,
setWishlist,
removeWishlistEntry
} = require("./lib/wishlist");
const { getUsers, setUser, validateUser } = require("./lib/users");
const { initDatabase } = require("./lib/database");
const {
createSession,
getUserBySession,
deleteSession
} = require("./lib/sessions");
const axios = require("axios");
const app = express();
const path = require("path");
const cookieParser = require("cookie-parser");
app.use(express.json());
app.use(cookieParser());
app.get(`/api/wishlist`, async (request, response) => {
try {
const user = getUserBySession(request.cookies.session);
if (!user) return response.status(403).end("unauthorized request");
if (user) {
const wishlist = await getWishlist(user.name);
return response.json(wishlist);
} else {
response.send("User not found");
}
} catch (error) {
return response.end("Error");
}
response.end();
});
app.get(`/api/games`, async (request, response) => {
try {
const user = getUserBySession(request.cookies.session);
if (!user) return response.status(403).end("unauthorized request");
if (user) {
const games = await getGames(user.name);
return response.json(games);
} else {
response.send("User not found");
}
} catch (error) {
console.log(error);
return response.end("Error");
}
});
app.post("/api/wishlist", async (request, response) => {
try {
const user = getUserBySession(request.cookies.session);
if (!user) return response.status(403).end("unauthorized request");
request.body.owner = user.name;
const WishlistEntry = await setWishlist(request.body);
return response.json({ WishlistEntry });
} catch (error) {
console.error(error);
response.end(error);
}
});
app.post("/api/games", async (request, response) => {
try {
const user = getUserBySession(request.cookies.session);
if (!user) return response.status(403).end("unauthorized request");
request.body.owner = user.name;
request.body.date_added = Date.now();
const newGame = await setGames(request.body);
return response.json({ newGame });
} catch (error) {
console.error(error);
response.end("Error");
}
});
app.get("/api/users", async (request, response) => {
try {
const users = await getUsers(request.params);
return response.json(users);
} catch (error) {
return response.end("Error");
}
});
app.post("/api/users", async (request, response) => {
try {
const emailExist = await getUsers(request.body.email);
if (emailExist) {
return response.status(400).json("email already exists");
} else {
await setUser(request.body);
return response.status(200).json("Account succesfully created");
}
} catch (error) {
console.error(error);
response.end("Error");
}
});
app.post("/api/login", async (request, response) => {
try {
const userExist = await validateUser(request.body);
if (userExist) {
const sessionId = createSession(userExist);
response.cookie("session", sessionId);
return response.json({ name: userExist.name });
} else if (!userExist) {
return response.status(401).json("access denied");
}
} catch (error) {
response.end("Error");
console.log(error);
}
response.end();
});
app.post("/api/logout", async (request, response) => {
try {
const session = request.cookies.session;
deleteSession(session);
response.clearCookie("session");
return response.end();
} catch (error) {
response.end();
}
});
app.get("/api/search", async (request, response) => {
const name = request.query.name;
const targetUrl = `https://www.boardgameatlas.com/api/search?name=${name}&limit=15&fields=id,name,description,image_url,publishers,average_user_rating,num_user_ratings,mechanics,categories,min_players,max_players,min_playtime,max_playtime&client_id=5cIY9zBPpt`;
return axios({
url: targetUrl,
method: "GET",
headers: {
Accept: "application/json",
"client-id": "5cIY9zBPpt"
}
})
.then(response => response.data)
.then(games => response.json(games));
});
app.get("/api/mechanics", async (request, response) => {
const targetUrl = `https://www.boardgameatlas.com/api/game/mechanics?client_id=5cIY9zBPpt`;
return axios({
url: targetUrl,
method: "GET",
headers: {
Accept: "application/json",
"client-id": "5cIY9zBPpt"
}
})
.then(response => response.data)
.then(mechanics => response.json(mechanics));
});
app.post("/api/wishlistremove", async (request, response) => {
try {
const user = getUserBySession(request.cookies.session);
if (!user) return response.status(403).end("unauthorized request");
removeWishlistEntry(request.body.name);
response.send("Wishlist entry deleted");
} catch (error) {
console.error;
}
});
app.post("/api/gamesremove", async (request, response) => {
try {
const user = getUserBySession(request.cookies.session);
if (!user) return response.status(403).end("unauthorized request");
removeGame(request.body.name);
response.send("Game deleted");
} catch (error) {
console.error;
}
});
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
initDatabase(process.env.DB_URL, process.env.DB_NAME).then(() => {
console.log(`Database ${(process.env.DB_URL, process.env.DB_NAME)} is ready`);
app.listen(process.env.PORT, () => {
console.log(`Server listens on http://localhost:${process.env.PORT}`);
});
});