This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
forked from johnpapa/angular-tour-of-heroes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed the conversion of Observables to Promises.
- Loading branch information
Showing
4 changed files
with
53 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,74 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Headers, Http, Response } from '@angular/http'; | ||
import {Injectable} from '@angular/core'; | ||
import {Headers, Http} from '@angular/http'; | ||
|
||
import 'rxjs/add/operator/toPromise'; | ||
|
||
import { Hero } from './hero'; | ||
import {Hero} from './hero'; | ||
import {Observable} from 'rxjs/Observable'; | ||
|
||
@Injectable() | ||
export class HeroService { | ||
private heroesUrl = 'app/heroes'; // URL to web api | ||
|
||
constructor(private http: Http) { } | ||
|
||
getHeroes(): Promise<Array<Hero>> { | ||
getHeroes(): Observable<Hero[]> { | ||
return this.http | ||
.get(this.heroesUrl) | ||
.toPromise() | ||
.then((response) => { | ||
return response.json().data as Hero[]; | ||
}) | ||
.catch(this.handleError); | ||
.map(res => { | ||
return res.json().data; | ||
}); | ||
} | ||
|
||
getHero(id: number): Promise<Hero> { | ||
return this.getHeroes() | ||
.then(heroes => heroes.find(hero => hero.id === id)); | ||
getHero(id: number): Observable<Hero> { | ||
const url = `${this.heroesUrl}/${id}`; | ||
|
||
return this.http | ||
.get(url) | ||
.map(res => { | ||
return res.json().data; | ||
}); | ||
} | ||
|
||
save(hero: Hero): Promise<Hero> { | ||
saveHero(hero: Hero): Observable<Hero> { | ||
if (hero.id) { | ||
return this.put(hero); | ||
} else { | ||
return this.post(hero); | ||
} | ||
return this.post(hero); | ||
} | ||
|
||
delete(hero: Hero): Promise<Response> { | ||
deleteHero(hero: Hero): Observable<Hero> { | ||
const headers = new Headers(); | ||
headers.append('Content-Type', 'application/json'); | ||
|
||
const url = `${this.heroesUrl}/${hero.id}`; | ||
|
||
return this.http | ||
.delete(url, { headers: headers }) | ||
.toPromise() | ||
.catch(this.handleError); | ||
.map(() => hero); | ||
} | ||
|
||
// Add new Hero | ||
private post(hero: Hero): Promise<Hero> { | ||
private post(hero: Hero): Observable<Hero> { | ||
const headers = new Headers({ | ||
'Content-Type': 'application/json' | ||
}); | ||
|
||
return this.http | ||
.post(this.heroesUrl, JSON.stringify(hero), { headers: headers }) | ||
.toPromise() | ||
.then(res => res.json().data) | ||
.catch(this.handleError); | ||
.map(res => { | ||
return res.json().data; | ||
}); | ||
} | ||
|
||
// Update existing Hero | ||
private put(hero: Hero): Promise<Hero> { | ||
private put(hero: Hero): Observable<Hero> { | ||
const headers = new Headers(); | ||
headers.append('Content-Type', 'application/json'); | ||
|
||
const url = `${this.heroesUrl}/${hero.id}`; | ||
|
||
return this.http | ||
.put(url, JSON.stringify(hero), { headers: headers }) | ||
.toPromise() | ||
.then(() => hero) | ||
.catch(this.handleError); | ||
} | ||
|
||
private handleError(error: any): Promise<any> { | ||
console.error('An error occurred', error); | ||
return Promise.reject(error.message || error); | ||
.map(() => hero); | ||
} | ||
} |
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