From 6fce605add33aa2de8db81067a0859183b328b8b Mon Sep 17 00:00:00 2001 From: Timo John Date: Tue, 16 Jun 2020 10:48:54 +0200 Subject: [PATCH] Added Endpoints for Countries and Region/Country by ID --- backend/index.js | 2 ++ backend/models/getCountries.js | 11 +++++++++++ backend/models/getCountryById.js | 13 +++++++++++++ backend/models/getRegionById.js | 18 ++++++++++++++++++ backend/models/getRegions.js | 4 +++- backend/routes/countries.js | 15 +++++++++++++++ backend/routes/regions.js | 5 +++++ 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 backend/models/getCountries.js create mode 100644 backend/models/getCountryById.js create mode 100644 backend/models/getRegionById.js create mode 100644 backend/routes/countries.js diff --git a/backend/index.js b/backend/index.js index 5795a84..3cf2720 100644 --- a/backend/index.js +++ b/backend/index.js @@ -13,6 +13,7 @@ const port = process.env.PORT // Router const search = require("./routes/search"); const regions = require("./routes/regions"); +const countries = require("./routes/countries"); const climate = require("./routes/climate"); const app = express(); @@ -29,6 +30,7 @@ const app = express(); // Express routes app.use(search(dbConn)); app.use(regions(dbConn)); + app.use(countries(dbConn)); app.use(climate(dbConn)); app.use((err, req, res, next) => { diff --git a/backend/models/getCountries.js b/backend/models/getCountries.js new file mode 100644 index 0000000..0402a19 --- /dev/null +++ b/backend/models/getCountries.js @@ -0,0 +1,11 @@ +module.exports = async (dbConn) => { + const countries = await dbConn.query( + `SELECT countries.id AS country_id, + countries.country AS name, + countries.description, + countries.preview_img + FROM countries` + ); + return countries; +}; + diff --git a/backend/models/getCountryById.js b/backend/models/getCountryById.js new file mode 100644 index 0000000..8b65ab1 --- /dev/null +++ b/backend/models/getCountryById.js @@ -0,0 +1,13 @@ +module.exports = async (dbConn, id) => { + const country = await dbConn.query( + `SELECT countries.id AS country_id, + countries.country AS name, + countries.description, + countries.preview_img + FROM countries + WHERE countries.id = ?`, + [id] + ); + return country; +}; + diff --git a/backend/models/getRegionById.js b/backend/models/getRegionById.js new file mode 100644 index 0000000..51ef337 --- /dev/null +++ b/backend/models/getRegionById.js @@ -0,0 +1,18 @@ +module.exports = async (dbConn, id) => { + const region = await dbConn.query( + `SELECT regions.id AS region_id, + regions.region AS name, + regions.description, + regions.preview_img, + regions.country_id AS country_id, + countries.country AS country, + regions.meteostat_id AS meteostat_id + FROM regions + JOIN countries + ON regions.country_id = countries.id + WHERE regions.id = ?`, + [id] + ); + return region; +}; + diff --git a/backend/models/getRegions.js b/backend/models/getRegions.js index a1efa2e..ba86da8 100644 --- a/backend/models/getRegions.js +++ b/backend/models/getRegions.js @@ -1,7 +1,9 @@ -module.exports = async (dbConn, lat, long, radius) => { +module.exports = async (dbConn) => { const regions = await dbConn.query( `SELECT regions.id AS region_id, regions.region AS name, + regions.description, + regions.preview_img, regions.country_id AS country_id, countries.country AS country, regions.meteostat_id AS meteostat_id diff --git a/backend/routes/countries.js b/backend/routes/countries.js new file mode 100644 index 0000000..b265b18 --- /dev/null +++ b/backend/routes/countries.js @@ -0,0 +1,15 @@ +const router = require("express").Router(); +const getCountries = require("../models/getCountries.js"); +const getCountryById = require("../models/getCountryById.js"); + +module.exports = dbConn => { + router.get("/api/v1/countries", async (req, res) => { + res.json(await getCountries(dbConn)); + }); + + router.get("/api/v1/countries/:id", async (req, res) => { + const id = req.params.id; + res.json(await getCountryById(dbConn, id)) + }); + return router; +}; diff --git a/backend/routes/regions.js b/backend/routes/regions.js index 44c4d91..821a669 100644 --- a/backend/routes/regions.js +++ b/backend/routes/regions.js @@ -1,10 +1,15 @@ const router = require("express").Router(); const getRegions = require("../models/getRegions.js"); +const getRegionById = require("../models/getRegionById.js"); module.exports = dbConn => { router.get("/api/v1/regions", async (req, res) => { res.json(await getRegions(dbConn)); }); + router.get("/api/v1/regions/:id", async (req, res) => { + const id = req.params.id; + res.json(await getRegionById(dbConn, id)) + }); return router; };