Added Endpoints for Countries and Region/Country by ID
This commit is contained in:
parent
a8d84a4b36
commit
359e93ed4d
@ -13,6 +13,7 @@ const port = process.env.PORT
|
|||||||
// Router
|
// Router
|
||||||
const search = require("./routes/search");
|
const search = require("./routes/search");
|
||||||
const regions = require("./routes/regions");
|
const regions = require("./routes/regions");
|
||||||
|
const countries = require("./routes/countries");
|
||||||
const climate = require("./routes/climate");
|
const climate = require("./routes/climate");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -29,6 +30,7 @@ const app = express();
|
|||||||
// Express routes
|
// Express routes
|
||||||
app.use(search(dbConn));
|
app.use(search(dbConn));
|
||||||
app.use(regions(dbConn));
|
app.use(regions(dbConn));
|
||||||
|
app.use(countries(dbConn));
|
||||||
app.use(climate(dbConn));
|
app.use(climate(dbConn));
|
||||||
|
|
||||||
app.use((err, req, res, next) => {
|
app.use((err, req, res, next) => {
|
||||||
|
|||||||
11
backend/models/getCountries.js
Normal file
11
backend/models/getCountries.js
Normal file
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
13
backend/models/getCountryById.js
Normal file
13
backend/models/getCountryById.js
Normal file
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
18
backend/models/getRegionById.js
Normal file
18
backend/models/getRegionById.js
Normal file
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
@ -1,7 +1,9 @@
|
|||||||
module.exports = async (dbConn, lat, long, radius) => {
|
module.exports = async (dbConn) => {
|
||||||
const regions = await dbConn.query(
|
const regions = await dbConn.query(
|
||||||
`SELECT regions.id AS region_id,
|
`SELECT regions.id AS region_id,
|
||||||
regions.region AS name,
|
regions.region AS name,
|
||||||
|
regions.description,
|
||||||
|
regions.preview_img,
|
||||||
regions.country_id AS country_id,
|
regions.country_id AS country_id,
|
||||||
countries.country AS country,
|
countries.country AS country,
|
||||||
regions.meteostat_id AS meteostat_id
|
regions.meteostat_id AS meteostat_id
|
||||||
|
|||||||
15
backend/routes/countries.js
Normal file
15
backend/routes/countries.js
Normal file
@ -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;
|
||||||
|
};
|
||||||
@ -1,10 +1,15 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
const getRegions = require("../models/getRegions.js");
|
const getRegions = require("../models/getRegions.js");
|
||||||
|
const getRegionById = require("../models/getRegionById.js");
|
||||||
|
|
||||||
module.exports = dbConn => {
|
module.exports = dbConn => {
|
||||||
router.get("/api/v1/regions", async (req, res) => {
|
router.get("/api/v1/regions", async (req, res) => {
|
||||||
res.json(await getRegions(dbConn));
|
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;
|
return router;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user