Basic finishes API implementation.

This commit is contained in:
BurnyLlama 2021-10-11 22:47:34 +02:00
parent d1dff43bbd
commit 4dc248c8b4
3 changed files with 62 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import { Router } from 'express' import { Router } from 'express'
import finishApi from './finishes.js'
import mapApi from './maps.js' import mapApi from './maps.js'
import playerApi from './players.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('/maps', mapApi)
api.use('/players', playerApi)
/** /**
* This module is the entrypoint for the API. * This module is the entrypoint for the API.

57
api/finishes.js Normal file
View File

@ -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

View File

@ -8,6 +8,8 @@ const Finish = new mongoose.Schema({
player: String player: String
}) })
Finish.index({ player: 1, map: 1 })
/** /**
* This cotains the mongoose 'Finish' model. * This cotains the mongoose 'Finish' model.
* @module schemas/Finish * @module schemas/Finish