Refactored player API.
This commit is contained in:
parent
5fbcc2cc68
commit
5ecb679fd9
|
@ -6,21 +6,23 @@ const playerApi = Router()
|
||||||
|
|
||||||
playerApi.get(
|
playerApi.get(
|
||||||
'/count',
|
'/count',
|
||||||
async (req, res) => {
|
(req, res) => {
|
||||||
const playerAmount = await Player.count({})
|
Player.count({}).then(
|
||||||
|
count => {
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
response: playerAmount
|
response: playerAmount
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
playerApi.get(
|
playerApi.get(
|
||||||
'/get/:player',
|
'/get/:player',
|
||||||
async (req, res) => {
|
(req, res) => {
|
||||||
const player = await Player.findOne({ name: req.params.player })
|
Player.findOne({ name: req.params.player }).then(
|
||||||
|
player => {
|
||||||
if (!player)
|
if (!player)
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -33,6 +35,8 @@ playerApi.get(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
playerApi.get(
|
playerApi.get(
|
||||||
'/search',
|
'/search',
|
||||||
|
@ -47,10 +51,10 @@ playerApi.get(
|
||||||
const sort = req.query.sort ?? 'name'
|
const sort = req.query.sort ?? 'name'
|
||||||
const order = req.query.order === "desc" ? -1 : 1
|
const order = req.query.order === "desc" ? -1 : 1
|
||||||
const page = req.query.page ?? 1
|
const page = req.query.page ?? 1
|
||||||
|
|
||||||
const pageCount = Math.ceil((await Player.find({ name: { $regex: name, $options: 'i' }}).count()) / 20)
|
const pageCount = Math.ceil((await Player.find({ name: { $regex: name, $options: 'i' }}).count()) / 20)
|
||||||
const players = await Player.find({ name: { $regex: name, $options: 'i' }}).sort([[sort, order]]).limit(20).skip((page - 1) * 20)
|
|
||||||
|
|
||||||
|
Player.find({ name: { $regex: name, $options: 'i' }}).sort([[sort, order]]).limit(20).skip((page - 1) * 20).then(
|
||||||
|
players => {
|
||||||
if (!players[0])
|
if (!players[0])
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -66,6 +70,8 @@ playerApi.get(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This module handles all API actions related to players.
|
* This module handles all API actions related to players.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user