From 76d9ad836de7fe40d637b4de77141c19844f166e Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Thu, 4 Nov 2021 21:33:42 +0100 Subject: [PATCH] Implemented a way to easily respond render/json. --- api/api.js | 26 -------- api/finishes.js | 59 ------------------ api/graph.js | 66 -------------------- api/maps.js | 115 ----------------------------------- api/players.js | 57 ----------------- index.js | 3 +- libs/routex.js | 10 +++ routes/routes.js | 5 +- views/components/_navbar.njk | 6 +- 9 files changed, 18 insertions(+), 329 deletions(-) delete mode 100644 api/api.js delete mode 100644 api/finishes.js delete mode 100644 api/graph.js delete mode 100644 api/maps.js delete mode 100644 api/players.js create mode 100644 libs/routex.js diff --git a/api/api.js b/api/api.js deleted file mode 100644 index 17fc8f5..0000000 --- a/api/api.js +++ /dev/null @@ -1,26 +0,0 @@ -import { Router } from 'express' -import playerApi from './players.js' -import mapApi from './maps.js' -import finishesApi from './finishes.js' -import graphApi from './graph.js' - -const api = Router() - -api.get( - '/', - (req, res) => res.json({ - success: true, - response: "You connected to DDStats API! :D" - }) -) - -api.use('/players', playerApi) -api.use('/maps', mapApi) -api.use('/finishes', finishesApi) -api.use('/graph', graphApi) - -/** - * This module is the entrypoint for the API. - * @module api/api - */ -export default api \ No newline at end of file diff --git a/api/finishes.js b/api/finishes.js deleted file mode 100644 index f339806..0000000 --- a/api/finishes.js +++ /dev/null @@ -1,59 +0,0 @@ -import { Router } from 'express' -import { sqlite } from '../libs/database/init.js' -import wrapper from '../libs/database/wrapper.js' - -const finishApi = Router() - -/* TODO: precalculate this */ -finishApi.get( - '/count', - (req, res) => { - const finishes = sqlite.prepare(`SELECT COUNT(*) as count FROM race`).get() - - return res.json({ - success: true, - response: finishes.count, - }) - } -) - -finishApi.get( - '/finishedMaps/:player', - (req, res) => { - /* Check if player exists */ - if(!wrapper.playerExists(req.params.player)) { - return res.json({ - success: false, - response: "No such player!" - }) - } - const finishes = wrapper.finishedMaps(req.params.player) - - return res.json({ - success: true, - response: finishes, - }) - } -) - -finishApi.get( - '/unfinishedMaps/:player', - (req, res) => { - /* Check if player exists */ - if(!wrapper.playerExists(req.params.player)) { - return res.json({ - success: false, - response: "No such player!" - }) - } - const finishes = wrapper.unfinishedMaps(req.params.player) - - return res.json({ - success: true, - response: finishes, - }) - } -) - - -export default finishApi \ No newline at end of file diff --git a/api/graph.js b/api/graph.js deleted file mode 100644 index 8e72a39..0000000 --- a/api/graph.js +++ /dev/null @@ -1,66 +0,0 @@ -import { Router } from 'express' -import { sqlite } from '../libs/database/init.js' - -const graphApi = Router() - -graphApi.get( - '/points', - (req, res) => { - /* Check if a query was provided */ - if (!req.query.q) { - return res.json({ - success: false, - response: "No query ('host/path?q=query') provided!" - }) - } - let player = req.query.q - - const finishes = sqlite.prepare( - ` - SELECT DISTINCT(a.map), a.date, b.points - FROM race AS a - INNER JOIN maps AS b - ON a.map = b.map - WHERE a.player = ? - GROUP BY a.map - ORDER BY a.date; - `) - let currentPoints = 0 - let array = [] - - for (const finish of finishes.iterate(player)) { - currentPoints += finish.points - array.push({ t: new Date(finish.date), y: currentPoints }) - } - - return res.json({ - success: true, - response: array, - }) - } -) - -graphApi.get( - '/map', - (req, res) => { - /* Check if a query was provided */ - if (!req.query.q) { - return res.json({ - success: false, - response: "No query ('host/path?q=query') provided!" - }) - } - const finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY date`) - - let array = [] - for (const record of finishes.iterate(req.query.q)) - array.push({ t: new Date(record.date), y: record.time, player: record.player}) - - return res.json({ - success: true, - response: array, - }) - } -) - -export default graphApi \ No newline at end of file diff --git a/api/maps.js b/api/maps.js deleted file mode 100644 index d2ebceb..0000000 --- a/api/maps.js +++ /dev/null @@ -1,115 +0,0 @@ -import { Router } from 'express' -import { sqlite } from '../libs/database/init.js' -import wrapper, { map } from '../libs/database/wrapper.js' - -const mapApi = Router() - -mapApi.get( - '/count', - (req, res) => { - const totalMaps = sqlite.prepare(`SELECT COUNT(*) as count FROM maps`).get() - - return res.json({ - success: true, - response: totalMaps.count, - }) - } -) - -mapApi.get( - '/get/:map', - (req, res) => { - /* Check if map exists */ - if(!wrapper.mapExists(req.params.map)) { - return res.json({ - success: false, - response: "No such map!" - }) - } - - return res.json({ - success: true, - response: map(req.params.map) - }) - } -) - -mapApi.get( - '/getAll', - (req, res) => { - return res.json({ - success: true, - response: wrapper.allMaps() - }) - } -) - -mapApi.get( - '/category/:category', - (req, res) => { - /* Check if category exists */ - if (!wrapper.categoryExists(req.params.category)) { - return res.json({ - success: false, - response: "Invalid category name!", - }) - } - - return res.json({ - success: true, - response: wrapper.mapCategory(req.params.category) - }) - } -) - -mapApi.get( - '/leaderboard/race/:map', - (req, res) => { - /* Check if map exists */ - if (!wrapper.mapExists(req.params.map)) { - return res.json({ - success: false, - response: "No such map!", - }) - } - - return res.json({ - success: true, - response: wrapper.leaderboardRace(req.params.map, 1, 20) - }) - } -) - -mapApi.get( - '/leaderboard/teamrace/:map', - (req, res) => { - /* Check if map exists */ - if (!wrapper.mapExists(req.params.map)) { - return res.json({ - success: false, - response: "No such map!", - }) - } - - return res.json({ - success: true, - response: wrapper.leaderboardTeamrace(req.params.map, 1, 20) - }) - } -) - -mapApi.get( - '/search', - async (req, res) => { - /* Check if a query was provided */ - if (!req.query.q) { - return res.json({ - success: false, - response: "No query ('host/path?q=query') provided!" - }) - } - /* TODO: Use the searcher function */ - } -) - -export default mapApi diff --git a/api/players.js b/api/players.js deleted file mode 100644 index 6bf13cf..0000000 --- a/api/players.js +++ /dev/null @@ -1,57 +0,0 @@ -import { Router } from 'express' -import wrapper from '../libs/database/wrapper.js' - -const playerApi = Router() - -playerApi.get( - '/get/:player', - async (req, res) => { - /* Check if player exists */ - if(!wrapper.playerExists(req.params.player)) { - return res.json({ - success: false, - response: "No such player!" - }) - } - const data = wrapper.player(req.params.player) - - return res.json({ - success: true, - response: data - }) - } -) - -playerApi.get( - '/search', - async (req, res) => { - if (!req.query.q) { - return res.json({ - success: false, - response: "No query ('?q=query') provided!" - }) - } - - searcher( - 'points', - 'player', - `%${req.query.q}%`, - req.query.sort ?? undefined, - req.query.order === "asc", - "all", - req.query.page - ).then( - player => res.json({ - success: true, - response: player - }) - ).catch( - error => res.json({ - success: false, - response: error - }) - ) - } -) - -export default playerApi diff --git a/index.js b/index.js index 8409864..6c77fff 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,6 @@ import njk from 'nunjucks' import initLog from './libs/utils/log.js' import routes from './routes/routes.js' -import api from './api/api.js' import { generateDB } from './libs/database/generate.js' import { dbInit } from './libs/database/init.js' @@ -40,7 +39,7 @@ njk.configure( ) Server.use('/', routes) -Server.use('/api', api) +Server.use('/api', routes) Server.use('/assets', express.static('static')) Server.listen(process.env.PORT ?? 12345, () => log(`Server started and listening on port ${process.env.PORT}.`)) diff --git a/libs/routex.js b/libs/routex.js new file mode 100644 index 0000000..1d5a333 --- /dev/null +++ b/libs/routex.js @@ -0,0 +1,10 @@ +export default function routex(req, res) { + if (req.baseUrl === "/api") + return function(_, data, _2) { + res.json(data) + } + + return function(template, data, options) { + res.render(template, { data, options }) + } +} \ No newline at end of file diff --git a/routes/routes.js b/routes/routes.js index d7b1dee..1b391dc 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -1,10 +1,13 @@ import { Router } from 'express' +import routex from '../libs/routex.js' const routes = Router() routes.get( '/', - (req, res) => res.render('pages/landing.njk', {currentSection: "maps"}) + (req, res) => { + routex(req, res)('pages/landing.njk', null, { currentSection: null }) + } ) export default routes \ No newline at end of file diff --git a/views/components/_navbar.njk b/views/components/_navbar.njk index 2e65684..c78b4ea 100644 --- a/views/components/_navbar.njk +++ b/views/components/_navbar.njk @@ -1,7 +1,7 @@ \ No newline at end of file