diff --git a/chapter04/src/restful_regions.js b/chapter04/src/restful_regions.js index d1d8da7..7637d0c 100644 --- a/chapter04/src/restful_regions.js +++ b/chapter04/src/restful_regions.js @@ -7,40 +7,38 @@ const getRegion = async ( res: any, dbConn: any, 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 { + 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); res .status(200) @@ -57,22 +55,21 @@ const deleteRegion = async ( country: string, region: string ) => { - const sqlCities = ` - SELECT 1 FROM cities - WHERE countryCode="${country} - AND regionCode="${region} - LIMIT 1" - `; - try { + const sqlCities = ` + SELECT 1 FROM cities + WHERE countryCode="${country}" + AND regionCode="${region}" + LIMIT 1" + `; const cities = await dbConn.query(sqlCities); if (cities.length > 0) { res.status(403).send("Cannot delete a region with cities"); } else { const deleteRegion = ` DELETE FROM regions - WHERE countryCode="${country} - AND regionCode="${region} + WHERE countryCode="${country}" + AND regionCode="${region}" `; const result = await dbConn.query(deleteRegion); @@ -92,14 +89,58 @@ const putRegion = async ( res: any, dbConn: any, country: string, - id: string, - region: any + region: string, + 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) => { - res.status(200).send("NOTHING DOING NOW..."); -}; - -module.exports = { getRegion, putRegion, deleteRegion, postRegion }; +module.exports = { getRegion, deleteRegion, putRegion }; diff --git a/chapter04/src/restful_server.js b/chapter04/src/restful_server.js index 30e137b..be37f72 100644 --- a/chapter04/src/restful_server.js +++ b/chapter04/src/restful_server.js @@ -13,8 +13,7 @@ const dbConn = require("./restful_db.js"); const { getRegion, deleteRegion, - putRegion, - postRegion + putRegion } = require("./restful_regions.js"); const SECRET_JWT_KEY = "modernJSbook"; @@ -77,11 +76,15 @@ app.delete("/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 // eslint-disable-next-line no-unused-vars