-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
125 additions
and
25 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
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 |
---|---|---|
@@ -1,24 +1,71 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Headers, Http } from '@angular/http'; | ||
|
||
import 'rxjs/add/operator/toPromise'; | ||
|
||
import { Hero } from './hero'; | ||
import { HEROES } from './mock-heroes'; | ||
|
||
|
||
@Injectable() | ||
export class HeroService { | ||
|
||
private heroesUrl = 'api/heroes'; | ||
|
||
constructor(private http: Http) {} | ||
|
||
getHeroes(): Promise<Hero[]> { | ||
return Promise.resolve(HEROES); | ||
return this.http.get(this.heroesUrl) | ||
.toPromise() | ||
.then(response => response.json().data as Hero[]) | ||
.catch(this.handleError); | ||
|
||
} | ||
|
||
getHeroesSlowly(): Promise<Hero[]> { | ||
return new Promise(resolve => { | ||
// Simulate server latency with 2 second delay | ||
setTimeout(() => resolve(this.getHeroes()), 2000); | ||
}); | ||
private handleError(error: any): Promise<any> { | ||
console.log('An error occurred', error); | ||
return Promise.reject(error.message || error); | ||
} | ||
|
||
// getHeroesSlowly(): Promise<Hero[]> { | ||
// return new Promise(resolve => { | ||
// // Simulate server latency with 2 second delay | ||
// setTimeout(() => resolve(this.getHeroes()), 2000); | ||
// }); | ||
// } | ||
|
||
getHero(id: number): Promise<Hero> { | ||
return this.getHeroes() | ||
.then(heroes => heroes.find(hero => hero.id === id)); | ||
const url = `${this.heroesUrl}/${id}`; | ||
return this.http.get(url) | ||
.toPromise() | ||
.then(response => response.json().data as Hero) | ||
.catch(this.handleError); | ||
} | ||
|
||
private headers = new Headers({'Content-Type': 'application/json'}); | ||
|
||
update(hero: Hero): Promise<Hero> { | ||
const url = `${this.heroesUrl}/${hero.id}`; | ||
return this.http | ||
.put(url, JSON.stringify(hero), {headers: this.headers}) | ||
.toPromise() | ||
.then(() => hero) | ||
.catch(this.handleError); | ||
} | ||
|
||
create(name: string): Promise<Hero> { | ||
return this.http | ||
.post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers}) | ||
.toPromise() | ||
.then(res => res.json().data as Hero) | ||
.catch(this.handleError); | ||
} | ||
|
||
delete(id: number): Promise<void> { | ||
const url = `${this.heroesUrl}/${id}`; | ||
return this.http.delete(url, {headers: this.headers}) | ||
.toPromise() | ||
.then(() => null) | ||
.catch(this.handleError); | ||
} | ||
|
||
} |
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
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,19 @@ | ||
import { InMemoryDbService } from 'angular-in-memory-web-api'; | ||
export class InMemoryDataService implements InMemoryDbService { | ||
createDb() { | ||
const heroes = [ | ||
{ id: 0, name: 'Zero' }, | ||
{ id: 11, name: 'Mr. Nice' }, | ||
{ id: 12, name: 'Narco' }, | ||
{ id: 13, name: 'Bombasto' }, | ||
{ id: 14, name: 'Celeritas' }, | ||
{ id: 15, name: 'Magneta' }, | ||
{ id: 16, name: 'RubberMan' }, | ||
{ id: 17, name: 'Dynama' }, | ||
{ id: 18, name: 'Dr IQ' }, | ||
{ id: 19, name: 'Magma' }, | ||
{ id: 20, name: 'Tornado' } | ||
]; | ||
return {heroes}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.