Completed code
This commit is contained in:
@@ -7,40 +7,38 @@ const getRegion = async (
|
|||||||
res: any,
|
res: any,
|
||||||
dbConn: any,
|
dbConn: any,
|
||||||
country: ?string,
|
country: ?string,
|
||||||
id: ?string
|
region: ?string
|
||||||
) => {
|
) => {
|
||||||
console.log("COUNTRY", country, "ID", id, typeof id);
|
|
||||||
|
|
||||||
let sqlQuery = "";
|
|
||||||
if (country == null) {
|
|
||||||
sqlQuery = `
|
|
||||||
SELECT rr.*, cc.countryName
|
|
||||||
FROM regions rr
|
|
||||||
JOIN countries cc
|
|
||||||
ON cc.countryCode=rr.countryCode
|
|
||||||
ORDER BY cc.countryCode, rr.regionCode
|
|
||||||
`;
|
|
||||||
} else if (id == null) {
|
|
||||||
sqlQuery = `
|
|
||||||
SELECT rr.*, cc.countryName
|
|
||||||
FROM regions rr
|
|
||||||
JOIN countries cc
|
|
||||||
ON cc.countryCode=rr.countryCode
|
|
||||||
WHERE rr.countryCode="${country}"
|
|
||||||
ORDER BY rr.regionCode
|
|
||||||
`;
|
|
||||||
} else {
|
|
||||||
sqlQuery = `
|
|
||||||
SELECT rr.*, cc.countryName
|
|
||||||
FROM regions rr
|
|
||||||
JOIN countries cc
|
|
||||||
ON cc.countryCode=rr.countryCode
|
|
||||||
WHERE rr.countryCode="${country}"
|
|
||||||
AND rr.regionCode="${id}"
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
let sqlQuery = "";
|
||||||
|
if (country == null) {
|
||||||
|
sqlQuery = `
|
||||||
|
SELECT rr.*, cc.countryName
|
||||||
|
FROM regions rr
|
||||||
|
JOIN countries cc
|
||||||
|
ON cc.countryCode=rr.countryCode
|
||||||
|
ORDER BY cc.countryCode, rr.regionCode
|
||||||
|
`;
|
||||||
|
} else if (id == null) {
|
||||||
|
sqlQuery = `
|
||||||
|
SELECT rr.*, cc.countryName
|
||||||
|
FROM regions rr
|
||||||
|
JOIN countries cc
|
||||||
|
ON cc.countryCode=rr.countryCode
|
||||||
|
WHERE rr.countryCode="${country}"
|
||||||
|
ORDER BY rr.regionCode
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
sqlQuery = `
|
||||||
|
SELECT rr.*, cc.countryName
|
||||||
|
FROM regions rr
|
||||||
|
JOIN countries cc
|
||||||
|
ON cc.countryCode=rr.countryCode
|
||||||
|
WHERE rr.countryCode="${country}"
|
||||||
|
AND rr.regionCode="${region}"
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
const regions = await dbConn.query(sqlQuery);
|
const regions = await dbConn.query(sqlQuery);
|
||||||
res
|
res
|
||||||
.status(200)
|
.status(200)
|
||||||
@@ -57,22 +55,21 @@ const deleteRegion = async (
|
|||||||
country: string,
|
country: string,
|
||||||
region: string
|
region: string
|
||||||
) => {
|
) => {
|
||||||
const sqlCities = `
|
|
||||||
SELECT 1 FROM cities
|
|
||||||
WHERE countryCode="${country}
|
|
||||||
AND regionCode="${region}
|
|
||||||
LIMIT 1"
|
|
||||||
`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const sqlCities = `
|
||||||
|
SELECT 1 FROM cities
|
||||||
|
WHERE countryCode="${country}"
|
||||||
|
AND regionCode="${region}"
|
||||||
|
LIMIT 1"
|
||||||
|
`;
|
||||||
const cities = await dbConn.query(sqlCities);
|
const cities = await dbConn.query(sqlCities);
|
||||||
if (cities.length > 0) {
|
if (cities.length > 0) {
|
||||||
res.status(403).send("Cannot delete a region with cities");
|
res.status(403).send("Cannot delete a region with cities");
|
||||||
} else {
|
} else {
|
||||||
const deleteRegion = `
|
const deleteRegion = `
|
||||||
DELETE FROM regions
|
DELETE FROM regions
|
||||||
WHERE countryCode="${country}
|
WHERE countryCode="${country}"
|
||||||
AND regionCode="${region}
|
AND regionCode="${region}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const result = await dbConn.query(deleteRegion);
|
const result = await dbConn.query(deleteRegion);
|
||||||
@@ -92,14 +89,58 @@ const putRegion = async (
|
|||||||
res: any,
|
res: any,
|
||||||
dbConn: any,
|
dbConn: any,
|
||||||
country: string,
|
country: string,
|
||||||
id: string,
|
region: string,
|
||||||
region: any
|
name: string
|
||||||
) => {
|
) => {
|
||||||
res.status(200).send("NOTHING DOING NOW...");
|
try {
|
||||||
|
const sqlCountry = `
|
||||||
|
SELECT 1 FROM countries
|
||||||
|
WHERE countryCode="${country}"
|
||||||
|
`;
|
||||||
|
const countries = await dbConn.query(sqlCountry);
|
||||||
|
if (countries.length === 0) {
|
||||||
|
res.status(403).send("Country must exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
const sqlGetRegion = `
|
||||||
|
SELECT 1
|
||||||
|
FROM regions
|
||||||
|
WHERE countryCode="${country}"
|
||||||
|
AND regionCode="${region}"
|
||||||
|
`;
|
||||||
|
const regions = await dbConn.query(sqlGetRegion);
|
||||||
|
if (regions.length === 0) {
|
||||||
|
const sqlAddRegion = `
|
||||||
|
INSERT INTO regions SET
|
||||||
|
countryCode="${country}",
|
||||||
|
regionCode="${region}",
|
||||||
|
regionName="${name}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
const result = await dbConn.query(sqlAddRegion);
|
||||||
|
if (result.affectedRows > 0) {
|
||||||
|
res.status(201).send("Region created");
|
||||||
|
} else {
|
||||||
|
res.status(409).send("Region not created");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const sqlUpdateRegion = `
|
||||||
|
UPDATE regions
|
||||||
|
SET regionName="${name}"
|
||||||
|
WHERE countryCode="${country}"
|
||||||
|
AND regionCode="${region}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
const result = await dbConn.query(sqlUpdateRegion);
|
||||||
|
if (result.affectedRows > 0) {
|
||||||
|
res.status(204).send("Region updated");
|
||||||
|
} else {
|
||||||
|
res.status(409).send("Region not updated");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
res.status(500).send("Server error");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const postRegion = async (res: any, dbConn: any, region: any) => {
|
module.exports = { getRegion, deleteRegion, putRegion };
|
||||||
res.status(200).send("NOTHING DOING NOW...");
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = { getRegion, putRegion, deleteRegion, postRegion };
|
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ const dbConn = require("./restful_db.js");
|
|||||||
const {
|
const {
|
||||||
getRegion,
|
getRegion,
|
||||||
deleteRegion,
|
deleteRegion,
|
||||||
putRegion,
|
putRegion
|
||||||
postRegion
|
|
||||||
} = require("./restful_regions.js");
|
} = require("./restful_regions.js");
|
||||||
|
|
||||||
const SECRET_JWT_KEY = "modernJSbook";
|
const SECRET_JWT_KEY = "modernJSbook";
|
||||||
@@ -77,11 +76,15 @@ app.delete("/regions/:country/:region", (req, res) =>
|
|||||||
);
|
);
|
||||||
|
|
||||||
app.put("/regions/:country/:region", (req, res) =>
|
app.put("/regions/:country/:region", (req, res) =>
|
||||||
putRegion(res, dbConn, req.params.country, req.params.region)
|
putRegion(
|
||||||
|
res,
|
||||||
|
dbConn,
|
||||||
|
req.params.country,
|
||||||
|
req.params.region,
|
||||||
|
req.body.name
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
app.post("/regions", (req, res) => postRegion(res, dbConn));
|
|
||||||
|
|
||||||
// END OF ROUTING FOR REGIONS
|
// END OF ROUTING FOR REGIONS
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
|||||||
Reference in New Issue
Block a user