From fe3f1a5f5e79ad7cf167db70e988529f2fe7f976 Mon Sep 17 00:00:00 2001 From: ridhishjain Date: Tue, 15 Dec 2020 10:52:34 +0530 Subject: [PATCH] Update server/models/contests/updateContest.js Co-authored-by: NIKHIL BN <45630095+NBNARADHYA@users.noreply.github.com> --- server/models/contests/addGroup.js | 5 +- server/models/contests/createContest.js | 80 ++++++++++++++----------- server/models/contests/removeGroup.js | 3 +- server/models/contests/updateContest.js | 19 +++--- server/models/database/index.js | 11 +++- 5 files changed, 68 insertions(+), 50 deletions(-) diff --git a/server/models/contests/addGroup.js b/server/models/contests/addGroup.js index c013a63..f00ddf5 100644 --- a/server/models/contests/addGroup.js +++ b/server/models/contests/addGroup.js @@ -4,15 +4,16 @@ const { pool } = require('../database') * @param {*} param0 * @param {Object} param0.body * @param {Object} param0.params + * @param {String} param0.username * @return {Promise} */ -function addGroup({ params, body }) { +function addGroup({ params, body, username }) { return new Promise((resolve, reject) => { const { contest_id: contestId } = params const { group_id: groupId } = body pool.query( - `INSERT INTO contests_groups (contest_id, group_id) SELECT contest_id, ? FROM contest_moderators WHERE contest_id=? AND moderator=?`, + `INSERT INTO contests_groups (contest_id, group_id) SELECT contest_id, ? FROM contests_moderators WHERE contest_id=? AND moderator=?`, [groupId, contestId, username], (error, res) => { if (error || res === undefined) { diff --git a/server/models/contests/createContest.js b/server/models/contests/createContest.js index 5db2e87..61c7b7a 100644 --- a/server/models/contests/createContest.js +++ b/server/models/contests/createContest.js @@ -1,4 +1,4 @@ -const { pool } = require('../database') +const { connection } = require('../database') /** * @@ -25,41 +25,51 @@ function createContest({ username, body }) { let prizes = body.prizes || null let confidentialQuestions = body.confidential_questions || null let currentId - pool.query( - `INSERT INTO contests (creator, name, show_leaderboard, public, start_time, end_time, about, rules, prizes, confidential_questions) VALUES (?,?,?,?,?,?,?,?,?,?)`, - [ - username, - name, - showLeaderboard, - public, - startTime, - endTime, - about, - rules, - prizes, - confidentialQuestions, - ], - (error, results) => { - if (error || results === undefined) { - return reject(error) - } - const { insertId } = results - currentId = insertId - pool.query( - `INSERT INTO contests_moderators (contest_id, moderator) VALUES (?,?)`, - [currentId, username], - (error, res) => { - if (error || res === undefined) { - return reject(error) - } - return resolve({ - message: 'Contest created successfully', - contestId: currentId, - }) - } - ) + connection.beginTransaction((error) => { + if (error) { + return reject(error) } - ) + connection.query( + `INSERT INTO contests (creator, name, show_leaderboard, public, start_time, end_time, about, rules, prizes, confidential_questions) VALUES (?,?,?,?,?,?,?,?,?,?)`, + [ + username, + name, + showLeaderboard, + public, + startTime, + endTime, + about, + rules, + prizes, + confidentialQuestions, + ], + (error, results) => { + if (error || results === undefined) { + return reject(error) + } + const { insertId } = results + currentId = insertId + connection.query( + `INSERT INTO contests_moderators (contest_id, moderator) VALUES (?,?)`, + [currentId, username], + (error, insertionResults) => { + if (error || insertionResults === undefined) { + return reject(error) + } + connection.commit((error) => { + if (error) { + return reject(error) + } + return resolve({ + message: 'Contest created successfully', + contestId: currentId, + }) + }) + } + ) + } + ) + }) }) } diff --git a/server/models/contests/removeGroup.js b/server/models/contests/removeGroup.js index 3c121f9..8242e64 100644 --- a/server/models/contests/removeGroup.js +++ b/server/models/contests/removeGroup.js @@ -4,10 +4,11 @@ const { pool } = require('../database') * @param {*} param0 * @param {Object} param0.body * @param {Object} param0.params + * @param {String} param0.username * @return {Promise} */ -function removeGroup({ params, body }) { +function removeGroup({ params, body, username }) { return new Promise((resolve, reject) => { const { contest_id: contestId } = params const { group_id: groupId } = body diff --git a/server/models/contests/updateContest.js b/server/models/contests/updateContest.js index 9737121..2883c9d 100644 --- a/server/models/contests/updateContest.js +++ b/server/models/contests/updateContest.js @@ -5,25 +5,24 @@ const { pool } = require('../database') * @param {*} param0 * @param {Object} param0.body * @param {Object} param0.params + * @param {String} param0.username * @return {Promise} * */ -function updateContest({ body, params }) { +function updateContest({ body, params, username }) { return new Promise((resolve, reject) => { const { name, show_leaderboard: showLeaderboard, public, - start_time, - end_time, + start_time: startTime, + end_time: endTime, about, rules, prizes, confidential_questions: confidentialQuestions, } = body - const startTime = new Date(start_time) - const endTime = new Date(end_time) const { contest_id: contestId } = params let query = `UPDATE contests SET ` let arr = [] @@ -39,13 +38,13 @@ function updateContest({ body, params }) { query += `public=?,` arr.push(public) } - if (startTime) { + if (startTime && !isNaN(Date.parse(startTime))) { query += `start_time=?,` - arr.push(startTime) + arr.push(new Date(startTime)) } - if (endTime) { + if (endTime && !isNaN(Date.parse(endTime))) { query += `end_time=?,` - arr.push(endTime) + arr.push(new Date(endTime)) } if (about) { query += `about=?,` @@ -70,7 +69,7 @@ function updateContest({ body, params }) { return reject('Contest is already upto date') } query += ` WHERE (SELECT COUNT(id) FROM contests_moderators WHERE contest_id=? AND moderator=? )AND id=?` - arr.push(contestId) + arr.push(contestId, username, contestId) pool.query(query, arr, (error, res) => { if (error || res === undefined) { return reject(error) diff --git a/server/models/database/index.js b/server/models/database/index.js index c101f5c..9b4e5bb 100644 --- a/server/models/database/index.js +++ b/server/models/database/index.js @@ -1,5 +1,5 @@ const dotenv = require('dotenv') -const { createPool } = require('mysql') +const { createPool, createConnection } = require('mysql') const config = dotenv.config() if (!config) { @@ -19,4 +19,11 @@ const pool = createPool({ database: database, }) -module.exports = { pool } +const connection = createConnection({ + host: host, + user: 'root', + password: password, + database: database, +}) + +module.exports = { pool, connection }