Skip to content

Commit

Permalink
Merge pull request #6 from Dashbrd/add-service-layer
Browse files Browse the repository at this point in the history
added service layer
  • Loading branch information
ankeshdave authored Jan 1, 2019
2 parents f985dad + da9c190 commit 03d3321
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 263 deletions.
15 changes: 8 additions & 7 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -18,7 +18,8 @@ import { CharacterSearchComponent } from './character-search/character-search.co
],
imports: [
BrowserModule,
AppRoutingModule
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
Expand Down
8 changes: 8 additions & 0 deletions src/app/models/api-response.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { StarWarsCharacter } from './star-wars-character.model';

export interface ApiResponse {
count: number;
next: string;
previous?: any;
results: StarWarsCharacter[];
}
12 changes: 12 additions & 0 deletions src/app/services/swapi.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
17 changes: 17 additions & 0 deletions src/app/services/swapi.service.ts
Original file line number Diff line number Diff line change
@@ -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<ApiResponse>('https://swapi.co/api/people');
}
}
265 changes: 9 additions & 256 deletions src/app/sw-people-list/sw-people-list.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -11,264 +12,16 @@ export class SwPeopleListComponent implements OnInit {

swCharacters: Array<StarWarsCharacter>;

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 = [];
});
}

/**
Expand Down

0 comments on commit 03d3321

Please sign in to comment.