-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add endpoints for retrieving towns and exact town details
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
1 parent
35ee60b
commit 4267e47
Showing
5 changed files
with
4,070 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; | ||
} | ||
}; |
Oops, something went wrong.