travopti/backend/routes/regions.js

113 lines
3.1 KiB
JavaScript

/**
* @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
* path:
* /regions:
* get:
* summary: Get all regions
* tags: [Regions]
* responses:
* "200":
* description: Returns all regions and aviable data
* content:
* application/json:
* schema:
* $ref: ''
*/
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
* path:
* /regions/:id:
* get:
* summary: Get a specific region
* tags: [Regions]
* responses:
* "200":
* description: Returns the specific region and aviable data
* content:
* application/json:
* schema:
* $ref: ''
*/
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
* path:
* /regions/:id/image:
* get:
* summary: Get all country
* tags: [Regions]
* responses:
* "200":
* description: Returns the image for a specific region
* content:
* application/json:
* schema:
* $ref: ''
*/
router.get('/api/v1/regions/:id/image', (req, res) => {
if (fs.existsSync(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))) {
res.sendFile(path.join(__dirname, `../data/regions/images/${req.params.id}.jpg`))
} else {
res.sendFile(path.join(__dirname, `../data/regions/images/x.png`))
}
})
/**
* @swagger
* path:
* /regions/:id/nearby:
* get:
* summary: Get nearby places for a specific region
* tags: [Regions]
* responses:
* "200":
* description: Returns nearby places for a specific region
* content:
* application/json:
* schema:
* $ref: ''
*/
router.get("/api/v1/regions/:id/nearby", async (req,res) => {
const id = sqlSanitzer(req.params.id);
res.json(await getRegionNearbyById(dbConn,id))
});
return router;
};