forked from bengdawson/final-project-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
361 lines (319 loc) · 11.4 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
356
357
358
359
360
361
const ApiBuilder = require("claudia-api-builder");
const { request } = require("express");
const api = new ApiBuilder();
//const mysql = require("mysql2/promise");
const express = require("express");
const pool = require("./middleware/pool");
const format = require("pg-format");
// USERS
//PATCH single user by username
api.patch(
"/users/{username}",
async function ({ body, pathParams }) {
const { username } = pathParams;
const { newUsername, newAvatar_url } = body;
let queryStr = "UPDATE users SET ";
if (newUsername) {
queryStr += `username="${newUsername}"`;
}
if (newUsername && newAvatar_url) {
queryStr += ", ";
}
if (newAvatar_url) {
queryStr += `avatar_url="${newAvatar_url}"`;
}
queryStr += `WHERE username="${username}";`;
const patchedUser = await pool.query(queryStr);
return { user_updated: "you updated the user!" };
// UPDATE user SET username="${newUsername}", avatar_url="${newAvatar_url}" WHERE username="${username}";
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//GET single user by username
api.get(
"/users/{username}",
async function ({ pathParams }) {
const { username } = pathParams;
const queryStr = format("SELECT * FROM users WHERE username = %L", [
username,
]);
const usersFromApi = await pool.query(queryStr);
const result = Object.values(JSON.parse(JSON.stringify(usersFromApi[0])));
return { users: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//GET all users
api.get(
"/users",
async function () {
const usersFromApi = await pool.query("SELECT * FROM users;");
const result = Object.values(JSON.parse(JSON.stringify(usersFromApi[0])));
return { users: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//POST a user
api.post(
"/users",
async function ({ body }, res) {
const {
username,
avatar_url,
firstname,
lastname,
dbs,
drive,
email,
password,
} = body;
const queryStr = format(
`INSERT INTO users (username,
avatar_url,
firstname,
lastname,
dbs,
drive,
email, password) VALUES (%L);`,
[username, avatar_url, firstname, lastname, dbs, drive, email, password]
);
const newUserFromApi = await pool.query(queryStr);
console.log(newUserFromApi, "<<< newUser");
console.log(res, "<<<<RES");
return { user_posted: "new user posted" };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
// ORGANISATIONS
//POST org
api.post(
"/orgs",
async function ({ body }, res) {
const { org_name, avatar_url, description, email, password } = body;
const newOrgFromApi = await pool.query(
`INSERT INTO orgs (org_name,
avatar_url,
description,
email,
password) VALUES ("${org_name}", "${avatar_url}", "${description}", "${email}", "${password}");`
);
return { org_posted: "new org posted" };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//GET all orgs
api.get(
"/orgs",
async function () {
const rows = await pool.query("SELECT * FROM orgs;");
const result = Object.values(JSON.parse(JSON.stringify(rows[0])));
return { orgs: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
// GET all the opportunities by organisation name.
api.get(
"/opportunities/orgs/{opp_owner}",
async function ({ pathParams }) {
const params = pathParams["opp_owner"].replace(/%20/g, " ");
const rows = await pool.query(
`SELECT * FROM opportunities WHERE opp_owner= "${params}";`
);
const result = Object.values(JSON.parse(JSON.stringify(rows[0])));
return { opportunities: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//OPPORTUNITIES
//DELETE opportunities by id
api.delete(
"/opportunities/delete/{opp_id}",
async function ({ pathParams }) {
const { opp_id } = pathParams;
const deletedOpp = await pool.query(
`DELETE FROM opportunities WHERE opp_id= "${opp_id}"`
);
return { opportunity_deleted: "you deleted the opportunity" };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//GET opportunities by id
api.get(
"/opportunities/opportunity/{opp_id}",
async function ({ pathParams }) {
const { opp_id } = pathParams;
const rows = await pool.query(
`SELECT * FROM opportunities WHERE opp_id="${opp_id}";`
);
const result = Object.values(JSON.parse(JSON.stringify(rows[0])));
return { opportunity: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//GET opportunities by category
api.get(
"/opportunities/{category}",
async function ({ pathParams }) {
const params = pathParams["category"].replace(/%20/g, " ");
// const paramsReplaced = params.replaceAll("%20", " ");
console.log(params, "<<< params");
const rows = await pool.query(
`SELECT * FROM opportunities WHERE categories="${params}";`
);
const result = Object.values(JSON.parse(JSON.stringify(rows[0])));
return { opportunities: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//GET all opportunities
api.get(
"/opportunities",
async function ({ queryString }) {
let queryStr = "SELECT * FROM opportunities";
if (queryString.sort_by === "date_posted") {
queryStr += " ORDER BY date_posted";
} else if (queryString.sort_by === "opp_date") {
queryStr += " ORDER BY opp_date";
}
queryStr += ";";
const oppsFromApi = await pool.query(queryStr);
const result = Object.values(JSON.parse(JSON.stringify(oppsFromApi[0])));
return { opportunities: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//POST opportunities
api.post(
"/opportunities",
async function ({ body }, res) {
const { name, description, opp_date, dbs, drive, categories, opp_owner } =
body;
const newOppFromApi = await pool.query(
`INSERT INTO opportunities (name, description, opp_date, dbs, drive, categories, opp_owner) VALUES ("${name}", "${description}", "${opp_date}", "${dbs}", "${drive}", "${categories}", "${opp_owner}");`
);
return { opp_posted: "new opportunity posted" };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
// APPLICATIONS
//GET applications by username
api.post(
"/applications",
async function ({ body }, res) {
const { username, opp_id } = body;
const newAppFromApi = await pool.query(
`INSERT INTO applications (username, opp_id) VALUES ("${username}", "${opp_id}");`
);
return { application_posted: "new application submitted" };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
api.get(
"/applications/{username}",
async function ({ pathParams }) {
const params = pathParams.username;
const rows = await pool.query(
`SELECT * FROM opportunities RIGHT JOIN applications ON opportunities.opp_id = applications.opp_id WHERE applications.username = "${params}"; `
);
const result = Object.values(JSON.parse(JSON.stringify(rows[0])));
return { applications: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
// DELETE application by id
api.delete(
"/applications/delete/{app_id}",
async function ({ pathParams }) {
const { app_id } = pathParams;
const queryStr = format(`DELETE FROM applications WHERE app_id = %L`, [
app_id,
]);
const deletedApp = await pool.query(queryStr);
return { application_deleted: "you deleted the application" };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//UP TO HERE (BEN)
// GET applicants by opportunity id.
api.get(
"/applications/opportunities/{opp_id}",
async function ({ pathParams }) {
const { opp_id } = pathParams;
const queryStr = format(
`SELECT username, status, name, opp_date FROM applications RIGHT JOIN opportunities ON applications.opp_id = opportunities.opp_id WHERE applications.opp_id = %L;`,
[opp_id]
);
const appFromApi = await pool.query(queryStr);
const result = Object.values(JSON.parse(JSON.stringify(appFromApi[0])));
return { applicants: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
api.patch(
"/applications/{username}",
async function ({ body, pathParams }, res) {
const { username } = pathParams;
const { opp_id, newStatus } = body;
const queryStr = format(
`UPDATE applications SET status=%L WHERE username=%L AND opp_id=%L;`,
[newStatus],
[username],
[opp_id]
);
await pool.query(queryStr);
const queryStr2 = format(
`SELECT * FROM applications WHERE username = %L AND opp_id = %L;`,
[username],
[opp_id]
);
const updatedApp = await pool.query(queryStr2);
return { updated_user: updatedApp[0] };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
// CATEGORIES
//GET all categories
api.get(
"/categories",
async function () {
const categoriesFromApi = await pool.query("SELECT * FROM categories;");
const result = Object.values(
JSON.parse(JSON.stringify(categoriesFromApi[0]))
);
return { categories: result };
},
{ success: { contentType: "application/json" }, error: { code: 403 } }
);
//API
api.get("/", function () {
return {
msg: "Welcome to ChariT! Below are the endpoints available,",
"GET /categories": "Get all the available categories",
"GET /users": "Get all users (volunteers)",
"GET /users/:username": "Get a specific user by its username",
"GET /orgs": "Get all organisations",
"GET /opportunities":
"Get all the opportunities - can add ?sort_by=opp_date or ?sort_by=date_posted for closest opportunities or most recent posted",
"GET /opportunities/orgs/:opp_owner":
"Get the opportunities availables by organisation name",
"GET /opportunities/:category": "Get opportunities by its category",
"GET /applications/:username": "Get applications by username",
"GET /applications/opportunities/:opp_id":
"Get applicants by opportunity id",
"POST /users":
"allow to post a create a new user { username: username, avatar_url: avatar_url, firstname: firstname, lastname: lastname, dbs: yes/no, drive: yes/no, email: email, password: password }",
"POST /orgs":
"allow to post a create a organistation { org_name: name, avatar_url: avatar_url, description: description, email: email, password: password }",
"POST /opportunities":
"allow to post a new opportunity { name: event_name, description: description, opp_date: event_date, dbs: yes/no, drive: yes/no, categories : category, opp_owner: org_name }",
"POST /applications":
" allow users to apply for events { username: username, opp_id: opportunity_id }",
"PATCH /users/:username":
"Update the username and avatar_url by its username, it should be an object like { username: username, avatar_url:avatar:url }",
"DELETE /applications/delete/:opp_id":
"allow user to delete a specific application by its opportunity id",
"DELETE /opportunities/delete/:opp_id":
"allow organisation to delete a specific application by its opportunity id",
};
});
module.exports = api;