Handling GETs

As we saw previously, there are three possible routes to handle:

  • /regions to get all regions of all countries
  • /regions/UY to get all regions of a given country—in this case, Uruguay (UY)
  • /regions/UY/11 to get a specific region of a country—here, region 11 of Uruguay

We can handle all three cases in a similar way by just changing the SQL SELECT we'll be doing. Handling the results, however, will require a special case, as we'll note in the following code:

// Source file: src/restful_regions.js

const getRegion = async (
res: any,
dbConn: any,
country: ?string,
region: ?string
) => {
try {
res.set("Connection", "close");

let sqlQuery = "";
let regions;
if (country == null) {
sqlQuery = `
SELECT rr.*
FROM regions rr
JOIN countries cc
ON cc.countryCode=rr.countryCode
ORDER BY cc.countryCode, rr.regionCode
`;
regions = await dbConn.query(sqlQuery);
} else if (region == null) {
sqlQuery = `
SELECT 1
FROM countries
WHERE countryCode=?
`;

const countries = await dbConn.query(sqlQuery, [country]);
if (countries.length === 0) {
res.status(404).send("Country not found");
return;
}

sqlQuery = `
SELECT rr.*
FROM regions rr
JOIN countries cc
ON cc.countryCode=rr.countryCode
WHERE rr.countryCode=?
ORDER BY rr.regionCode
`;
regions = await dbConn.query(sqlQuery, [country]);
} else {
sqlQuery = `
SELECT rr.*
FROM regions rr
JOIN countries cc
ON cc.countryCode=rr.countryCode
WHERE rr.countryCode=?
AND rr.regionCode=?
`;
regions = await dbConn.query(sqlQuery, [country, region]);
}

if (regions.length > 0 || region === null) {
res.status(200)
.set("Content-Type", "application/json")
.send(JSON.stringify(regions));
} else {
res.status(404).send("Not found");
}
} catch (e) {
res.status(500).send("Server error");
}
};

The special case we mentioned in the preceding code is asking for something like /regions/XYZZY, and providing a wrong country code. In this case, instead of sending an empty set (which could imply that the country does exist, as it doesn't seem to have any regions) we can send a 404, so the second if statement (country provided, region absent) does a special check before proceeding.

We can see this code working with several examples. Getting /regions with no further parameter provides a largish output (22 MB), so adding parameters to allow for filtering or paging could be in order:

I removed the HTTPS, CORS, and mainly the JWT code from the server to make the examples simpler to follow. Doing this meant that I haven't received extra headers, and have avoided having to provide a JWT in each call. Yes, I cheated a bit, but the source code provided with the book includes everything, so don't worry about it!

> curl localhost:8080/regions/
[{"countryCode":"AD", "regionCode":"2", "regionName":"Canillo"}, {"countryCode":"AD", "regionCode":"3", "regionName":"Encamp"}, {"countryCode":"AD", "regionCode":"4", "regionName":"La Massana"},
.
.
.
{"countryCode":"ZW", "regionCode":"7", "regionName":"Matabeleland South"}, {"countryCode":"ZW", "regionCode":"8", "regionName":"Masvingo"}, {"countryCode":"ZW", "regionCode":"9", "regionName":"Bulawayo"}]

A request for a specific country (such as /regions/UY) produces an answer very much like the one that we received previously, but including only the regions in the country UY (Uruguay) and a request for a single region gets a single object:

> curl localhost:8080/regions/uy/10 
[{"countryCode":"UY","regionCode":"10","regionName":"Montevideo"}]

Finally, we can try for an error; check out the following screenshot and note the 404 status:

 Asking our RESTful server for regions in a non-existent country produces a 404 error
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.2.184