Small refactor.

This commit is contained in:
BurnyLlama 2021-10-04 19:35:53 +02:00
parent ac7b31e5a6
commit d7cb68906b
3 changed files with 80 additions and 71 deletions

View File

@ -1,13 +1,18 @@
import { Router } from 'express' import { Router } from 'express'
import playerApi from './players/players.js' import mapApi from './maps.js'
import playerApi from './players.js'
const api = Router() const api = Router()
api.get('/', (req, res) => res.json({ api.get(
success: true, '/',
response: "You connected to DDStats API! :D" (req, res) => res.json({
})) success: true,
response: "You connected to DDStats API! :D"
})
)
api.use('/players', playerApi) api.use('/players', playerApi)
api.use('/maps', mapApi)
export default api export default api

70
api/players.js Normal file
View File

@ -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

View File

@ -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