/** * @swagger * tags: * name: Regions * description: Access region data. */ const router = require("express").Router(); // Models const getRegions = require("../models/getRegions.js"); const getRegionById = require("../models/getRegionById.js"); const getRegionNearbyById = require("../models/getRegionNearbyById.js") // Utils const path = require("path"); const fs = require("fs"); const _ = require('lodash') const sqlSanitzer = require("../util/sqlstring_sanitizer.js") module.exports = dbConn => { /** * @swagger * /regions: * get: * summary: Get all regions * tags: [Regions] * responses: * "200": * description: Returns available data for all regions */ router.get("/api/v1/regions", async (req, res) => { const data = await getRegions(dbConn) if (req.query.randomize) { const randomize = sqlSanitzer(req.query.randomize) res.json(_.sampleSize(data, randomize)) } else { res.json(data); } }); /** * @swagger * /regions/{id}: * get: * summary: Get a specific region by id * tags: [Regions] * parameters: * - name: "id" * in: "path" * required: true * type: int * responses: * "200": * description: Returns available data for the region */ router.get("/api/v1/regions/:id", async (req, res) => { console.log(typeof req.params.id) const id = sqlSanitzer(req.params.id); console.log(id) res.json(await getRegionById(dbConn, id)) }); /** * @swagger * /regions/{id}/image: * get: * summary: Get image for specific region * tags: [Regions] * parameters: * - name: "id" * in: "path" * required: true * type: int * responses: * "200": * description: Returns the image for a specific region * "404": * description: Returns a placeholder image for the region */ router.get('/api/v1/regions/:id/image', (req, res) => { console.log("HERE") if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) { console.log("EXISTS") res.status(200).sendFile(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`)) } else { console.log("NOT EXISTS") res.status(404).sendFile(path.join(__dirname, `../data/regions/images/x.png`)) } }) /** * @swagger * /regions/{id}/nearby: * get: * summary: Get nearby places of a specific region by id * tags: [Regions] * parameters: * - name: "id" * in: "path" * required: true * type: int * responses: * "200": * description: Returns all nearby places for the region */ router.get("/api/v1/regions/:id/nearby", async (req,res) => { const id = sqlSanitzer(req.params.id); res.json(await getRegionNearbyById(dbConn,id)) }); return router; };