From 5fbcc2cc68b11f53a2ce00ac84550e409b8baf3d Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Mon, 11 Oct 2021 19:49:53 +0200 Subject: [PATCH] Refactored maps API. Added ability to seacrh by mapper. --- api/maps.js | 93 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/api/maps.js b/api/maps.js index 766cdd2..e4ad97a 100644 --- a/api/maps.js +++ b/api/maps.js @@ -5,31 +5,55 @@ const mapApi = Router() mapApi.get( '/count', - async (req, res) => { - const mapAmount = await Level.find({}).count() - - res.json({ - success: true, - response: mapAmount - }) + (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, + response: "No map found!" + }) + + res.json({ + success: true, + response: map + }) + } + ) + } +) - if (!map) - return res.json({ - success: false, - response: "No map found!" - }) - - res.json({ - success: true, - response: map - }) +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 + }) + } + ) } ) @@ -46,23 +70,26 @@ 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) - if (!maps[0]) - return res.json({ - success: false, - response: "No maps found!" - }) - - res.json({ - success: true, - response: { - pageCount, - maps + 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 + } + }) } - }) + ) } )