@@ -36,20 +36,28 @@ export class HeroService {
3636 }
3737
3838 getHeroById ( id : number ) : Promise < Hero > {
39- const url = `${ this . heroesUrl } / ${ id } ` ;
39+ const url = `${ this . heroesUrl } ` ;
4040 return this . http . get ( url )
4141 . toPromise ( )
42- . then ( response => response . json ( ) as Hero )
42+ . then ( ( response ) => {
43+ const heroesWithId = response . json ( ) . filter ( ( hero ) => {
44+ return hero . id === id ;
45+ } ) ;
46+ return heroesWithId . length === 1 ? heroesWithId [ 0 ] : { } ;
47+ } )
4348 . catch ( HeroService . handleError ) ;
4449 }
4550
46- create ( hero : Hero ) : Promise < Hero > {
51+ create ( hero : Hero ) : Promise < Array < Hero > > {
52+ let allHeroes = JSON . parse ( localStorage . getItem ( 'heroes' ) ) ;
53+ allHeroes . push ( {
54+ id : '' + parseInt ( ( Math . random ( ) * ( 99999 - 100 + 1 ) ) , 10 ) + 100 ,
55+ name : hero . name ,
56+ alterEgo : hero . alterEgo ,
57+ power : hero . power
58+ } ) ;
4759 return this . http
48- . post ( this . heroesUrl , JSON . stringify ( {
49- name : hero . name ,
50- alterEgo : hero . alterEgo ,
51- power : hero . power
52- } ) , { headers : this . headers } )
60+ . put ( this . heroesUrl , JSON . stringify ( allHeroes ) , { headers : this . headers } )
5361 . toPromise ( )
5462 . then ( res => res . json ( ) )
5563 . catch ( HeroService . handleError ) ;
@@ -66,7 +74,7 @@ export class HeroService {
6674
6775 remove ( id : number ) : Promise < void > {
6876 const url = `${ this . heroesUrl } /${ id } ` ;
69- return this . http . delete ( url , { headers : this . headers } )
77+ return this . http . put ( url , { headers : this . headers } )
7078 . toPromise ( )
7179 . then ( ( ) => null )
7280 . catch ( HeroService . handleError ) ;
0 commit comments