Skip to content

Commit

Permalink
feat: Add endpoints for retrieving towns and exact town details (#31)
Browse files Browse the repository at this point in the history
The code changes include adding new endpoints in the controller.js file to handle requests for retrieving towns and exact town details. The getTowns endpoint retrieves a list of towns based on various query parameters such as name, population, offset, limit, fields, and sort. The getExactTown endpoint retrieves the details of a specific town based on its ID.

This commit message follows the conventional format of "feat: <description>" to indicate the addition of a new feature.
  • Loading branch information
ubeydeozdmr committed Aug 11, 2024
1 parent 35ee60b commit 1711542
Show file tree
Hide file tree
Showing 5 changed files with 4,070 additions and 8 deletions.
41 changes: 41 additions & 0 deletions src/v1/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Provinces = require('./data/Provinces');
const Districts = require('./data/Districts');
const Neighborhoods = require('./data/Neighborhoods');
const Villages = require('./data/Villages');
const Towns = require('./data/Towns');

exports.getProvinces = (req, res) => {
try {
Expand Down Expand Up @@ -171,3 +172,43 @@ exports.getExactVillage = (req, res) => {
});
}
};

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

const town = Towns.getTowns(
name,
minPopulation,
maxPopulation,
offset,
limit,
fields,
sort,
);

return res.send({ status: 'OK', data: town });
} catch (error) {
res.status(error?.status || 500).send({
status: 'ERROR',
error: error?.message || 'Internal Server Error',
});
}
};

exports.getExactTown = (req, res) => {
try {
const { id } = req.params;
const { fields } = req.query;

const town = Towns.getExactTown(id, fields);

return res.send({ status: 'OK', data: town });
} catch (error) {
res.status(error?.status || 500).send({
status: 'ERROR',
error: error?.message || 'Internal Server Error',
});
}
};
205 changes: 205 additions & 0 deletions src/v1/data/Towns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
const data = require('./towns.json');

exports.getTowns = function (
name,
minPopulation = 1,
maxPopulation = 1000000000,
offset = 0,
limit,
fields,
sort,
) {
try {
let towns = data;

if (!Object.values(arguments).some((item) => item)) {
return towns.slice(+offset, +offset + 1000);
}

if (name) {
nameAlt =
name.charAt(0).toLocaleUpperCase('TR') +
name.slice(1).toLocaleLowerCase('tr');
towns = towns.filter(
(item) => item.name.includes(name) || item.name.includes(nameAlt),
);
}

if (minPopulation || maxPopulation) {
if (+minPopulation <= 0 && +maxPopulation <= 0) {
throw {
status: 404,
message:
"You can't search for a town with a population of 0 or less.",
};
}

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

towns = towns.filter((item) => {
return (
item.population >= minPopulation && item.population <= maxPopulation
);
});
}

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

sortArray.forEach((item) => {
if (item !== 'name' && item !== '-name') {
if (item.startsWith('-')) {
const field = item.slice(1);
towns.sort((a, b) => (a[field] > b[field] ? -1 : 1));
} else {
towns.sort((a, b) => (a[item] > b[item] ? 1 : -1));
}
} else {
if (item.startsWith('-')) {
const field = item.slice(1);
towns.sort((a, b) =>
b[field].localeCompare(a[field], 'tr', { sensitivity: 'base' }),
);
} else {
towns.sort((a, b) =>
a[item].localeCompare(b[item], 'tr', { sensitivity: 'base' }),
);
}
}
});

if (
sortArray.some((item) => !Object.keys(data[0]).includes(item)) &&
!sortArray.some(
(item) => !Object.keys(data[0]).includes(item.startsWith('-')),
)
) {
throw {
status: 404,
message:
'Invalid sort. The sort parameter must be a comma-separated list of valid fields.',
};
}
}

if (fields) {
const fieldsArray = fields.split(',');
const filteredTowns = [];

towns.forEach((item) => {
const filteredTown = {};
fieldsArray.forEach((field) => {
filteredTown[field] = item[field];
});
filteredTowns.push(filteredTown);
});

towns = filteredTowns;

if (fieldsArray.some((item) => !Object.keys(data[0]).includes(item))) {
throw {
status: 404,
message:
'Invalid fields. The fields parameter must be a comma-separated list of valid fields.',
};
}
}

if (!limit) {
limit = 1000;
}

if (limit > 1000) {
throw {
status: 404,
message: 'The limit value must be less than or equal to 1000.',
};
}
towns = towns.slice(+offset, +offset + +limit);

if (+offset >= data.length && +limit == 0) {
throw {
status: 404,
message:
'Invalid offset and limit. The limit value must be greater than 0. The offset value must be less than ' +
data.length,
};
} else if (+offset >= data.length) {
throw {
status: 404,
message:
'Invalid offset. The offset value must be less than ' + data.length,
};
} else if (+limit == 0) {
throw {
status: 404,
message: 'Invalid limit. The limit value must be greater than 0.',
};
}

if (towns.length > 0) {
return towns;
} else {
throw {
status: 404,
message: 'No town found.',
};
}
} catch (error) {
throw {
status: error?.status || 500,
message: error?.message || 'Internal Server Error',
};
}
};

exports.getExactTown = function (id, fields) {
try {
if (!isFinite(id)) {
throw {
status: 404,
message: 'Invalid town ID. The id parameter must be a number.',
};
}

const town = data.find((item) => item.id === +id);

if (fields) {
const fieldsArray = fields.split(',');
const filteredTown = {};

fieldsArray.forEach((field) => {
filteredTown[field] = town[field];
});

if (fieldsArray.some((item) => !Object.keys(data[0]).includes(item))) {
throw {
status: 404,
message:
'Invalid fields. The fields parameter must be a comma-separated list of valid fields.',
};
}
return filteredTown;
} else {
if (town) {
return town;
} else {
throw {
status: 404,
message: 'No town found.',
};
}
}
} catch (error) {
throw {
status: error?.status || 500,
message: error?.message || 'Internal Server Error',
};
}
};
Loading

0 comments on commit 1711542

Please sign in to comment.