ddstats-server/api/maps.js

115 lines
2.8 KiB
JavaScript

import { Router } from 'express'
import Level from '../schemas/Level.js'
const mapApi = Router()
mapApi.get(
'/count',
(req, res) => {
Level.find({}).count().then(
mapAmount => {
res.json({
success: true,
response: mapAmount
})
}
)
}
)
mapApi.get(
'/get/:map',
(req, res) => {
Level.findOne({ name: req.params.map }).then(
map => {
if (!map)
return res.json({
success: false,
response: "No map found!"
})
res.json({
success: true,
response: map
})
}
)
}
)
mapApi.get(
'/getAll',
(req, res) => {
Level.find({}).then(
maps => {
res.json({
success: true,
response: maps
})
}
)
}
)
mapApi.get(
'/category/:category',
(req, res) => {
Level.find({ category: req.params.category }).then(
maps => {
if (!maps[0])
return res.json({
success: false,
response: "Invalid category name!"
})
res.json({
success: true,
response: maps
})
}
)
}
)
mapApi.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 filter = req.query.byMapper ? { mapper: { $regex: name, $options: 'i' }} : { name: { $regex: name, $options: 'i' }}
const pageCount = Math.ceil((await Level.find({ name: { $regex: name, $options: 'i' }}).count()) / 20)
Level.find(filter).sort([[sort, order]]).limit(20).skip((page - 1) * 20).then(
maps => {
if (!maps[0])
return res.json({
success: false,
response: "No maps found!"
})
res.json({
success: true,
response: {
pageCount,
maps
}
})
}
)
}
)
/**
* This module handles all API actions related to maps.
* @module api/maps
*/
export default mapApi