Skip to content

Commit e2c0512

Browse files
committed
Improve bind routes on javascript-backend
1 parent 2e8ff89 commit e2c0512

19 files changed

+1019
-10
lines changed

backend-javascript/src/modules/city/city.controller.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { validateItem } from './city.schema.js';
55
class Controller {
66
constructor(service) {
77
this.service = service;
8-
this.getItems = this.getItems.bind(this);
9-
this.getItemById = this.getItemById.bind(this);
10-
this.createItem = this.createItem.bind(this);
11-
this.updateItem = this.updateItem.bind(this);
12-
this.deleteItem = this.deleteItem.bind(this);
8+
// this.getItems = this.getItems.bind(this);
9+
// this.getItemById = this.getItemById.bind(this);
10+
// this.createItem = this.createItem.bind(this);
11+
// this.updateItem = this.updateItem.bind(this);
12+
// this.deleteItem = this.deleteItem.bind(this);
1313
}
1414

15-
async getItems(req, res, next) {
15+
// async getItems(req, res, next) {
16+
getItems = async (req, res, next) => {
1617
try {
1718
const result = await this.service.getItems(req.query);
1819
res.locals = { data: result, statusCode: HTTP_STATUS.OK };
@@ -24,7 +25,7 @@ class Controller {
2425
}
2526
}
2627

27-
async getItemById(req, res, next) {
28+
getItemById = async (req, res, next) => {
2829
try {
2930
const result = await this.service.getItemById(parseInt(req.params.id));
3031
res.locals = { data: result, statusCode: HTTP_STATUS.OK };
@@ -48,7 +49,7 @@ class Controller {
4849
}
4950
}
5051

51-
async createItem(req, res, next) {
52+
createItem = async (req, res, next) => {
5253
try {
5354
validateItem(req.body);
5455
const result = await this.service.createItem(req.body);
@@ -67,7 +68,7 @@ class Controller {
6768
}
6869
}
6970

70-
async updateItem(req, res, next) {
71+
updateItem = async (req, res, next) => {
7172
try {
7273
validateItem(req.body);
7374
const result = await this.service.updateItem(parseInt(req.params.id), req.body);
@@ -86,7 +87,7 @@ class Controller {
8687
}
8788
}
8889

89-
async deleteItem(req, res, next) {
90+
deleteItem = async (req, res, next) => {
9091
try {
9192
const result = await this.service.deleteItem(parseInt(req.params.id));
9293
res.locals = { data: result, statusCode: HTTP_STATUS.OK };
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"paginationTotals": {
3+
"count": 7,
4+
"area": 150278011,
5+
"population": 6344901941,
6+
"countriesNumber": 682,
7+
"density": "42.22109"
8+
},
9+
"allTotals": {
10+
"count": 7,
11+
"area": 150278011,
12+
"population": 6344901941,
13+
"countriesNumber": 682,
14+
"density": "42.22109"
15+
},
16+
"continents": [
17+
{
18+
"id": 1016,
19+
"code": "AF44",
20+
"name": "Africa-pgbxcvbxc",
21+
"wikipediaLink": "Africa1111",
22+
"area": 30370011,
23+
"population": 451,
24+
"countriesNumber": 541,
25+
"density": 0.00001
26+
},
27+
{
28+
"id": 1017,
29+
"code": "AN",
30+
"name": "Antarctica-pg",
31+
"wikipediaLink": "Antarctica",
32+
"area": 14000000,
33+
"population": 4490,
34+
"countriesNumber": 0,
35+
"density": 0.00032
36+
},
37+
{
38+
"id": 1018,
39+
"code": "AS",
40+
"name": "Asia-pgdfghdfgh",
41+
"wikipediaLink": "Asia",
42+
"area": 44579000,
43+
"population": 4545133000,
44+
"countriesNumber": 47,
45+
"density": 101.95682
46+
},
47+
{
48+
"id": 1019,
49+
"code": "OC",
50+
"name": "Australia-pg",
51+
"wikipediaLink": "Australia_(continent)",
52+
"area": 8600000,
53+
"population": 41261000,
54+
"countriesNumber": 14,
55+
"density": 4.79779
56+
},
57+
{
58+
"id": 1020,
59+
"code": "EU",
60+
"name": "Europe-pg",
61+
"wikipediaLink": "Europe",
62+
"area": 10180000,
63+
"population": 742648000,
64+
"countriesNumber": 45,
65+
"density": 72.95167
66+
},
67+
{
68+
"id": 1021,
69+
"code": "NA",
70+
"name": "North America-pg",
71+
"wikipediaLink": "North_America",
72+
"area": 24709000,
73+
"population": 587615000,
74+
"countriesNumber": 23,
75+
"density": 23.78142
76+
},
77+
{
78+
"id": 1022,
79+
"code": "SA",
80+
"name": "South America-pg",
81+
"wikipediaLink": "South_America",
82+
"area": 17840000,
83+
"population": 428240000,
84+
"countriesNumber": 12,
85+
"density": 24.00448
86+
}
87+
]
88+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
WITH filtered_data AS (
2+
SELECT
3+
id,
4+
code,
5+
name,
6+
wikipedia_link AS wikipedia_link,
7+
CAST(area AS SIGNED) AS area,
8+
CAST(population AS DECIMAL(15, 2)) AS population,
9+
CAST(countries_number AS SIGNED) AS countries_number,
10+
ROUND(
11+
CAST(population AS DECIMAL(15, 2)) / NULLIF(CAST(area AS DECIMAL(15, 2)), 0),
12+
5
13+
) AS density
14+
FROM
15+
continent
16+
WHERE
17+
LOWER(name) LIKE '%a%'
18+
ORDER BY
19+
name ASC
20+
),
21+
pagination AS (
22+
SELECT
23+
id,
24+
code,
25+
name,
26+
wikipedia_link AS wikipediaLink,
27+
CAST(area AS SIGNED) AS area,
28+
CAST(population AS DECIMAL(15, 2)) AS population,
29+
CAST(countries_number AS SIGNED) AS countriesNumber,
30+
ROUND(
31+
CAST(population AS DECIMAL(15, 2)) / NULLIF(CAST(area AS DECIMAL(15, 2)), 0),
32+
5
33+
) AS density
34+
FROM
35+
filtered_data
36+
LIMIT
37+
5 OFFSET 0
38+
),
39+
totals AS (
40+
SELECT
41+
CAST(SUM(area) AS SIGNED) AS total_area_all,
42+
CAST(SUM(population) AS DECIMAL(15, 2)) AS total_population_all,
43+
CAST(SUM(countries_number) AS SIGNED) AS total_countries_number_all,
44+
COUNT(id) AS count_all,
45+
ROUND(
46+
CAST(SUM(population) AS DECIMAL(15, 2)) / NULLIF(CAST(SUM(area) AS DECIMAL(15, 2)), 0),
47+
5
48+
) AS average_density_all
49+
FROM
50+
filtered_data
51+
),
52+
totals_pagination AS (
53+
SELECT
54+
CAST(SUM(area) AS SIGNED) AS total_area,
55+
CAST(SUM(population) AS DECIMAL(15, 2)) AS total_population,
56+
CAST(SUM(countriesNumber) AS SIGNED) AS total_countries_number,
57+
COUNT(id) AS count,
58+
ROUND(
59+
CAST(SUM(population) AS DECIMAL(15, 2)) / NULLIF(CAST(SUM(area) AS DECIMAL(15, 2)), 0),
60+
5
61+
) AS average_density
62+
FROM
63+
pagination
64+
)
65+
SELECT
66+
(
67+
SELECT
68+
count_all
69+
FROM
70+
totals
71+
) AS count,
72+
(
73+
SELECT
74+
total_area_all
75+
FROM
76+
totals
77+
) AS area,
78+
(
79+
SELECT
80+
total_population_all
81+
FROM
82+
totals
83+
) AS population,
84+
(
85+
SELECT
86+
total_countries_number_all
87+
FROM
88+
totals
89+
) AS countriesNumber,
90+
(
91+
SELECT
92+
count
93+
FROM
94+
totals_pagination
95+
) AS countPagination,
96+
(
97+
SELECT
98+
total_area
99+
FROM
100+
totals_pagination
101+
) AS areaPagination,
102+
(
103+
SELECT
104+
total_population
105+
FROM
106+
totals_pagination
107+
) AS populationPagination,
108+
(
109+
SELECT
110+
total_countries_number
111+
FROM
112+
totals_pagination
113+
) AS countriesNumberPagination,
114+
(
115+
SELECT
116+
average_density_all
117+
FROM
118+
totals
119+
) AS density,
120+
(
121+
SELECT
122+
average_density
123+
FROM
124+
totals_pagination
125+
) AS densityPagination,
126+
JSON_ARRAYAGG(
127+
JSON_OBJECT(
128+
'id',
129+
pagination.id,
130+
'code',
131+
pagination.code,
132+
'name',
133+
pagination.name,
134+
'wikipediaLink',
135+
pagination.wikipediaLink,
136+
'area',
137+
pagination.area,
138+
'population',
139+
pagination.population,
140+
'countriesNumber',
141+
pagination.countriesNumber,
142+
'density',
143+
pagination.density
144+
)
145+
) AS continents
146+
FROM
147+
pagination;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DROP DATABASE IF EXISTS backend_javascript;
2+
3+
CREATE DATABASE backend_javascript;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
CREATE TABLE continent (
2+
id MEDIUMINT NOT NULL AUTO_INCREMENT,
3+
code CHAR(10) NOT NULL,
4+
name CHAR(100) NOT NULL,
5+
wikipedia_link CHAR(100) DEFAULT "",
6+
area INT DEFAULT 0,
7+
population BIGINT DEFAULT 0,
8+
countries_number INT DEFAULT 0,
9+
PRIMARY KEY (id)
10+
);
11+
12+
ALTER TABLE
13+
continent AUTO_INCREMENT = 1000;
14+
15+
CREATE TABLE country (
16+
id MEDIUMINT NOT NULL AUTO_INCREMENT,
17+
name CHAR(50) NOT NULL,
18+
wikipedia_link CHAR(50) DEFAULT "",
19+
continent_id INT,
20+
iso_numeric CHAR(50),
21+
iso_alpha2 CHAR(50),
22+
iso_alpha3 CHAR(50),
23+
flag CHAR(50),
24+
PRIMARY KEY (id)
25+
);
26+
27+
ALTER TABLE
28+
country AUTO_INCREMENT = 1000;
29+
30+
CREATE TABLE city (
31+
id MEDIUMINT NOT NULL AUTO_INCREMENT,
32+
name CHAR(50) NOT NULL,
33+
wikipedia_link CHAR(50) DEFAULT "",
34+
country_id INTEGER,
35+
capital BOOLEAN DEFAULT false,
36+
PRIMARY KEY (id)
37+
);
38+
39+
ALTER TABLE
40+
city AUTO_INCREMENT = 1000;
41+
42+
CREATE TABLE person (
43+
id MEDIUMINT NOT NULL AUTO_INCREMENT,
44+
name CHAR(100) NOT NULL,
45+
wikipedia_link CHAR(100) NOT NULL,
46+
birth_date DATE,
47+
birth_city_id INT,
48+
death_date DATE,
49+
death_city_id INT,
50+
gender_id INT,
51+
image CHAR(50),
52+
PRIMARY KEY (id)
53+
);
54+
55+
ALTER TABLE
56+
person AUTO_INCREMENT = 1000;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DROP TABLE continent;
2+
3+
DROP TABLE country;
4+
5+
DROP TABLE city;
6+
7+
DROP TABLE person;

0 commit comments

Comments
 (0)