From 5ecb679fd9719955820a8c45b964a7695e5c0f20 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Mon, 11 Oct 2021 19:51:55 +0200 Subject: [PATCH] Refactored player API. --- api/players.js | 74 +++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/api/players.js b/api/players.js index b93dc86..5f8019e 100644 --- a/api/players.js +++ b/api/players.js @@ -6,31 +6,35 @@ const playerApi = Router() playerApi.get( '/count', - async (req, res) => { - const playerAmount = await Player.count({}) - - res.json({ - success: true, - response: playerAmount - }) + (req, res) => { + Player.count({}).then( + count => { + res.json({ + success: true, + response: playerAmount + }) + } + ) } ) playerApi.get( '/get/:player', - async (req, res) => { - const player = await Player.findOne({ name: req.params.player }) - - if (!player) - return res.json({ - success: false, - response: "No player found!" - }) - - res.json({ - success: true, - response: player - }) + (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 + }) + } + ) } ) @@ -47,23 +51,25 @@ playerApi.get( 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) - const players = await Player.find({ name: { $regex: name, $options: 'i' }}).sort([[sort, order]]).limit(20).skip((page - 1) * 20) - if (!players[0]) - return res.json({ - success: false, - response: "No players found!" - }) - - res.json({ - success: true, - response: { - pageCount, - players + 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 + } + }) } - }) + ) } )