From d7cb68906bf2887b8015dfb71982ae13a5772366 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Mon, 4 Oct 2021 19:35:53 +0200 Subject: [PATCH] Small refactor. --- api/api.js | 15 ++++++--- api/players.js | 70 ++++++++++++++++++++++++++++++++++++++++++ api/players/players.js | 66 --------------------------------------- 3 files changed, 80 insertions(+), 71 deletions(-) create mode 100644 api/players.js delete mode 100644 api/players/players.js diff --git a/api/api.js b/api/api.js index 68093e0..ce9674a 100644 --- a/api/api.js +++ b/api/api.js @@ -1,13 +1,18 @@ import { Router } from 'express' -import playerApi from './players/players.js' +import mapApi from './maps.js' +import playerApi from './players.js' const api = Router() -api.get('/', (req, res) => res.json({ - success: true, - response: "You connected to DDStats API! :D" -})) +api.get( + '/', + (req, res) => res.json({ + success: true, + response: "You connected to DDStats API! :D" + }) +) api.use('/players', playerApi) +api.use('/maps', mapApi) export default api \ No newline at end of file diff --git a/api/players.js b/api/players.js new file mode 100644 index 0000000..cebacc5 --- /dev/null +++ b/api/players.js @@ -0,0 +1,70 @@ +import { Router } from 'express' +import Player from '../schemas/Player.js' + + +const playerApi = Router() + +playerApi.get( + '/count', + async (req, res) => { + const playerAmount = await Player.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 + }) + } +) + +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) + 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 + } + }) + } +) + +export default playerApi \ No newline at end of file diff --git a/api/players/players.js b/api/players/players.js deleted file mode 100644 index c490f14..0000000 --- a/api/players/players.js +++ /dev/null @@ -1,66 +0,0 @@ -import { response, Router } from 'express' -import initLog from '../../libs/utills/log.js' -import Player from '../../schemas/Player.js' - -const log = initLog("Player API") - -const playerApi = Router() - -playerApi.get('/countAll', async (req, res) => { - const playerAmount = await Player.count({}) - - res.json({ - success: true, - response: playerAmount - }) -}) - -playerApi.get('/getAll', async (req, res) => { - const players = await Player.find({}).select('name') - - res.json({ - success: true, - response: players - }) -}) - -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 - }) -}) - -playerApi.get('/search', async (req, res) => { - if (!req.query.name) - return res.json({ - success: false, - response: "No parameter 'name' provided!" - }) - - const name = req.query.name - const sort = req.query.sort ?? 'name' - const order = req.query.order === "desc" ? -1 : 1 - const page = req.query.page ?? 1 - - - 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(players) -}) - -export default playerApi \ No newline at end of file