Skip to content

Commit

Permalink
Update server/models/contests/updateContest.js
Browse files Browse the repository at this point in the history
Co-authored-by: NIKHIL BN <[email protected]>
  • Loading branch information
ridhishjain and NBNARADHYA committed Dec 15, 2020
1 parent f39bd98 commit fe3f1a5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 50 deletions.
5 changes: 3 additions & 2 deletions server/models/contests/addGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
80 changes: 45 additions & 35 deletions server/models/contests/createContest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { pool } = require('../database')
const { connection } = require('../database')

/**
*
Expand All @@ -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,
})
})
}
)
}
)
})
})
}

Expand Down
3 changes: 2 additions & 1 deletion server/models/contests/removeGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions server/models/contests/updateContest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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=?,`
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions server/models/database/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const dotenv = require('dotenv')
const { createPool } = require('mysql')
const { createPool, createConnection } = require('mysql')

const config = dotenv.config()
if (!config) {
Expand All @@ -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 }

0 comments on commit fe3f1a5

Please sign in to comment.