-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
228 lines (205 loc) · 8.34 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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
import pg from "pg";
import bcrypt from "bcrypt";
const port = 3000;
const app = express();
const saltRounds = 10;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
let currentUserEmail = null;
const tableName = "problems";
const userTable = "users";
const db = new pg.Client({
user:"postgres",
password:"123456",
host:"localhost",
database:"cfproblems",
port:5432,
});
db.connect();
app.get("/", (req, res) => {
res.render("login.ejs")
})
app.get("/register", (req, res) => {
res.render("register.ejs")
})
app.post("/login", async (req, res) => {
let email = req.body.email;
let password = req.body.password;
const result = await db.query(`select * from ${userTable} where email = '${email}'`);
if(result.rows.length == 0){
let errorMessage = "User has not been registered yet. Please register."
res.render("register.ejs", {message : errorMessage})
} else {
const userCredentials = result.rows[0];
bcrypt.compare(password, userCredentials["password"], (err, result) => {
if(err){
console.error("Failed to check if password was correct", err.stack);
res.status(500).send("Error occurred while authenticating credentials");
} else {
if(result){
currentUserEmail = email;
res.redirect("/dashboard");
}
else{
let errorMessage = "Incorrect password. Try again."
res.render("login.ejs", {message : errorMessage})
}
}
})
}
})
app.post("/register",async (req, res) => {
let email = req.body.email;
let password = req.body.password;
bcrypt.hash(password, saltRounds, async (err, hash) => {
if(err){
console.error("Failed to upload user credentials", err.stack);
res.status(500).send("Error occurred while salting");
} else {
try {
const result = await db.query(`SELECT * from ${userTable} where email = '${email}'`);
if(result.rows.length > 0){
let errorMessage = "User with this email already exists. Try logging in.";
res.render("register.ejs", {message : errorMessage})
} else {
password = hash;
await db.query(`INSERT INTO ${userTable} (email, \"password\") VALUES ($1, $2)`, [email, password]);
res.redirect("/dashboard")
}
} catch (error) {
console.error("Failed to upload user credentials", error.stack);
res.status(500).send("Error occurred while authenticating");
}
}
})
})
app.get("/dashboard", async (req, res) => {
console.log(currentUserEmail)
try {
const result = await db.query(`SELECT * from (select * from ${tableName} where email = '${currentUserEmail}' ORDER BY \"sno\" desc LIMIT 10) sub ORDER BY \"sno\" ASC`);
const ListProblems = result.rows;
res.render("dashboard.ejs", { problemsData: ListProblems, currentUser: currentUserEmail });
} catch (err) {
console.error("Failed to make query", err.stack);
res.status(500).send("Error occurred while fetching problems");
}
});
app.get("/upload", (req, res) => {
res.render("problemupload.ejs")
})
app.get('/problem/:id', async (req, res) => {
try {
const result = await db.query(`SELECT * from ${tableName} where submissionid = '${req.params.id}' and email = '${currentUserEmail}'`)
res.render("problemcard.ejs", {problemsData : result.rows[0]})
} catch(err) {
console.error("failed to make query", err.stack)
res.status(500).send("Error occurred while fetching problems");
}
})
app.post("/submit", async (req, res) => {
const uploadedProblemID = req.body.subCode.trim();
const handle = req.body.currentHandle;
const comment = req.body.comments;
let uploadedProblem = "";
// console.log(uploadedProblemID)
const CF_request = await axios.get(`https://codeforces.com/api/user.status?handle=${handle}&from=1&count=100000`);
const recentSubmissions = CF_request.data.result;
for(let i = 0; i < recentSubmissions.length; i++){
if(recentSubmissions[i].id == uploadedProblemID){
uploadedProblem = recentSubmissions[i];
break;
}
}
const result = await db.query(`select * from problems where email = '${currentUserEmail}' and submissionid = '${uploadedProblemID}'`);
console.log(result.rows);
if(result.rows.length > 0){
let errorMessage = "Problem already uploaded.";
res.render("problemupload.ejs", {message : errorMessage})
} else {
if(uploadedProblem === ""){
let errorMessage = "Could not find submission in user history.";
res.render("problemupload.ejs", {message : errorMessage})
}
else{
let dbArray = [handle, currentUserEmail, uploadedProblem.verdict ,uploadedProblem.problem.contestId + uploadedProblem.problem.index, uploadedProblem.problem.name, comment, (uploadedProblem.problem.rating ? uploadedProblem.problem.rating : "unknown")];
let problemTags = uploadedProblem.problem.tags.map((tag) => {
return '"' + tag + '"'
});
let sizeArray = ["$1", "$2", "$3", "$4", "$5", "$6", "$7"]
for(let i = 0 ; i < uploadedProblem.problem.tags.length + 1; i++){
if(i < uploadedProblem.problem.tags.length){
dbArray.push('t');
}
sizeArray.push(`$${i+8}`);
}
dbArray.push(uploadedProblemID);
let text = `INSERT INTO ${tableName} ("user", email , verdict, problemid, problemname, comment, rating, ` + problemTags.toString() + ",\"submissionid\") VALUES (" + sizeArray.toString() + ")" ;
db.query(text, dbArray)
res.redirect("/upload");
}}
});
app.post("/tag", async (req, res) => {
let tag = 0;
if("tagselect" in req.body)
tag = req.body["tagselect"];
let lowerBound = req.body["rating-lower"];
let upperBound = req.body["rating-upper"];
let verdictFilter = req.body["verdict-select"];
let needFavs = 0;
if("wantFav" in req.body){
needFavs = 1;
}
if(lowerBound == '')lowerBound = 0;
if(upperBound == '')upperBound = 4000;
try {
let dbRequest = `SELECT * FROM ${tableName} where rating >= ${lowerBound} AND rating <= ${upperBound} and email='${currentUserEmail}' `;
if(verdictFilter !== "Any")
dbRequest += `AND verdict = '${verdictFilter}' `;
if(needFavs)
dbRequest += `AND favorite = true `
if("tagselect" in req.body){
dbRequest += `AND `;
if(typeof(tag) == "string")
dbRequest += "\"" + tag + "\" = 't' ";
else{
for(let i = 0; i < tag.length; i++){
dbRequest += "\"" + tag[i] + "\" = 't' ";
if(i < tag.length - 1)
dbRequest += "AND ";
}
}
}
dbRequest.trim();
console.log(dbRequest)
const result = await db.query(dbRequest);
const ListProblems = result.rows;
res.render("dashboard.ejs", { problemsData: ListProblems, currentUser : currentUserEmail });
} catch (err) {
console.error("Failed to make query", err.stack);
res.status(500).send("Error occurred while fetching problems");
}
})
app.post("/fav", async (req, res) => {
let subID = req.body["problemID"];
let change = req.body["Fav"];
try {
if(change === '1')
await db.query(`update ${tableName} set favorite = 't' where submissionid = '${subID}' and email = '${currentUserEmail}';`)
else
await db.query(`update ${tableName} set favorite = 'f' where submissionid = '${subID}' and email = '${currentUserEmail}';`)
res.redirect("/dashboard")
} catch(err) {
console.error("failed to make query", err.stack)
res.status(500).send("Error occurred while favoriting");
}
})
app.listen(port, () => {
console.log(`Listening on port ${port}`);
})
app.get("/logout", (req, res) => {
currentUserEmail = null;
res.redirect("/");
})