This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
412 lines (365 loc) · 12.2 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const express = require('express');
const Router = require('express-promise-router');
const path = require('path');
var bodyParser = require('body-parser');
const db = require('./db/index');
const app = express();
const cors = require('cors');
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client/build')));
// ########### DATAENTRY START ###########
app.post('/api/apis', async (req, res) => {
await db.query('DELETE FROM apis;');
const { apis } = req.body;
for (let i = 0; i < apis.length; i ++) {
await db.query('INSERT INTO apis(name, type) VALUES($1 ,$2)', [
apis[i][1],
apis[i][0]
]);
}
res.json("You successfully posted to apis");
});
app.get('/api/projects', async (req, res) => {
try {
const query = await db.query('SELECT * FROM projects;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
app.get('/api/apis', async (req, res) => {
try {
const query = await db.query('SELECT * FROM apis;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
app.post('/api/projects', async (req, res) => {
await db.query('DELETE from scores;');
await db.query('DELETE FROM projects;');
const { projectCSV } = req.body;
for (let i = 1; i < projectCSV.length; i++) {
const project = projectCSV[i];
await db.query('INSERT INTO projects(name, github, categories, projectId) VALUES($1 ,$2, $3, $4)', [
project['Submission Title'],
project['Submission Url'],
project['Categories'],
i
]);
}
res.json("You successfully posted to projects");
});
app.put('/api/projects', async (req, res) => {
const { projectNum, waveNum, tableNum, tablesCSV } = req.body;
if (tableNum * tablesCSV.length * waveNum < projectNum) {
console.log("error: not enough capacity");
} else {
// wave assignment
let w = 1;
for (let id = 1; id <= projectNum; id++) {
await db.query('UPDATE projects SET wave = $1 WHERE projectId = $2;', [
w,
id
]);
w++;
if (w > waveNum) {
w = 1;
}
}
// table assignment -- this is just based the asusmption that given a list of
// tables the projects should spread evenly amoung the tables
let t = 0;
for (let i = 1; i < projectNum; i++) {
await db.query('UPDATE projects SET tableName = $1 WHERE projectId = $2;', [
tablesCSV[t][0],
i
]);
t++;
if (t === tablesCSV.length) {
t = 0;
}
}
}
res.json("Successfully assigned waves and tables");
});
app.get('/api/project-tables-waves', async(req, res) => {
try {
const query = await db.query('SELECT name, wave, tableName FROM projects;');
res.send(query.rows);
} catch(error) {
console.log(error.stack);
}
})
// API endpoint for judge names for Judge Login
app.get('/api/judgenames', async (req, res) => {
try {
const query = await db.query('SELECT judgeId, name FROM judges;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
// endpoint to get scored projects in the Scoring Overview page
app.get('/api/scored/judge/:judgeId', async (req, res) => {
try {
const { judgeId } = req.params;
const query = await db.query('SELECT DISTINCT ON (projects.projectId) projects.projectId, projects.name, projects.categories, projects.github, projects.tableName, projects.wave, filtered.score FROM projects INNER JOIN (SELECT * FROM scores WHERE scores.judgeId = $1 AND scores.score IS NOT NULL) AS filtered ON projects.projectId=filtered.projectId;', [
judgeId
]);
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
// endpoint to get unscored projects in the Scoring Overview page
app.get('/api/unscored/judge/:judgeId', async (req, res) => {
try {
const { judgeId } = req.params;
const query = await db.query('SELECT DISTINCT ON (projects.projectId) projects.projectId, projects.name, projects.categories, projects.github, projects.tableName, projects.wave, filtered.score FROM projects INNER JOIN (SELECT * FROM scores WHERE scores.judgeId = $1 AND scores.score IS NULL) AS filtered ON projects.projectId=filtered.projectId;', [
judgeId
]);
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
// endpoint to select the categories to be scored for in each project info page
app.get('/api/categories/judge/:judgeId/project/:projectId', async (req, res) => {
try {
const { judgeId, projectId } = req.params;
const query = await db.query('SELECT category, score FROM scores WHERE judgeId = $1 AND projectId = $2;', [
judgeId,
projectId
]);
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
// updating project scores
app.put('/api/scoreupdate/judge/:judgeId/project/:projectId/category/:category', async (req, res) => {
const { judgeId, projectId, category } = req.params;
const { score } = req.body;
await db.query('UPDATE scores SET score = $1 WHERE judgeId = $2 AND projectId = $3 AND category = $4;', [
score,
judgeId,
projectId,
category
]);
res.json('Score update successfully');
});
// ########### DATAENTRY END ###########
// ########### JUDGEINFO START ###########
app.get('/api/judgeinfo', async (req, res) => {
try {
const query = await db.query('SELECT * FROM judges;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
app.post('/api/judgeinfo', async (req, res) => {
try {
const { info } = req.body;
if (info.length > 1) {
await db.query('INSERT INTO judges(name, API) VALUES($1, $2)', [
info[0],
info[1]
]);
}
res.json("Successfully posted ".concat(info[0]));
} catch (error) {
console.log(error.stack);
}
});
function getApiMapping(apisJSON, judgeJSON) {
/**
* @param {*} apisJSON
* @param {*} judgeJSON
*
* @returns an json that maps each api to another json objects with index and judges as fields
*/
let apiMappings = {};
var i;
for (i = 0; i < apisJSON.length; i += 1) {
const apiName = apisJSON[i].name;
apiMappings[apiName] = { index: 0, judges: [] };
}
apiMappings["General Category"] = {index: 0, judges: []};
var j;
for (j = 0; j < judgeJSON.length; j += 1) {
const api = judgeJSON[j]['api'];
apiMappings[api].judges = apiMappings[api].judges.concat(judgeJSON[j].judgeid);
}
return apiMappings;
}
// ########### JUDGEINFO END ###########
// API endpoint for judge names
app.get('/api/judgenames', async (req, res) => {
try {
const query = await db.query('SELECT name FROM judges;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
app.post('/api/assignjudges', async (req, res) => {
/**
* pseudo:
* fetch all judges and all projects as lists of JSONs
* sort the judges into a list all judges scoring each api and one for all general category judges
* loop through each project and find the next judge of each of this project's api (a pointer that traverses judge list)
* inserts assignments into scores table
*/
try {
await db.query('DELETE FROM scores;');
const { } = req.body;
const judges = await db.query('SELECT * FROM judges;');
const judgeJSON = judges.rows;
const projects = await db.query('SELECT * FROM projects;');
const projectsJSON = projects.rows;
const apis = await db.query('SELECT * FROM apis WHERE type=$1;', ['API']);
const apisJSON = apis.rows;
let apiMappings = getApiMapping(apisJSON, judgeJSON);
//now we match
let i;
/**
* looping through projects to match judges
*/
for (i = 0; i < projectsJSON.length; i += 1) {
const currProj = projectsJSON[i];
const categories = currProj.categories;
let hasGC = false;
let j;
for (j = 0; j < categories.length; j += 1) {
/**
* loops through the categories of each project
*/
let currCat = categories[j];
if (currCat.slice(0, 3) === "GC:") {
/**
* check if category is a general category
* assigns the category JSON key accordingly
*/
if (hasGC) {
var currCatKey = 'General Category'
let apiJudges = apiMappings[currCatKey].judges;
let apiIndex = (apiMappings[currCatKey].index - 1) % apiJudges.length;
if (apiIndex < 0) {
apiIndex = apiJudges.length - 1;
}
await db.query('INSERT INTO scores(judgeID, projectID, category) VALUES ($1, $2, $3)', [
apiJudges[apiIndex],
currProj.projectid,
currCat
]);
continue;
} else {
hasGC = true;
var currCatKey = 'General Category';
}
} else {
var currCatKey = currCat;
}
let apiJudges = apiMappings[currCatKey].judges;
let apiIndex = apiMappings[currCatKey].index;
apiMappings[currCatKey].index = (apiMappings[currCatKey].index + 1) % apiJudges.length;
await db.query('INSERT INTO scores(judgeID, projectID, category) VALUES ($1, $2, $3)', [
apiJudges[apiIndex],
currProj.projectid,
currCat
]);
}
}
res.json("Successfully assigned judges to projects.")
} catch (error) {
console.log(error.stack);
}
})
app.post('/api/deletejudge', async (req, res) => {
try {
const { deleted } = req.body;
if (deleted.length > 1) {
await db.query('DELETE FROM judges WHERE name=\'' + deleted[0] + '\' AND API=\'' + deleted[1] + '\';');
}
res.json("You successfully posted: ".concat(deleted));
} catch (error) {
console.log(error.stack)
}
})
app.get('/api/scores', async (req, res) => {
try {
const query = await db.query('SELECT * FROM scores;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
app.get('/api/projectscore/:id', async (req, res) => {
try {
const { id } = req.params;
const query = await db.query('SELECT * FROM scores WHERE projectID=' + id);
res.send(query.rows);
} catch (error) {
console.log(error.stack)
}
})
app.get('/api/categories', async (req, res) => {
try {
const query = await db.query('SELECT DISTINCT category FROM scores;');
res.send(query.rows);
} catch (error) {
console.log(error.stack);
}
});
app.get('/api/projectname/:id', async (req, res) => {
try {
const { id } = req.params;
const query = await db.query('SELECT * FROM projects WHERE projectID=' + id);
res.send(query.rows);
} catch (error) {
console.log(error.stack)
}
})
app.get('/api/judgename/:id', async (req, res) => {
try {
const { id } = req.params;
const query = await db.query('SELECT * FROM judges WHERE judgeId=' + id);
res.send(query.rows);
} catch (error) {
console.log(error.stack)
}
})
app.get('/api/winners', async (req, res) => {
try {
const apis = await db.query('SELECT DISTINCT category FROM scores ORDER BY category;');
const apisJSON = apis.rows;
const winnersJSON = {};
let currCat, query;
for (let i = 0; i < apisJSON.length; i += 1) {
currCat = apisJSON[i].category;
query = await db.query('SELECT * FROM scores WHERE category = $1 ORDER BY score DESC;', [currCat]);
projects = query.rows;
winnersJSON[currCat] = [];
let currID, nameQuery, name, currJudgeID, judgeNameQuery, judgeName;
for (let j = 0; j < projects.length; j += 1) {
currID = projects[j].projectid;
nameQuery = await db.query('SELECT name FROM projects WHERE projectId = $1', [currID]);
name = nameQuery.rows[0].name;
currJudgeID = projects[j].judgeid;
judgeNameQuery = await db.query('SELECT name FROM judges WHERE judgeId = $1', [currJudgeID]);
judgeName = judgeNameQuery.rows[0].name;
winnersJSON[currCat] = winnersJSON[currCat].concat([{ projectname: name, judgename: judgeName, score: projects[j].score }]);
}
}
res.send(winnersJSON);
} catch (error) {
console.log(error.stack);
}
});
app.get('*', (req, res) => res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html')));
const port = process.env.PORT || 5000;
app.listen(port);
console.log(`Database listening on ${port}`);