Refactored maps API. Added ability to seacrh by mapper.

This commit is contained in:
BurnyLlama 2021-10-11 19:49:53 +02:00
parent 0bafd5e4a6
commit 5fbcc2cc68

View File

@ -5,21 +5,23 @@ const mapApi = Router()
mapApi.get(
'/count',
async (req, res) => {
const mapAmount = await Level.find({}).count()
(req, res) => {
Level.find({}).count().then(
mapAmount => {
res.json({
success: true,
response: mapAmount
})
}
)
}
)
mapApi.get(
'/get/:map',
async (req, res) => {
const map = await Level.findOne({ name: req.params.map })
(req, res) => {
Level.findOne({ name: req.params.map }).then(
map => {
if (!map)
return res.json({
success: false,
@ -32,6 +34,28 @@ mapApi.get(
})
}
)
}
)
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',
@ -46,10 +70,11 @@ mapApi.get(
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)
const maps = await Level.find({ name: { $regex: name, $options: 'i' }}).sort([[sort, order]]).limit(20).skip((page - 1) * 20)
Level.find(filter).sort([[sort, order]]).limit(20).skip((page - 1) * 20).then(
maps => {
if (!maps[0])
return res.json({
success: false,
@ -65,6 +90,8 @@ mapApi.get(
})
}
)
}
)
/**
* This module handles all API actions related to maps.