landing page finished

This commit is contained in:
2024-07-29 21:23:26 +02:00
parent 6348af8862
commit 55e800009e
8 changed files with 164 additions and 54 deletions

View File

@@ -10,6 +10,10 @@ export class GeoController {
findByPrefix(@Param('prefix') prefix: string): any {
return this.geoService.findCitiesStartingWith(prefix);
}
@Get('citiesandstates/:prefix')
findByCitiesAndStatesByPrefix(@Param('prefix') prefix: string): any {
return this.geoService.findCitiesAndStatesStartingWith(prefix);
}
@Get(':prefix/:state')
findByPrefixAndState(@Param('prefix') prefix: string, @Param('state') state: string): any {

View File

@@ -61,6 +61,43 @@ export class GeoService {
});
return state ? result.filter(e => e.state_code.toLowerCase() === state.toLowerCase()) : result;
}
findCitiesAndStatesStartingWith(prefix: string, state?: string): Array<{ id: string; name: string; type: 'city' | 'state'; state_code: string }> {
const results: Array<{ id: string; name: string; type: 'city' | 'state'; state_code: string }> = [];
const lowercasePrefix = prefix.toLowerCase();
//for (const country of this.geo) {
// Suche nach passenden Staaten
for (const state of this.geo.states) {
if (state.name.toLowerCase().startsWith(lowercasePrefix)) {
results.push({
id: state.id.toString(),
name: state.name,
type: 'state',
state_code: state.state_code,
});
}
// Suche nach passenden Städten
for (const city of state.cities) {
if (city.name.toLowerCase().startsWith(lowercasePrefix)) {
results.push({
id: city.id.toString(),
name: city.name,
type: 'city',
state_code: state.state_code,
});
}
}
//}
}
return results.sort((a, b) => {
if (a.type === 'state' && b.type === 'city') return -1;
if (a.type === 'city' && b.type === 'state') return 1;
return a.name.localeCompare(b.name);
});
}
getCityWithCoords(state: string, city: string): City {
return this.geo.states.find(s => s.state_code === state).cities.find(c => c.name === city);
}