Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9 admin part of contest #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions backend/Models/contestadmin/checkAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { pool } = require('../db');

/**
*
* @param {String} username
* @return {Promise}
*/
/*function checkadmin(username) {
return new Promise((resolve, reject) => {
pool.query(
`SELECT isadmin FROM users WHERE username=?`,
[username],
(error, results) => {
if (results[0].isadmin==0) {
return reject('Not an admin');
}
return response(results);
}
);
});
}
*/

function checkadmin(username) {
pool.query(
`SELECT isadmin FROM users WHERE username=?`,
[username],
(error, results) => {
if (error) return false;
if (results[0].isadmin == 0) {
return false;
}
return true;
}
);
}
module.exports = checkadmin;
91 changes: 91 additions & 0 deletions backend/Models/contestadmin/createNewContest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const { pool } = require('../db');

function createNewContest({
username,
title,
about,
rules,
prize,
start_time: startTime,
end_time: endTime,
group_id: groupId,
confidential
}) {
return new Promise((resolve, reject) => {
pool.getConnection(function(err, connection) {
if (err) return reject(err);
connection.beginTransaction(function(err) {
if (err) return reject(err);
connection.query(
'SELECT is_admin FROM users WHERE username=?',
[username],
(error, results) => {
if (error) {
return connection.rollback(function() {
connection.release();
return reject(error);
});
}
if (results[0].is_admin == 0) {
return reject('You are not allowed to create a contest');
}
connection.query(
'INSERT INTO contests (username,title,about,rules,prize,start_time,end_time,group_id,confidential) VALUES (?,?,?,?,?,?,?,?,?)',
[
username,
title,
about,
rules,
prize,
startTime,
endTime,
groupId,
confidential
],
(error, results) => {
if (error) {
return connection.rollback(function() {
connection.release();
return reject(error);
});
}
var id = results.id;
connection.query(
'INSERT INTO admin_of_contest (admin_name,contest_id,is_admin) VALUES (?,?,?)',
[username, id, 1],
error => {
if (error) {
return connection.rollback(function() {
connection.release();
return reject(error);
});
}
connection.commit(function(err) {
if (err) {
return connection.rollback(function() {
connection.release();
return reject(error);
});
}

connection.release();
if (err) {
return connection.rollback(function() {
connection.release();
return reject(error);
});
}
return resolve('Succesfully added a new contest');
});
}
);
}
);
}
);
});
});
});
}

module.exports = createNewContest;
47 changes: 47 additions & 0 deletions backend/Models/contestadmin/createNewQuestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { pool } = require('../db');

function createNewQuestion({
username,
question_name: questionName,
contest_id: contestId,
type,
problemstatement,
solution,
max_score: maxScore,
negative,
partial,
difficulty,
answer
}) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO questions (username,question_name,contest_id,type,problem_statement,solution,maxscore,negative,partial,difficulty,answer) VALUES (?,?,?,?,?,?,?,?,?,?,?) WHERE (SELECT is_admin from admin_of_contest WHERE admin_name = ? AND contest_id = ?)',
[
username,
questionName,
contestId,
type,
problemstatement,
solution,
maxScore,
negative,
partial,
difficulty,
answer,
username,
contestId
],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You are not an admin');
}
return resolve('Successfully deleted');
}
);
});
}

module.exports = createNewQuestion;
21 changes: 21 additions & 0 deletions backend/Models/contestadmin/deleteAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { pool } = require('../db');

function deleteAdmin({ username, contest_id: contestId, adminname }) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE admin_of_contest SET admin_of_contest.delete_status=1 WHERE (SELECT is_admin FROM admin_of_contest WHERE admin_name =? AND contest_id = ?) AND admin_name = ? AND contest_id = ? ',
[username, contestId, adminname, contestId],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You can not delete an admin');
}
return resolve('Successfully deleted');
}
);
});
}

module.exports = deleteAdmin;
21 changes: 21 additions & 0 deletions backend/Models/contestadmin/deleteContest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { pool } = require('../db');

