| 117 | |
| 118 | // Serve the index page, showing vote tallies. |
| 119 | const httpGet = async (req, res) => { |
| 120 | try { |
| 121 | // Get the 5 most recent votes. |
| 122 | const recentVotes = await connection.query( |
| 123 | 'SELECT TOP(5) candidate, time_cast FROM votes ORDER BY time_cast DESC' |
| 124 | ); |
| 125 | |
| 126 | const {NVarChar} = connection.TYPES; |
| 127 | |
| 128 | // Get votes |
| 129 | const stmt = |
| 130 | 'SELECT COUNT(vote_id) as count FROM votes WHERE candidate=@candidate'; |
| 131 | const [tabsVotes] = await connection.query(stmt, [ |
| 132 | ['candidate', NVarChar, 'TABS'], |
| 133 | ]); |
| 134 | const [spacesVotes] = await connection.query(stmt, [ |
| 135 | ['candidate', NVarChar, 'SPACES'], |
| 136 | ]); |
| 137 | |
| 138 | // failing: no recordset on undefined |
| 139 | res.render('index.pug', { |
| 140 | recentVotes: recentVotes || [], |
| 141 | tabCount: tabsVotes.count.value, |
| 142 | spaceCount: spacesVotes.count.value, |
| 143 | }); |
| 144 | } catch (err) { |
| 145 | logger.error(err); |
| 146 | res |
| 147 | .status(500) |
| 148 | .send( |
| 149 | 'Unable to load page. Please check the application logs for more details.' |
| 150 | ) |
| 151 | .end(); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | app.get('/', httpGet); |
| 156 | |