Skip to content

Commit 716d6c5

Browse files
committed
POST-,PUT-,DELETE-requests implementation
1 parent 0c4bf87 commit 716d6c5

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
<h3>Heroes</h3>
2+
<div>
3+
<label
4+
>Hero name:
5+
<input #heroName />
6+
</label>
7+
<button (click)="add(heroName.value); heroName.value = ''">
8+
add
9+
</button>
10+
<button (click)="search(heroName.value)">
11+
search
12+
</button>
13+
</div>
214

315
<ul class="heroes">
416
<li *ngFor="let hero of heroes">
517
<a (click)="edit(hero)">
618
<span class="badge">{{ hero.id || -1 }}</span>
719
<span *ngIf="hero !== editHero">{{ hero.name }}</span>
20+
<input
21+
*ngIf="hero === editHero"
22+
[(ngModel)]="hero.name"
23+
(blur)="update()"
24+
(keyup.enter)="update()"
25+
/>
826
</a>
27+
<button class="delete" title="delete hero" (click)="delete(hero)">x</button>
928
</li>
1029
</ul>

src/app/heroes/heroes.component.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,41 @@ export class HeroesComponent implements OnInit {
2222
this.heroesService.getHeroes().subscribe(heroes => (this.heroes = heroes));
2323
}
2424

25+
public add(name: string): void {
26+
this.editHero = undefined;
27+
name = name.trim();
28+
if (!name) {
29+
return;
30+
}
31+
const newHero: Hero = { name } as Hero;
32+
this.heroesService.addHero(newHero).subscribe(hero => this.heroes.push(hero));
33+
}
34+
35+
public delete(hero: Hero): void {
36+
this.heroes = this.heroes.filter(h => h !== hero);
37+
this.heroesService.deleteHero(hero.id).subscribe();
38+
}
39+
2540
public edit(hero: Hero): void {
2641
this.editHero = hero;
2742
}
43+
44+
public search(searchTerm: string): void {
45+
this.editHero = undefined;
46+
if (searchTerm) {
47+
this.heroesService.searchHeroes(searchTerm).subscribe(heroes => (this.heroes = heroes));
48+
}
49+
}
50+
51+
public update(): void {
52+
if (this.editHero) {
53+
this.heroesService.updateHero(this.editHero).subscribe(hero => {
54+
const ix = hero ? this.heroes.findIndex(h => h.id === hero.id) : -1;
55+
if (ix > -1) {
56+
this.heroes[ix] = hero;
57+
}
58+
});
59+
this.editHero = undefined;
60+
}
61+
}
2862
}

src/app/heroes/heroes.service.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { catchError } from 'rxjs/operators';
2-
import { Hero } from './hero';
3-
import { HttpClient, HttpHeaders } from '@angular/common/http';
42
import { HandleError, HttpErrorHandler } from '../http-error-handler.service';
3+
import { Hero } from './hero';
4+
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
55
import { Injectable } from '@angular/core';
66
import { Observable } from 'rxjs';
77

@@ -21,9 +21,35 @@ export class HeroesService {
2121
this.handleError = httpErrorHandler.createHandleError('HeroesService');
2222
}
2323

24+
public addHero(hero: Hero): Observable<Hero> {
25+
return this.http
26+
.post<Hero>(this.heroesUrl, hero, httpOptions)
27+
.pipe(catchError(this.handleError('addHero', hero)));
28+
}
29+
30+
public deleteHero(id: number): Observable<{}> {
31+
const url = `${this.heroesUrl}/${id}`;
32+
return this.http.delete(url, httpOptions).pipe(catchError(this.handleError('deleteHero')));
33+
}
34+
2435
public getHeroes(): Observable<Hero[]> {
2536
return this.http
2637
.get<Hero[]>(this.heroesUrl)
2738
.pipe(catchError(this.handleError('getHeroes', [])));
2839
}
40+
41+
public searchHeroes(term: string): Observable<Hero[]> {
42+
term = term.trim();
43+
const options = term ? { params: new HttpParams().set('name', term) } : {};
44+
return this.http
45+
.get<Hero[]>(this.heroesUrl, options)
46+
.pipe(catchError(this.handleError<Hero[]>('searchHeroes', [])));
47+
}
48+
49+
public updateHero(hero: Hero): Observable<Hero> {
50+
httpOptions.headers = httpOptions.headers.set('Authorization', 'my-new-auth-token');
51+
return this.http
52+
.put<Hero>(this.heroesUrl, hero, httpOptions)
53+
.pipe(catchError(this.handleError('updateHero', hero)));
54+
}
2955
}

0 commit comments

Comments
 (0)