From 6080b0de4ff0be370455b930fbe59934deeb2105 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Mon, 4 Oct 2021 19:36:07 +0200 Subject: [PATCH] Added API for maps... --- api/maps.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 api/maps.js diff --git a/api/maps.js b/api/maps.js new file mode 100644 index 0000000..93d5164 --- /dev/null +++ b/api/maps.js @@ -0,0 +1,69 @@ +import { Router } from 'express' +import Level from '../schemas/Level.js' + +const mapApi = Router() + +mapApi.get( + '/count', + async (req, res) => { + const mapAmount = await Level.find({}).count() + + res.json({ + success: true, + response: mapAmount + }) + } +) + +mapApi.get( + '/get/:map', + async (req, res) => { + const map = await Level.findOne({ name: req.params.map }) + + if (!map) + return res.json({ + success: false, + response: "No map found!" + }) + + res.json({ + success: true, + response: map + }) + } +) + +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 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 + } + }) + } +) + +export default mapApi \ No newline at end of file