@@ -6,80 +6,89 @@ import { Http, Response, Headers } from 'angular2/http';
6
6
7
7
@Injectable ( )
8
8
export class HeroService {
9
-
9
+
10
10
private _heroesUrl = 'app/heroes' ; // URL to web api
11
-
12
- constructor ( private _http :Http ) {
13
- }
14
-
15
- //#docregion get-heroes
16
- getHeroes ( ) {
11
+
12
+ constructor ( private _http : Http ) { }
13
+
14
+ // #docregion get-heroes
15
+ getHeroes ( ) : Promise < Hero [ ] > {
17
16
return this . _http
17
+ // #docregion to-promise
18
18
. get ( this . _heroesUrl ) . toPromise ( )
19
- . then ( ( res :Response ) => res . json ( ) . data )
19
+ // #enddocregion to-promise
20
+ // #docregion to-data
21
+ . then ( response => response . json ( ) . data )
22
+ // #enddocregion to-data
23
+ // #docregion catch
20
24
. catch ( this . _handleError ) ;
25
+ // #enddocregion catch
21
26
}
22
- //#enddocregion get-heroes
23
-
27
+ // #enddocregion get-heroes
28
+
24
29
getHero ( id : number ) {
25
30
return this . getHeroes ( )
26
- . then ( heroes => heroes . filter ( ( hero : Hero ) => hero . id === id ) [ 0 ] ) ;
31
+ . then ( heroes => heroes . filter ( hero => hero . id === id ) [ 0 ] ) ;
27
32
}
28
-
29
- //#docregion save
30
- save ( hero :Hero ) {
31
- if ( hero . id ) {
33
+
34
+ // #docregion save
35
+ save ( hero : Hero ) : Promise < Hero > {
36
+ if ( hero . id ) {
32
37
return this . _put ( hero ) ;
33
38
}
34
39
return this . _post ( hero ) ;
35
40
}
36
- //#enddocregion save
37
-
38
- //#docregion delete-hero
39
- delete ( hero :Hero ) {
41
+ // #enddocregion save
42
+
43
+ // #docregion delete-hero
44
+ delete ( hero : Hero ) {
40
45
let headers = new Headers ( ) ;
41
46
headers . append ( 'Content-Type' , 'application/json' ) ;
42
-
47
+
43
48
let url = `${ this . _heroesUrl } /${ hero . id } ` ;
44
-
49
+
45
50
return this . _http
46
- . delete ( url , headers )
51
+ . delete ( url , headers )
47
52
. toPromise ( )
48
53
. catch ( this . _handleError ) ;
49
54
}
50
- //#enddocregion delete-hero
51
-
52
- //#docregion post-hero
53
- private _post ( hero :Hero ) {
54
- let headers = new Headers ( ) ;
55
- headers . append ( 'Content-Type' , 'application/json' ) ;
56
-
55
+ // #enddocregion delete-hero
56
+
57
+ // #docregion post-hero
58
+ // Add new Hero
59
+ private _post ( hero : Hero ) : Promise < Hero > {
60
+ let headers = new Headers ( {
61
+ 'Content-Type' : 'application/json' } ) ;
62
+
57
63
return this . _http
58
- . post ( this . _heroesUrl , JSON . stringify ( hero ) , { headers :headers } )
64
+ . post ( this . _heroesUrl , JSON . stringify ( hero ) , { headers : headers } )
59
65
. toPromise ( )
66
+ . then ( res => res . json ( ) . data )
60
67
. catch ( this . _handleError ) ;
61
68
}
62
- //#enddocregion post-hero
63
-
64
- //#docregion put-hero
65
- private _put ( hero :Hero ) {
69
+ // #enddocregion post-hero
70
+
71
+ // #docregion put-hero
72
+ // Update existing Hero
73
+ private _put ( hero : Hero ) {
66
74
let headers = new Headers ( ) ;
67
75
headers . append ( 'Content-Type' , 'application/json' ) ;
68
-
76
+
69
77
let url = `${ this . _heroesUrl } /${ hero . id } ` ;
70
-
78
+
71
79
return this . _http
72
- . put ( url , JSON . stringify ( hero ) , { headers :headers } )
80
+ . put ( url , JSON . stringify ( hero ) , { headers : headers } )
73
81
. toPromise ( )
82
+ . then ( ( ) => hero )
74
83
. catch ( this . _handleError ) ;
75
84
}
76
- //#enddocregion put-hero
77
-
78
- //#docregion error-handler
79
- private _handleError ( error :any ) {
80
- console . log ( 'An error occurred:' + error ) ;
81
- return Promise . reject ( error ) ;
85
+ // #enddocregion put-hero
86
+
87
+ // #docregion error-handler
88
+ private _handleError ( error : any ) {
89
+ console . error ( 'An error occurred' , error ) ;
90
+ return Promise . reject ( error . message || error ) ;
82
91
}
83
- //#enddocregion error-handler
92
+ // #enddocregion error-handler
84
93
}
85
- // #enddocregion
94
+ // #enddocregion
0 commit comments