Skip to content

Commit

Permalink
feat(country-data): Add country data service
Browse files Browse the repository at this point in the history
  • Loading branch information
glecko committed Apr 10, 2021
1 parent ca05a9c commit b79d936
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CountryDataServiceModule } from './services/country-data/country-data.module';

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
AppRoutingModule,
CountryDataServiceModule
],
providers: [],
bootstrap: [
Expand Down
26 changes: 26 additions & 0 deletions src/app/services/country-data/country-data.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const COUNTRY_DATA_ENDPOINT = 'http://api.geonames.org/countryInfoJSON?formatted=true&username=hydrane';

export interface CountryDataResponseModel {
geonames: CountryDataModel[];
}

export interface CountryDataModel {
continent: string;
capital: string;
languages: string;
geonameId: number;
isoAlpha3: string;
fipsCode: string;
population: string;
north: number;
south: number;
east: number;
west: number;
isoNumeric: string;
areaInSqKm: string;
countryCode: string;
countryName: string;
continentName: string;
currencyCode: string;
}

13 changes: 13 additions & 0 deletions src/app/services/country-data/country-data.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NgModule } from '@angular/core';
import { CountryDataService } from './country-data.service';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
imports: [
HttpClientModule,
],
providers: [
CountryDataService,
],
})
export class CountryDataServiceModule { }
32 changes: 32 additions & 0 deletions src/app/services/country-data/country-data.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CountryDataResponseModel, COUNTRY_DATA_ENDPOINT, CountryDataModel } from './country-data.model';

@Injectable()
export class CountryDataService {

constructor(
public http: HttpClient,
) {}

private static getUniqueAttributeValues(countriesData: CountryDataModel[], attributeName: string): string[] {
return [ ...new Set(countriesData.map((country: CountryDataModel) => country[attributeName])) ];
}

private static sortAlphabeticallyIgnoreCase(list: string[]): string[] {
return list.sort((a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase()));
}

async fetchData(): Promise<{ countriesData: CountryDataModel[], uniqueContinents: string[] }> {
const responseData = await this.fetchCountriesData();
const uniqueContinents = CountryDataService.getUniqueAttributeValues(responseData.geonames, 'continentName');
return {
countriesData: responseData.geonames,
uniqueContinents: CountryDataService.sortAlphabeticallyIgnoreCase(uniqueContinents),
};
}

private fetchCountriesData(): Promise<CountryDataResponseModel> {
return this.http.get<CountryDataResponseModel>(COUNTRY_DATA_ENDPOINT).toPromise();
}
}

0 comments on commit b79d936

Please sign in to comment.