Basic finishes API implementation.
This commit is contained in:
parent
d1dff43bbd
commit
4dc248c8b4
|
@ -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.
|
||||
|
|
57
api/finishes.js
Normal file
57
api/finishes.js
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user