diff --git a/api/api.js b/api/api.js index d630594..36f86a5 100644 --- a/api/api.js +++ b/api/api.js @@ -1,4 +1,5 @@ import { Router } from 'express' +import finishApi from './finishes.js' import mapApi from './maps.js' import playerApi from './players.js' @@ -12,8 +13,9 @@ api.get( }) ) -api.use('/players', playerApi) +api.use('/finishes', finishApi) api.use('/maps', mapApi) +api.use('/players', playerApi) /** * This module is the entrypoint for the API. diff --git a/api/finishes.js b/api/finishes.js new file mode 100644 index 0000000..329b83e --- /dev/null +++ b/api/finishes.js @@ -0,0 +1,57 @@ +import { Router } from 'express' +import Finish from '../schemas/Finish.js' + +const finishApi = Router() + +finishApi.get( + '/count', + (req, res) => { + Finish.find({}).count().then( + finishAmount => { + res.json({ + success: true, + response: finishAmount + }) + } + ) + } +) + +finishApi.get( + '/find', + async (req, res) => { + if (!req.query.player && !req.query.map) + return res.json({ + success: false, + response: "Please provide either a player or map!" + }) + + const player = req.query.player + const map = req.query.map + const sort = req.query.sort ?? 'date' + const order = req.query.order === "desc" ? -1 : 1 + + Finish.find({ player: player ?? /\w/, map: map ?? /\w/ }).sort([[sort, order]]).then( + finishes => { + if (!finishes[0]) + return res.json({ + success: false, + response: "No maps found!" + }) + + res.json({ + success: true, + response: { + finishes + } + }) + } + ) + } +) + +/** + * This module handles all API actions related to maps. + * @module api/finishes + */ +export default finishApi \ No newline at end of file diff --git a/schemas/Finish.js b/schemas/Finish.js index f3a4816..7fb925f 100644 --- a/schemas/Finish.js +++ b/schemas/Finish.js @@ -8,6 +8,8 @@ const Finish = new mongoose.Schema({ player: String }) +Finish.index({ player: 1, map: 1 }) + /** * This cotains the mongoose 'Finish' model. * @module schemas/Finish