| 135 | |
| 136 | // Serve the index page, showing vote tallies. |
| 137 | const httpGet = async (req, res) => { |
| 138 | try { |
| 139 | // Get the 5 most recent votes. |
| 140 | const recentVotesQuery = pool |
| 141 | .request() |
| 142 | .query( |
| 143 | 'SELECT TOP(5) candidate, time_cast FROM votes ORDER BY time_cast DESC' |
| 144 | ); |
| 145 | |
| 146 | // Get votes |
| 147 | const stmt = |
| 148 | 'SELECT COUNT(vote_id) as count FROM votes WHERE candidate=@candidate'; |
| 149 | |
| 150 | const tabsQuery = pool |
| 151 | .request() |
| 152 | .input('candidate', mssql.VarChar(6), 'TABS') |
| 153 | .query(stmt); |
| 154 | |
| 155 | const spacesQuery = pool |
| 156 | .request() |
| 157 | .input('candidate', mssql.VarChar(6), 'SPACES') |
| 158 | .query(stmt); |
| 159 | |
| 160 | // Run queries concurrently, and wait for them to complete |
| 161 | // This is faster than await-ing each query object as it is created |
| 162 | |
| 163 | const recentVotes = await recentVotesQuery; |
| 164 | const tabsVotes = await tabsQuery; |
| 165 | const spacesVotes = await spacesQuery; |
| 166 | |
| 167 | res.render('index.pug', { |
| 168 | recentVotes: recentVotes.recordset, |
| 169 | tabCount: tabsVotes.recordset[0].count, |
| 170 | spaceCount: spacesVotes.recordset[0].count, |
| 171 | }); |
| 172 | } catch (err) { |
| 173 | logger.error(err); |
| 174 | res |
| 175 | .status(500) |
| 176 | .send( |
| 177 | 'Unable to load page. Please check the application logs for more details.' |
| 178 | ) |
| 179 | .end(); |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | app.get('/', httpGet); |
| 184 | |