-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
162 lines (142 loc) · 4.37 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const express = require('express');
const app = express();
const fs = require('fs');
const port = 5000;
const mongoose = require('mongoose');
const cors = require('cors');
const { get_remaining_time } = require('./server/time/time');
/////////////////////////////
// mlab related
const { username, password } = require('./credentials/mlab.js');
const url = `mongodb://${username}:${password}@ds231529.mlab.com:31529/coding-platform`
mongoose.connect(url);
const schemas = require('./schemas/Schemas');
const { Contest, question } = schemas;
//////////////////////////////////////////
app.use(cors());
/////////////////////////////
// body - parser related.
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api/auth', require('./server/auth/auth'));
//////////////////////////////
// utils.
const solved_choices = [
'full',
'no',
'partial'
];
const getRandomFrom = (arr) => {
let len = arr.length;
return arr[Math.floor(Math.random()*len)];
}
//////////////
const add_question_to_a_contest = (contest_id, question) => {
Contest.findById(contest_id, {$push:{questions: question}},
(err, data) => {
if(err) throw err;
console.log(data, 'question added.');
})
}
const add_contest = (title, type, details, additionalDetails) => {
new Contest({
title,
type,
details,
additionalDetails,
questions: []
}).save()
.then((data) => console.log(data, 'added contest successfully'))
.catch((err) => console.log(err));
}
const sum = (arr) => {
let tot = 0;
for(let i=0;i<arr.length;++i)
tot += arr[i];
return tot;
}
app.get('/api/contests', (req, res) => {
Contest.find({}, 'title type details additionalDetails start_time end_time')
.then(data => {
for(let i=0;i<data.length;++i) {
let time = get_remaining_time(data[i].start_time, data[i].end_time);
delete data[i].start_time;
delete data[i].end_time;
let tot = sum(time);
if(tot == 0)
data[i].type = 'mock';
else if(tot < 0)
data[i].type = 'future';
else
data[i].type = 'ongoing';
}
res.status(200).json({data: data, error: null})
})
.catch(err => res.status(404).json({error: err, data: null}));
});
const get_questions = (contest_id, requirments_arr) => {
return new Promise((resolve, reject) => {
Contest.findById(contest_id, requirments_arr.join(' '), (err, data) => {
if(err)
reject(err);
else {
resolve(data);
}
})
});
}
app.get('/api/contests/:contest_id/questions', (req, res) => {
get_questions(req.params.contest_id,
['title' ,'start_time' ,'end_time' ,'questions'])
.then(data => {
let resp = {};
resp['contestName'] = data.title;
resp['timeRemaining'] = get_remaining_time(data.start_time, data.end_time);
resp['questions'] = [];
if(data.questions.length == 0) {
res.status(200).json({data: resp, error: null});
}
else {
resp['questions'] = data.questions.map((question) => {
return {
question_id: question._id,
title: question.title,
details: question.details,
solved: getRandomFrom(solved_choices),
solvedBy: question.solvedBy,
difficulty: question.difficulty
}
});
res.status(200).json({data: resp, error: null});
}
})
.catch(err => res.status(404).json({data: null, error: 'error'}));
});
const purify = (question) => {
question.test_cases.private = undefined;
question.test_case_results.private = undefined;
question.test_cases = question.test_cases.public;
question.test_case_results = question.test_case_results.public;
return question;
}
app.get('/api/contests/:contest_id/questions/:question_id', (req, res) => {
let qid = req.params.question_id;
get_questions(req.params.contest_id, ['questions'])
.then(
(data) => {
let questions = data.questions, i;
for(i=0;i<questions.length;++i)
if(questions[i]._id == qid) {
res.status(200).json({data: purify(questions[i]), error: null});
break;
}
if(i == questions.length)
res.status(404).json({data: null, error: 'no such question'});
}
)
.catch(err => res.status(404).json({data: null, error: 'error!!'}));
});
app.listen(port, () => {
console.log('listening to port: ' + port);
})