function deleteContest({ username, contest_id: contestId }) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE contests,admin_of_contest,questions SET contests.delete_status=1 AND admin_of_contest.delete_status=1 AND questions.delete_status=1 WHERE (SELECT is_admin FROM admin_of_contest WHERE admin_name =? AND contest_id = ?) ',
[username, contestId],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You are not an admin');
}
return resolve('Successfully deleted');
}
);
});
}

module.exports = deleteContest;
25 changes: 25 additions & 0 deletions backend/Models/contestadmin/deleteQuestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { pool } = require('../db');

function deleteQuestion({
username,
contest_id: contestId,
question_id: questionId
}) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE questions SET questions.delete_status=1 WHERE (SELECT is_admin FROM admin_of_contest WHERE admin_name =? AND contest_id = ?) AND id = ? ',
[username, contestId, questionId],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You are not an admin');
}
return resolve('Successfully deleted');
}
);
});
}

module.exports = deleteQuestion;
21 changes: 21 additions & 0 deletions backend/Models/contestadmin/editAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { pool } = require('../db');

function editAdmin({ username, contest_id: contestId, adminname, newvalue }) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE admin_of_contest SET admin_of_contest.is_admin=? WHERE (SELECT is_admin FROM admin_of_contest WHERE admin_name =? AND contest_id = ?) AND admin_name = ? AND contest_id = ? ',
[newvalue, username, contestId, adminname, contestId],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You cannot edit an admin');
}
return resolve('Successfully deleted');
}
);
});
}

module.exports = editAdmin;
44 changes: 44 additions & 0 deletions backend/Models/contestadmin/editContest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { pool } = require('../db');

function editContest({
username,
contest_id: contestId,
title,
about,
rules,
prize,
start_time,
end_time,
group_id: groupId,
confidential
}) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE contests SET title=?,about=?,rules=?,prize=?,start_time=?,end_time=?,group_id=?,confidential=? WHERE (SELECT is_admin FROM admin_of_contest WHERE admin_name =? AND contest_id = ?) AND id = ?',
[
title,
about,
rules,
prize,
start_time,
end_time,
groupId,
confidential,
username,
contestId,
contestId
],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You are not an admin');
}
return resolve('Successfully edited');
}
);
});
}

module.exports = editContest;
47 changes: 47 additions & 0 deletions backend/Models/contestadmin/editQuestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { pool } = require('../db');

function editQuestion({
username,
contest_id: contestId,
question_id: questionId,
question_name: questionName,
type,
problemstatement,
solution,
maxscore,
negative,
partial,
difficulty,
answer
}) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE questions SET type = ?,question_name=?,problem_statement = ?,solution = ?,max_score = ?,negative = ?,partial = ?,difficulty = ?,answer=? WHERE (SELECT is_admin FROM admin_of_contest WHERE admin_name = ? AND contest_id = ?) AND id = ?',
[
type,
questionName,
problemstatement,
solution,
maxscore,
negative,
partial,
difficulty,
answer,
username,
contestId,
questionId
],
(error, results) => {
if (error) {
return reject(error);
}
if (results.affectedRows == 0) {
return reject('You are not an admin');
}
return resolve('Successfully deleted');
}
);
});
}

module.exports = editQuestion;
31 changes: 31 additions & 0 deletions backend/Models/contestadmin/getAllContests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { pool } = require('../db');

function getAllContests(username) {
return new Promise((resolve, reject) => {
pool.query(
`SELECT contest_id FROM admin_of_contest WHERE admin_name=?`,
[username],
(error, results) => {
if (error) {
return reject(error);
}
if (!results.length) {
return reject('No contests');
}
let contestIdArr = results.map(result => result.contest_id);
pool.query(
`SELECT * FROM contests WHERE id IN (?)`,
[contestIdArr],
(error, resultss) => {
if (error) {
return reject(error);
}
return resolve(resultss);
}
);
}
);
});
}

module.exports = getAllContests;
Loading