diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 9c018ad..edc842c 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,12 +1,12 @@ -import { BrowserModule } from '@angular/platform-browser'; -import { NgModule } from '@angular/core'; - -import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; +import { AppRoutingModule } from './app-routing.module'; +import { BrowserModule } from '@angular/platform-browser'; +import { CharacterSearchComponent } from './character-search/character-search.component'; import { HeaderComponent } from './header/header.component'; -import { SwPeopleListComponent } from './sw-people-list/sw-people-list.component'; +import { HttpClientModule } from '@angular/common/http'; +import { NgModule } from '@angular/core'; import { SwCharacterComponent } from './sw-character/sw-character.component'; -import { CharacterSearchComponent } from './character-search/character-search.component'; +import { SwPeopleListComponent } from './sw-people-list/sw-people-list.component'; @NgModule({ declarations: [ @@ -18,7 +18,8 @@ import { CharacterSearchComponent } from './character-search/character-search.co ], imports: [ BrowserModule, - AppRoutingModule + AppRoutingModule, + HttpClientModule ], providers: [], bootstrap: [AppComponent] diff --git a/src/app/models/api-response.model.ts b/src/app/models/api-response.model.ts new file mode 100644 index 0000000..9d7ae34 --- /dev/null +++ b/src/app/models/api-response.model.ts @@ -0,0 +1,8 @@ +import { StarWarsCharacter } from './star-wars-character.model'; + +export interface ApiResponse { + count: number; + next: string; + previous?: any; + results: StarWarsCharacter[]; +} diff --git a/src/app/services/swapi.service.spec.ts b/src/app/services/swapi.service.spec.ts new file mode 100644 index 0000000..ac9f827 --- /dev/null +++ b/src/app/services/swapi.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { SwapiService } from './swapi.service'; + +describe('SwapiService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: SwapiService = TestBed.get(SwapiService); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/swapi.service.ts b/src/app/services/swapi.service.ts new file mode 100644 index 0000000..c9d6243 --- /dev/null +++ b/src/app/services/swapi.service.ts @@ -0,0 +1,17 @@ +import { ApiResponse } from '../models/api-response.model'; +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class SwapiService { + + constructor(private httpClient: HttpClient) { + + } + + getPeople() { + return this.httpClient.get('https://swapi.co/api/people'); + } +} diff --git a/src/app/sw-people-list/sw-people-list.component.ts b/src/app/sw-people-list/sw-people-list.component.ts index ddce4b5..e57ddda 100644 --- a/src/app/sw-people-list/sw-people-list.component.ts +++ b/src/app/sw-people-list/sw-people-list.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { StarWarsCharacter } from '../models/star-wars-character.model'; +import { SwapiService } from '../services/swapi.service'; @Component({ selector: 'app-sw-people-list', @@ -11,264 +12,16 @@ export class SwPeopleListComponent implements OnInit { swCharacters: Array; - constructor() { } + constructor(private api: SwapiService) { + } ngOnInit() { - - this.swCharacters = [ - { - 'name': 'Luke Skywalker', - 'height': '172', - 'mass': '77', - 'hair_color': 'blond', - 'skin_color': 'fair', - 'eye_color': 'blue', - 'birth_year': '19BBY', - 'gender': 'male', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/2/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/3/', - 'https://swapi.co/api/films/1/', - 'https://swapi.co/api/films/7/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [ - 'https://swapi.co/api/vehicles/14/', - 'https://swapi.co/api/vehicles/30/' - ], - 'starships': [ - 'https://swapi.co/api/starships/12/', - 'https://swapi.co/api/starships/22/' - ], - 'url': 'https://swapi.co/api/people/1/' - }, - { - 'name': 'C-3PO', - 'height': '167', - 'mass': '75', - 'hair_color': 'n/a', - 'skin_color': 'gold', - 'eye_color': 'yellow', - 'birth_year': '112BBY', - 'gender': 'n/a', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/2/', - 'https://swapi.co/api/films/5/', - 'https://swapi.co/api/films/4/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/3/', - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/2/' - ], - 'vehicles': [], - 'starships': [], - 'url': 'https://swapi.co/api/people/2/' - }, - { - 'name': 'R2-D2', - 'height': '96', - 'mass': '32', - 'hair_color': 'n/a', - 'skin_color': 'white, blue', - 'eye_color': 'red', - 'birth_year': '33BBY', - 'gender': 'n/a', - 'homeworld': 'https://swapi.co/api/planets/8/', - 'films': [ - 'https://swapi.co/api/films/2/', - 'https://swapi.co/api/films/5/', - 'https://swapi.co/api/films/4/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/3/', - 'https://swapi.co/api/films/1/', - 'https://swapi.co/api/films/7/' - ], - 'species': [ - 'https://swapi.co/api/species/2/' - ], - 'vehicles': [], - 'starships': [], - 'url': 'https://swapi.co/api/people/3/' - }, - { - 'name': 'Darth Vader', - 'height': '202', - 'mass': '136', - 'hair_color': 'none', - 'skin_color': 'white', - 'eye_color': 'yellow', - 'birth_year': '41.9BBY', - 'gender': 'male', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/2/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/3/', - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [], - 'starships': [ - 'https://swapi.co/api/starships/13/' - ], - 'url': 'https://swapi.co/api/people/4/' - }, - { - 'name': 'Leia Organa', - 'height': '150', - 'mass': '49', - 'hair_color': 'brown', - 'skin_color': 'light', - 'eye_color': 'brown', - 'birth_year': '19BBY', - 'gender': 'female', - 'homeworld': 'https://swapi.co/api/planets/2/', - 'films': [ - 'https://swapi.co/api/films/2/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/3/', - 'https://swapi.co/api/films/1/', - 'https://swapi.co/api/films/7/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [ - 'https://swapi.co/api/vehicles/30/' - ], - 'starships': [], - 'url': 'https://swapi.co/api/people/5/' - }, - { - 'name': 'Owen Lars', - 'height': '178', - 'mass': '120', - 'hair_color': 'brown, grey', - 'skin_color': 'light', - 'eye_color': 'blue', - 'birth_year': '52BBY', - 'gender': 'male', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/5/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [], - 'starships': [], - 'url': 'https://swapi.co/api/people/6/' - }, - { - 'name': 'Beru Whitesun lars', - 'height': '165', - 'mass': '75', - 'hair_color': 'brown', - 'skin_color': 'light', - 'eye_color': 'blue', - 'birth_year': '47BBY', - 'gender': 'female', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/5/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [], - 'starships': [], - 'url': 'https://swapi.co/api/people/7/' - }, - { - 'name': 'R5-D4', - 'height': '97', - 'mass': '32', - 'hair_color': 'n/a', - 'skin_color': 'white, red', - 'eye_color': 'red', - 'birth_year': 'unknown', - 'gender': 'n/a', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/2/' - ], - 'vehicles': [], - 'starships': [], - 'url': 'https://swapi.co/api/people/8/' - }, - { - 'name': 'Biggs Darklighter', - 'height': '183', - 'mass': '84', - 'hair_color': 'black', - 'skin_color': 'light', - 'eye_color': 'brown', - 'birth_year': '24BBY', - 'gender': 'male', - 'homeworld': 'https://swapi.co/api/planets/1/', - 'films': [ - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [], - 'starships': [ - 'https://swapi.co/api/starships/12/' - ], - 'url': 'https://swapi.co/api/people/9/' - }, - { - 'name': 'Obi-Wan Kenobi', - 'height': '182', - 'mass': '77', - 'hair_color': 'auburn, white', - 'skin_color': 'fair', - 'eye_color': 'blue-gray', - 'birth_year': '57BBY', - 'gender': 'male', - 'homeworld': 'https://swapi.co/api/planets/20/', - 'films': [ - 'https://swapi.co/api/films/2/', - 'https://swapi.co/api/films/5/', - 'https://swapi.co/api/films/4/', - 'https://swapi.co/api/films/6/', - 'https://swapi.co/api/films/3/', - 'https://swapi.co/api/films/1/' - ], - 'species': [ - 'https://swapi.co/api/species/1/' - ], - 'vehicles': [ - 'https://swapi.co/api/vehicles/38/' - ], - 'starships': [ - 'https://swapi.co/api/starships/48/', - 'https://swapi.co/api/starships/59/', - 'https://swapi.co/api/starships/64/', - 'https://swapi.co/api/starships/65/', - 'https://swapi.co/api/starships/74/' - ], - 'url': 'https://swapi.co/api/people/10/' - } - ]; - + this.api.getPeople().subscribe(response => { + this.swCharacters = response.results; + }, (error) => { + console.log(error); + this.swCharacters = []; + }); } /**