Skip to content

Commit 113c22e

Browse files
committed
[add] remaining endPointGuide Route
1 parent b41f26a commit 113c22e

File tree

6 files changed

+123
-26
lines changed

6 files changed

+123
-26
lines changed

constants/constant.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@ export const availableContinents = [
1717
"SouthAmerica",
1818
];
1919

20-
export const availableSovereignties = [];
20+
export const availableSovereignties = [
21+
"Antarctic Treaty",
22+
"Australia",
23+
"British Crown",
24+
"China",
25+
"Denmark",
26+
"Disputed",
27+
"Finland",
28+
"France",
29+
"Netherlands",
30+
"New Zealand",
31+
"Norway",
32+
"UK",
33+
"UN",
34+
"UN member",
35+
"UN observer",
36+
"United Kingdom",
37+
"United States",
38+
"US",
39+
];

controller/countryController.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Request, Response } from "express";
22
import { Country } from "../types/country";
3-
import { availableContinents, sovereigntyMapping } from "../constants/constant";
3+
import {
4+
availableContinents,
5+
availableSovereignties,
6+
sovereigntyMapping,
7+
} from "../constants/constant";
48
const countries = require("../data/countries.json");
59

610
const getAllCountries = (_req: Request, res: Response) => {
@@ -24,7 +28,7 @@ const getCountriesByContinent = (req: Request, res: Response) => {
2428

2529
if (countriesByContinent.length == 0) {
2630
const apiContinentGuide = {
27-
message: "Continent Should be one of these",
31+
message: `${req.params.continentName} isn't a valid continent. It should be one of these`,
2832
availableContinents,
2933
};
3034
res.json(apiContinentGuide);
@@ -47,6 +51,13 @@ const getCountriesBySovereignty = (req: Request, res: Response) => {
4751
return standardizedSovereignty === sovereigntyInput;
4852
});
4953

54+
if (countriesBySovereignty.length < 1) {
55+
const apiSovereigntyGuide = {
56+
message: `${req.params.sovereignty} isn't a valid sovereignty. It should be one of the following`,
57+
availableSovereignties,
58+
};
59+
return res.status(400).json(apiSovereigntyGuide);
60+
}
5061
return res.json(countriesBySovereignty);
5162
};
5263

controller/endPointGuideController.ts

+56-20
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,78 @@ import {
33
availableContinents,
44
availableSovereignties,
55
} from "../constants/constant";
6+
import generateGuideResponse from "../helper/generateGuideResponse";
67

78
const index = (req: Request, res: Response) => {
89
const fullUrl = req.fullUrl;
910

1011
const apiIndexGuide = {
11-
allCountriesUrl: `${fullUrl}/all`,
12-
countriesByContinent: `${fullUrl}/continent/{continentName}`,
13-
countriesBySovereignty: `${fullUrl}/sovereignty/{sovereigntyName}`,
14-
countryByISONumericCode: `${fullUrl}/isocode/numeric/{ISONumericCode}`,
15-
countryByISOCode: `${fullUrl}/isocode/{ISOCode}`,
16-
countriesByCurrencyCode: `${fullUrl}/currency/{currencyCode}`,
17-
countryByTopLevelDomain: `${fullUrl}/topLevelDomain/{topLevelDomain}`,
18-
countriesByDialingCode: `${fullUrl}/dialingCode/{dialingCode}`,
12+
allCountriesUrl: `${fullUrl}/countries/all`,
13+
countriesByContinent: `${fullUrl}/countries/continent/{continentName}`,
14+
countriesBySovereignty: `${fullUrl}/countries/sovereignty/{sovereigntyName}`,
15+
countryByISONumericCode: `${fullUrl}/countries/isocode/numeric/{ISONumericCode}`,
16+
countryByISOCode: `${fullUrl}/countries/isocode/{ISOCode}`,
17+
countriesByCurrencyCode: `${fullUrl}/countries/currency/{currencyCode}`,
18+
countryByTopLevelDomain: `${fullUrl}/countries/topLevelDomain/{topLevelDomain}`,
19+
countriesByDialingCode: `${fullUrl}/countries/dialingCode/{dialingCode}`,
1920
};
2021

2122
res.json(apiIndexGuide);
2223
};
2324

24-
const continent = (_req: Request, res: Response) => {
25-
const apiContinentGuide = {
26-
message: "Continent should be one of the following",
27-
availableContinents,
28-
};
29-
res.status(404).json(apiContinentGuide);
25+
const continent = (req: Request, res: Response) => {
26+
generateGuideResponse(req, res, "continent", "continentName", "Continent");
3027
};
3128

32-
const sovereignty = (_req: Request, res: Response) => {
33-
const apiSovereigntyGuide = {
34-
message: "Sovereignty should be one of the following",
35-
availableSovereignties,
36-
};
37-
res.status(404).json(apiSovereigntyGuide);
29+
const sovereignty = (req: Request, res: Response) => {
30+
generateGuideResponse(
31+
req,
32+
res,
33+
"sovereignty",
34+
"sovereigntyName",
35+
"Sovereignty"
36+
);
37+
};
38+
39+
const ISONumericCode = (req: Request, res: Response) => {
40+
generateGuideResponse(
41+
req,
42+
res,
43+
"isocode/numeric",
44+
"ISONumericCode",
45+
"ISO Numeric Code"
46+
);
47+
};
48+
49+
const ISOCode = (req: Request, res: Response) => {
50+
generateGuideResponse(req, res, "isocode", "ISOCode", "ISOCode");
51+
};
52+
53+
const currencyCode = (req: Request, res: Response) => {
54+
generateGuideResponse(req, res, "currency", "ISOCode", "Currency Code");
55+
};
56+
57+
const topLevelDomain = (req: Request, res: Response) => {
58+
generateGuideResponse(
59+
req,
60+
res,
61+
"topLevelDomain",
62+
"topLevelDomain",
63+
"Top Level Domain"
64+
);
65+
};
66+
67+
const dialingCode = (req: Request, res: Response) => {
68+
generateGuideResponse(req, res, "dialingCode", "dialingCode", "Dialing Code");
3869
};
3970

4071
export default {
4172
index,
4273
continent,
4374
sovereignty,
75+
ISONumericCode,
76+
ISOCode,
77+
currencyCode,
78+
topLevelDomain,
79+
dialingCode,
4480
};

helper/generateGuideResponse.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Request, Response } from "express";
2+
3+
const generateGuideResponse = (
4+
req: Request,
5+
res: Response,
6+
path: string,
7+
paramName: string,
8+
urlName: string
9+
) => {
10+
const fullUrl = req.fullUrl;
11+
const guide = {
12+
message: `Invalid. ${paramName} url should be like down below`,
13+
url: `${fullUrl}/countries/${path}/{${paramName}}`,
14+
};
15+
res.status(404).json(guide);
16+
};
17+
18+
export default generateGuideResponse;

routes/endPointGuide.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const router = require("express").Router();
2+
import endPointGuide from "../controller/endPointGuideController";
3+
4+
router.get("/", endPointGuide.index);
5+
router.get("/countries", endPointGuide.index);
6+
router.get("/countries/continent", endPointGuide.continent);
7+
router.get("/countries/sovereignty", endPointGuide.sovereignty);
8+
router.get("/countries/isocode/numeric", endPointGuide.ISONumericCode);
9+
router.get("/countries/isocode", endPointGuide.ISOCode);
10+
router.get("/countries/currency", endPointGuide.currencyCode);
11+
router.get("/countries/topLevelDomain", endPointGuide.topLevelDomain);
12+
router.get("/countries/dialingCode", endPointGuide.topLevelDomain);
13+
14+
export default router;

routes/route.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const router = require("express").Router();
22
import countryRoutes from "./countryRoute";
3-
import endPointGuide from "../controller/endPointGuideController";
3+
import endPointRoutes from "./endPointGuide";
44

5-
// router.get("/", endPointGuide.index);
6-
// router.get("/continent", endPointGuide.continent);
5+
router.use("/", endPointRoutes);
76
router.use("/countries", countryRoutes);
87

98
export default router;

0 commit comments

Comments
 (0)