2021-10-04 19:35:53 +02:00
|
|
|
import { Router } from 'express'
|
|
|
|
import Player from '../schemas/Player.js'
|
|
|
|
|
|
|
|
|
|
|
|
const playerApi = Router()
|
|
|
|
|
|
|
|
playerApi.get(
|
|
|
|
'/count',
|
2021-10-11 19:51:55 +02:00
|
|
|
(req, res) => {
|
|
|
|
Player.count({}).then(
|
2021-10-11 20:22:22 +02:00
|
|
|
playerAmount => {
|
2021-10-11 19:51:55 +02:00
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
response: playerAmount
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-10-04 19:35:53 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
playerApi.get(
|
|
|
|
'/get/:player',
|
2021-10-11 19:51:55 +02:00
|
|
|
(req, res) => {
|
|
|
|
Player.findOne({ name: req.params.player }).then(
|
|
|
|
player => {
|
|
|
|
if (!player)
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
response: "No player found!"
|
|
|
|
})
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
response: player
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-10-04 19:35:53 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
playerApi.get(
|
|
|
|
'/search',
|
|
|
|
async (req, res) => {
|
|
|
|
if (!req.query.q)
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
response: "No query ('host/path?q=query') provided!"
|
|
|
|
})
|
|
|
|
|
|
|
|
const name = req.query.q
|
|
|
|
const sort = req.query.sort ?? 'name'
|
|
|
|
const order = req.query.order === "desc" ? -1 : 1
|
|
|
|
const page = req.query.page ?? 1
|
|
|
|
const pageCount = Math.ceil((await Player.find({ name: { $regex: name, $options: 'i' }}).count()) / 20)
|
|
|
|
|
2021-10-11 19:51:55 +02:00
|
|
|
Player.find({ name: { $regex: name, $options: 'i' }}).sort([[sort, order]]).limit(20).skip((page - 1) * 20).then(
|
|
|
|
players => {
|
|
|
|
if (!players[0])
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
|
|
|
response: "No players found!"
|
|
|
|
})
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
response: {
|
|
|
|
pageCount,
|
|
|
|
players
|
|
|
|
}
|
|
|
|
})
|
2021-10-04 19:35:53 +02:00
|
|
|
}
|
2021-10-11 19:51:55 +02:00
|
|
|
)
|
2021-10-04 19:35:53 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-06 20:36:20 +02:00
|
|
|
/**
|
|
|
|
* This module handles all API actions related to players.
|
|
|
|
* @module api/players
|
|
|
|
*/
|
2021-10-04 19:35:53 +02:00
|
|
|
export default playerApi
|