Added countries & cities REST services
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
// eslint-disable
|
||||
|
||||
const getCity = async (res: any, dbConn: any, city: string) => {
|
||||
try {
|
||||
res.set("Connection", "close");
|
||||
|
||||
let sqlQuery = "";
|
||||
sqlQuery = `
|
||||
SELECT *
|
||||
FROM cities
|
||||
WHERE cityId=?
|
||||
`;
|
||||
|
||||
const cities = await dbConn.query(sqlQuery, [city]);
|
||||
if (cities.length === 0) {
|
||||
res.status(404).send("City not found");
|
||||
} else {
|
||||
res
|
||||
.status(200)
|
||||
.set("Content-Type", "application/json")
|
||||
.send(JSON.stringify(cities));
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
const deleteCity = async (res: any, dbConn: any, city: string) => {
|
||||
try {
|
||||
res.set("Connection", "close");
|
||||
|
||||
const deleteCity = `
|
||||
DELETE FROM cities
|
||||
WHERE cityId=?
|
||||
`;
|
||||
|
||||
const result = await dbConn.query(deleteCity, [city]);
|
||||
if (result.info.affectedRows > 0) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(404).send("City not found");
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
const postCity = async (
|
||||
res: any,
|
||||
dbConn: any,
|
||||
name: string,
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
population: number,
|
||||
country: string,
|
||||
region: string
|
||||
) => {
|
||||
res.set("Connection", "close");
|
||||
|
||||
if (!name) {
|
||||
res.status(400).send("Missing name");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const sqlRegion = `
|
||||
SELECT 1
|
||||
FROM regions
|
||||
WHERE countryCode=? AND regionCode=?
|
||||
`;
|
||||
const regions = await dbConn.query(sqlRegion, [country, region]);
|
||||
if (regions.length === 0) {
|
||||
res.status(403).send("Region must exist");
|
||||
return;
|
||||
}
|
||||
|
||||
const sqlGetId = `
|
||||
SELECT MAX(CAST(cityId AS INTEGER)) AS maxr
|
||||
FROM cities
|
||||
`;
|
||||
const cities = await dbConn.query(sqlGetId);
|
||||
const newId = cities.length === 0 ? 1 : 1 + Number(cities[0].maxr);
|
||||
|
||||
const sqlAddCity = `
|
||||
INSERT INTO cities SET
|
||||
cityId=?
|
||||
cityName=?,
|
||||
latitude=?,
|
||||
longitude=?,
|
||||
population=?,
|
||||
countryCode=?,
|
||||
regionCode=?
|
||||
`;
|
||||
|
||||
const result = await dbConn.query(sqlAddCity, [
|
||||
newId,
|
||||
name,
|
||||
latitude,
|
||||
longitude,
|
||||
population,
|
||||
country,
|
||||
region
|
||||
]);
|
||||
if (result.info.affectedRows > 0) {
|
||||
res
|
||||
.status(201)
|
||||
.header("Location", `/cities/${newId}`)
|
||||
.send("City created");
|
||||
} else {
|
||||
res.status(409).send("City not created");
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
const putCity = async (
|
||||
res: any,
|
||||
dbConn: any,
|
||||
city: string,
|
||||
name: string,
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
population: number,
|
||||
country: string,
|
||||
region: string
|
||||
) => {
|
||||
res.set("Connection", "close");
|
||||
|
||||
if (!name) {
|
||||
res.status(400).send("Missing name");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const sqlUpdateCity = `
|
||||
UPDATE cities
|
||||
SET
|
||||
cityName=?,
|
||||
latitude=?,
|
||||
longitude=?,
|
||||
population=?,
|
||||
countryCode=?,
|
||||
regionCode=?
|
||||
WHERE cityId=?
|
||||
`;
|
||||
|
||||
const result = await dbConn.query(sqlUpdateCity, [
|
||||
name,
|
||||
latitude,
|
||||
longitude,
|
||||
population,
|
||||
country,
|
||||
region,
|
||||
city
|
||||
]);
|
||||
|
||||
if (result.info.affectedRows > 0) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(409).send("City not updated");
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { getCity, deleteCity, postCity, putCity };
|
||||
@@ -0,0 +1,107 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
const getCountry = async (res: any, dbConn: any, country: ?string) => {
|
||||
try {
|
||||
res.set("Connection", "close");
|
||||
|
||||
let sqlQuery = "";
|
||||
let countries;
|
||||
if (country == null) {
|
||||
sqlQuery = `
|
||||
SELECT cc.*
|
||||
FROM countries cc
|
||||
ORDER BY cc.countryCode
|
||||
`;
|
||||
countries = await dbConn.query(sqlQuery);
|
||||
} else {
|
||||
sqlQuery = `
|
||||
SELECT cc.*
|
||||
FROM countries cc
|
||||
WHERE cc.countryCode=?
|
||||
`;
|
||||
countries = await dbConn.query(sqlQuery, [country]);
|
||||
}
|
||||
|
||||
if (countries.length > 0) {
|
||||
res
|
||||
.status(200)
|
||||
.set("Content-Type", "application/json")
|
||||
.send(JSON.stringify(countries));
|
||||
} else {
|
||||
res.status(404).send("Not found");
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
const deleteCountry = async (res: any, dbConn: any, country: string) => {
|
||||
try {
|
||||
res.set("Connection", "close");
|
||||
|
||||
const sqlRegions = `
|
||||
SELECT 1 FROM regions
|
||||
WHERE countryCode=?
|
||||
LIMIT 1
|
||||
`;
|
||||
const regions = await dbConn.query(sqlRegions, [country]);
|
||||
if (regions.length > 0) {
|
||||
res.status(405).send("Cannot delete a country with regions");
|
||||
return;
|
||||
}
|
||||
|
||||
const deleteCountry = `
|
||||
DELETE FROM countries
|
||||
WHERE countryCode=?
|
||||
`;
|
||||
|
||||
const result = await dbConn.query(deleteCountry, [country]);
|
||||
if (result.info.affectedRows > 0) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(404).send("Country not found");
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
const putCountry = async (
|
||||
res: any,
|
||||
dbConn: any,
|
||||
country: string,
|
||||
name: string
|
||||
) => {
|
||||
res.set("Connection", "close");
|
||||
|
||||
if (!name) {
|
||||
res.status(400).send("Missing name");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const sqlInsertOrUpdateCountry = `
|
||||
INSERT INTO countries (countryCode, countryName)
|
||||
VALUES (?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
countryName = ?
|
||||
`;
|
||||
|
||||
const result = await dbConn.query(sqlInsertOrUpdateCountry, [
|
||||
country,
|
||||
name,
|
||||
name
|
||||
]);
|
||||
|
||||
if (result.info.affectedRows > 0) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(409).send("Country not updated");
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { getCountry, deleteCountry, putCountry };
|
||||
@@ -79,6 +79,26 @@ app.use((req, res, next) => {
|
||||
});
|
||||
});
|
||||
|
||||
const {
|
||||
getCountry,
|
||||
deleteCountry,
|
||||
putCountry
|
||||
} = require("./restful_countries.js");
|
||||
|
||||
app.get("/countries", (req, res) => getCountry(res, dbConn));
|
||||
|
||||
app.get("/countries/:country", (req, res) =>
|
||||
getCountry(res, dbConn, req.params.country)
|
||||
);
|
||||
|
||||
app.delete("/countries/:country", (req, res) =>
|
||||
deleteCountry(res, dbConn, req.params.country)
|
||||
);
|
||||
|
||||
app.put("/countries/:country", (req, res) =>
|
||||
putCountry(res, dbConn, req.params.country, req.body.name)
|
||||
);
|
||||
|
||||
const {
|
||||
getRegion,
|
||||
deleteRegion,
|
||||
@@ -86,13 +106,13 @@ const {
|
||||
putRegion
|
||||
} = require("./restful_regions.js");
|
||||
|
||||
app.get("/regions/", (req, res) => getRegion(res, dbConn));
|
||||
app.get("/regions", (req, res) => getRegion(res, dbConn));
|
||||
|
||||
app.get("/regions/:country/", (req, res) =>
|
||||
app.get("/regions/:country", (req, res) =>
|
||||
getRegion(res, dbConn, req.params.country)
|
||||
);
|
||||
|
||||
app.get("/regions/:country/:region/", (req, res) =>
|
||||
app.get("/regions/:country/:region", (req, res) =>
|
||||
getRegion(res, dbConn, req.params.country, req.params.region)
|
||||
);
|
||||
|
||||
@@ -114,6 +134,50 @@ app.put("/regions/:country/:region", (req, res) =>
|
||||
)
|
||||
);
|
||||
|
||||
const {
|
||||
getCity,
|
||||
deleteCity,
|
||||
postCity,
|
||||
putCity
|
||||
} = require("./restful_cities.js");
|
||||
|
||||
// We do not allow calling /cities to get every city in the world
|
||||
|
||||
app.get("/cities/:city", (req, res) =>
|
||||
getCity(res, dbConn, req.params.city)
|
||||
);
|
||||
|
||||
app.delete("/cities/:city", (req, res) =>
|
||||
deleteCity(res, dbConn, req.params.city)
|
||||
);
|
||||
|
||||
app.post("/cities/:city", (req, res) =>
|
||||
postCity(
|
||||
res,
|
||||
dbConn,
|
||||
req.body.name,
|
||||
req.body.latitude,
|
||||
req.body.longitude,
|
||||
req.body.population,
|
||||
req.body.country,
|
||||
req.body.region
|
||||
)
|
||||
);
|
||||
|
||||
app.put("/cities/:city", (req, res) =>
|
||||
putCity(
|
||||
res,
|
||||
dbConn,
|
||||
req.params.city,
|
||||
req.body.name,
|
||||
req.body.latitude,
|
||||
req.body.longitude,
|
||||
req.body.population,
|
||||
req.body.country,
|
||||
req.body.region
|
||||
)
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
app.use((err, req, res, next) => {
|
||||
console.error("Error....", err.message);
|
||||
|
||||
Reference in New Issue
Block a user