| 156 | |
| 157 | // Handle incoming vote requests and inserting them into the database. |
| 158 | const httpPost = async (req, res) => { |
| 159 | const {team} = req.body; |
| 160 | const timestamp = new Date(); |
| 161 | |
| 162 | if (!team || (team !== 'TABS' && team !== 'SPACES')) { |
| 163 | return res.status(400).send('Invalid team specified.').end(); |
| 164 | } |
| 165 | |
| 166 | try { |
| 167 | const stmt = |
| 168 | 'INSERT INTO votes (time_cast, candidate) VALUES (@timestamp, @team)'; |
| 169 | const {DateTime, NVarChar} = connection.TYPES; |
| 170 | // Runs query |
| 171 | await connection.query(stmt, [ |
| 172 | ['timestamp', DateTime, timestamp], |
| 173 | ['team', NVarChar, team], |
| 174 | ]); |
| 175 | } catch (err) { |
| 176 | // If something goes wrong, handle the error in this section. This might |
| 177 | // involve retrying or adjusting parameters depending on the situation. |
| 178 | logger.error(err); |
| 179 | return res |
| 180 | .status(500) |
| 181 | .send( |
| 182 | 'Unable to successfully cast vote! Please check the application logs for more details.' |
| 183 | ) |
| 184 | .end(); |
| 185 | } |
| 186 | |
| 187 | res.status(200).send(`Successfully voted for ${team} at ${timestamp}`).end(); |
| 188 | }; |
| 189 | |
| 190 | app.post('*', httpPost); |
| 191 | |