Skip to content

Commit

Permalink
[Some work behind the scenes] refactor: Update data files to include …
Browse files Browse the repository at this point in the history
…search filters for area and altitude

This commit updates the data files for Provinces, Districts, Towns, and Villages to include search filters for area and altitude. The new filters allow users to search for entities within a specific range of area or altitude. The code has been refactored to handle the new filters and validate the input values. If the provided values are invalid or out of range, appropriate error messages are thrown.

Closes #31
  • Loading branch information
ubeydeozdmr committed Sep 20, 2024
1 parent 85d3bd4 commit 092911a
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 8 deletions.
84 changes: 76 additions & 8 deletions src/v1/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ exports.getProvinces = (req, res) => {
name,
minPopulation,
maxPopulation,
minArea,
maxArea,
minAltitude,
maxAltitude,
isMetropolitan,
offset,
limit,
Expand All @@ -21,6 +25,10 @@ exports.getProvinces = (req, res) => {
name,
minPopulation,
maxPopulation,
minArea,
maxArea,
minAltitude,
maxAltitude,
isMetropolitan,
offset,
limit,
Expand Down Expand Up @@ -55,13 +63,28 @@ exports.getExactProvince = (req, res) => {

exports.getDistricts = (req, res) => {
try {
const { name, minPopulation, maxPopulation, offset, limit, fields, sort } =
req.query;
const {
name,
minPopulation,
maxPopulation,
minArea,
maxArea,
provinceId,
province,
offset,
limit,
fields,
sort,
} = req.query;

const districts = Districts.getDistricts(
name,
minPopulation,
maxPopulation,
minArea,
maxArea,
provinceId,
province,
offset,
limit,
fields,
Expand Down Expand Up @@ -95,13 +118,28 @@ exports.getExactDistrict = (req, res) => {

exports.getNeighborhoods = (req, res) => {
try {
const { name, minPopulation, maxPopulation, offset, limit, fields, sort } =
req.query;
const {
name,
minPopulation,
maxPopulation,
provinceId,
province,
districtId,
district,
offset,
limit,
fields,
sort,
} = req.query;

const neighborhood = Neighborhoods.getNeighborhoods(
name,
minPopulation,
maxPopulation,
provinceId,
province,
districtId,
district,
offset,
limit,
fields,
Expand Down Expand Up @@ -135,13 +173,28 @@ exports.getExactNeighborhood = (req, res) => {

exports.getVillages = (req, res) => {
try {
const { name, minPopulation, maxPopulation, offset, limit, fields, sort } =
req.query;
const {
name,
minPopulation,
maxPopulation,
provinceId,
province,
districtId,
district,
offset,
limit,
fields,
sort,
} = req.query;

const village = Villages.getVillages(
name,
minPopulation,
maxPopulation,
provinceId,
province,
districtId,
district,
offset,
limit,
fields,
Expand Down Expand Up @@ -175,13 +228,28 @@ exports.getExactVillage = (req, res) => {

exports.getTowns = (req, res) => {
try {
const { name, minPopulation, maxPopulation, offset, limit, fields, sort } =
req.query;
const {
name,
minPopulation,
maxPopulation,
provinceId,
province,
districtId,
district,
offset,
limit,
fields,
sort,
} = req.query;

const town = Towns.getTowns(
name,
minPopulation,
maxPopulation,
provinceId,
province,
districtId,
district,
offset,
limit,
fields,
Expand Down
59 changes: 59 additions & 0 deletions src/v1/data/Districts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ exports.getDistricts = function (
name,
minPopulation = 1,
maxPopulation = 1000000000,
minArea = 1,
maxArea = 1000000000,
provinceId,
province,
offset = 0,
limit = 973,
fields,
Expand Down Expand Up @@ -51,6 +55,61 @@ exports.getDistricts = function (
});
}

if (minArea || maxArea) {
if (+minArea <= 0 && +maxArea <= 0) {
throw {
status: 404,
message: "You can't search for a district with an area of 0 or less.",
};
}

if (+minArea > +maxArea) {
throw {
status: 404,
message: 'The minimum area cannot be greater than the maximum area.',
};
}

districts = districts.filter((item) => {
return item.area >= minArea && item.area <= maxArea;
});
}

if (provinceId || province) {
console.log(provinceId, province);
if (provinceId && province) {
throw {
status: 404,
message:
'You can only use one of the provinceId or province parameters.',
};
}

if (provinceId) {
if (!isFinite(provinceId)) {
throw {
status: 404,
message:
'Invalid province ID. The provinceId parameter must be a number.',
};
}

districts = districts.filter((item) => item.provinceId === +provinceId);
}

if (province) {
const provinceAlt =
province.charAt(0).toLocaleUpperCase('TR') +
province.slice(1).toLocaleLowerCase('tr');

districts = districts.filter(
(item) =>
item.province.includes(province) ||
item.province.includes(provinceAlt),
);
}
}

if (sort) {
const sortArray = sort.split(',').reverse();

Expand Down
76 changes: 76 additions & 0 deletions src/v1/data/Neighborhoods.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ exports.getNeighborhoods = function (
name,
minPopulation = 1,
maxPopulation = 1000000000,
provinceId,
province,
districtId,
district,
offset = 0,
limit,
fields,
Expand Down Expand Up @@ -49,6 +53,78 @@ exports.getNeighborhoods = function (
});
}

if (provinceId || province) {
if (provinceId && province) {
throw {
status: 404,
message:
'You can only use one of the provinceId or province parameters.',
};
}

if (provinceId) {
if (!isFinite(provinceId)) {
throw {
status: 404,
message:
'Invalid province ID. The provinceId parameter must be a number.',
};
}

neighborhoods = neighborhoods.filter(
(item) => item.provinceId === +provinceId,
);
}

if (province) {
const provinceAlt =
province.charAt(0).toLocaleUpperCase('TR') +
province.slice(1).toLocaleLowerCase('tr');

neighborhoods = neighborhoods.filter(
(item) =>
item.province.includes(province) ||
item.province.includes(provinceAlt),
);
}
}

if (districtId || district) {
if (districtId && district) {
throw {
status: 404,
message:
'You can only use one of the districtId or district parameters.',
};
}

if (districtId) {
if (!isFinite(districtId)) {
throw {
status: 404,
message:
'Invalid district ID. The districtId parameter must be a number.',
};
}

neighborhoods = neighborhoods.filter(
(item) => item.districtId === +districtId,
);
}

if (district) {
const districtAlt =
district.charAt(0).toLocaleUpperCase('TR') +
district.slice(1).toLocaleLowerCase('tr');

neighborhoods = neighborhoods.filter(
(item) =>
item.district.includes(district) ||
item.district.includes(districtAlt),
);
}
}

if (sort) {
const sortArray = sort.split(',').reverse();

Expand Down
46 changes: 46 additions & 0 deletions src/v1/data/Provinces.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ exports.getProvinces = function (
name,
minPopulation = 1,
maxPopulation = 1000000000,
minArea = 1,
maxArea = 1000000000,
minAltitude = 0,
maxAltitude = 10000,
isMetropolitan,
offset = 0,
limit = 81,
Expand Down Expand Up @@ -66,6 +70,48 @@ exports.getProvinces = function (
});
}

if (minArea || maxArea) {
if (+minArea <= 0 && +maxArea <= 0) {
throw {
status: 404,
message: "You can't search for a province with an area of 0 or less.",
};
}

if (+minArea > +maxArea) {
throw {
status: 404,
message: 'The minimum area cannot be greater than the maximum area.',
};
}

provinces = provinces.filter((item) => {
return item.area >= minArea && item.area <= maxArea;
});
}

if (minAltitude || maxAltitude) {
if (+minAltitude < 0 && +maxAltitude < 0) {
throw {
status: 404,
message:
"You can't search for a province with an altitude of less than 0.",
};
}

if (+minAltitude > +maxAltitude) {
throw {
status: 404,
message:
'The minimum altitude cannot be greater than the maximum altitude.',
};
}

provinces = provinces.filter((item) => {
return item.altitude >= minAltitude && item.altitude <= maxAltitude;
});
}

if (isMetropolitan) {
if (isMetropolitan === 'true') {
provinces = provinces.filter((item) => item.isMetropolitan === true);
Expand Down
Loading

0 comments on commit 092911a

Please sign in to comment.