-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from bounswe/backend/development
Backend/development
- Loading branch information
Showing
10 changed files
with
872 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
const mysql = require('mysql2') | ||
|
||
require('dotenv').config(); | ||
|
||
const pool = mysql.createPool({ | ||
host: process.env.MYSQL_HOST, | ||
user: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD, | ||
database: process.env.MYSQL_DATABASE | ||
}).promise() | ||
|
||
|
||
async function getDiscretePolls(){ | ||
const sql = 'SELECT * FROM discrete_polls'; | ||
|
||
try { | ||
const [rows, fields] = await pool.query(sql); | ||
return rows | ||
} catch (error) { | ||
console.error('getDiscretePolls(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function getContinuousPolls(){ | ||
const sql = 'SELECT * FROM continuous_polls'; | ||
|
||
try { | ||
const [rows, fields] = await pool.query(sql); | ||
return rows | ||
} catch (error) { | ||
console.error('getDiscretePolls(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function getDiscretePollWithId(pollId){ | ||
const sql = 'SELECT * FROM discrete_polls WHERE id = ?'; | ||
|
||
try { | ||
const [rows, fields] = await pool.query(sql, [pollId]); | ||
return rows; | ||
} catch (error) { | ||
console.error('getDiscretePollWithId(): Database Error'); | ||
throw error; | ||
} | ||
|
||
} | ||
|
||
async function getContinuousPollWithId(pollId){ | ||
const sql = 'SELECT * FROM continuous_polls WHERE id = ?'; | ||
|
||
try { | ||
const [rows, fields] = await pool.query(sql, [pollId]); | ||
return rows; | ||
} catch (error) { | ||
console.error('getContinuousPollWithId(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function addDiscretePoll(question, choices){ | ||
const sql_poll = 'INSERT INTO discrete_polls (question) VALUES (?)'; | ||
const sql_choice = 'INSERT INTO discrete_poll_choices (choice_text, poll_id) VALUES (?, ?)'; | ||
|
||
try { | ||
const [resultSetHeader] = await pool.query(sql_poll, [question]); | ||
poll_id = resultSetHeader.insertId; | ||
if (!poll_id) { | ||
return false; | ||
} | ||
await Promise.all(choices.map(choice => { | ||
return pool.query(sql_choice, [choice, poll_id]); | ||
})) | ||
return true; | ||
} catch (error) { | ||
console.error('addDiscretePoll(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function addContinuousPoll(question, min, max){ | ||
const sql_poll = 'INSERT INTO continuous_polls (question, min_value, max_value) VALUES (?, ?, ?)'; | ||
|
||
try { | ||
const [resultSetHeader] = await pool.query(sql_poll, [question, min, max]); | ||
poll_id = resultSetHeader.insertId; | ||
if (!poll_id) { | ||
return false; | ||
} | ||
return true; | ||
} catch (error) { | ||
console.error('addContinuousPoll(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function getDiscretePollChoices(pollid) { | ||
const sql = "SELECT * FROM discrete_poll_choices WHERE poll_id = ?"; | ||
|
||
try { | ||
[rows] = await pool.query(sql, [pollid]); | ||
return rows; | ||
} catch (error) { | ||
console.error('getPollChoices(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function getDiscreteVoteCount(choiceid) { | ||
const sql = "SELECT COUNT(*) AS 'count' FROM discrete_polls_selections WHERE choice_id = ?"; | ||
|
||
try { | ||
[result] = await pool.query(sql, [choiceid]); | ||
return result[0].count; | ||
} catch (error) { | ||
console.error('getPollChoices(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
async function voteDiscretePoll(pollId, userId, choiceId){ | ||
const deleteExistingSql = "DELETE FROM discrete_polls_selections WHERE poll_id = ? AND user_id = ?" | ||
const addVoteSql = "INSERT INTO discrete_polls_selections (poll_id, choice_id, user_id) VALUES (?, ?, ?)" | ||
|
||
try { | ||
// TODO: Consider making this a transaction, commit | ||
deleteResult = await pool.query(deleteExistingSql, [pollId, userId]); | ||
addResult = await pool.query(addVoteSql, [pollId, choiceId, userId]); | ||
} catch (error) { | ||
console.error('voteDiscretePoll(): Database Error'); | ||
throw error; | ||
} | ||
|
||
} | ||
|
||
async function voteContinuousPoll(pollId, userId, choice){ | ||
const deleteExistingSql = "DELETE FROM continuous_poll_selections WHERE poll_id = ? AND user_id = ?" | ||
const addVoteSql = "INSERT INTO continuous_poll_selections (poll_id, user_id, selected_value) VALUES (?, ?, ?)" | ||
|
||
try { | ||
// TODO: Consider making this a transaction, commit | ||
deleteResult = await pool.query(deleteExistingSql, [pollId, userId]); | ||
addResult = await pool.query(addVoteSql, [pollId, userId, choice]); | ||
} catch (error) { | ||
console.error('voteContinuousPoll(): Database Error'); | ||
throw error; | ||
} | ||
|
||
} | ||
|
||
async function getContinuousPollVotes(pollId) { | ||
const sql = "SELECT selected_value FROM continuous_poll_selections"; | ||
|
||
try { | ||
[rows, fields] = await pool.query(sql); | ||
return rows; | ||
} catch (error) { | ||
console.error('getContinuousPollVotes(): Database Error'); | ||
throw error; | ||
} | ||
} | ||
|
||
|
||
|
||
module.exports = {getDiscretePolls, getContinuousPolls, getDiscretePollWithId, getContinuousPollWithId, | ||
addDiscretePoll,addContinuousPoll, getDiscretePollChoices, getDiscreteVoteCount, voteDiscretePoll, voteContinuousPoll, | ||
getContinuousPollVotes} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.