mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Headers, Http, Response } from '@angular/http';
|
|
|
|
import 'rxjs/add/operator/toPromise';
|
|
|
|
import { Hero } from './hero';
|
|
|
|
@Injectable()
|
|
export class HeroService {
|
|
private heroesUrl = 'app/heroes'; // URL to web api
|
|
|
|
constructor(private http: Http) { }
|
|
|
|
getHeroes(): Promise<Hero[]> {
|
|
return this.http
|
|
.get(this.heroesUrl)
|
|
.toPromise()
|
|
.then(response => response.json() as Hero[])
|
|
.catch(this.handleError);
|
|
}
|
|
|
|
getHero(id: number): Promise<Hero> {
|
|
return this.http
|
|
.get(this.heroesUrl+'/'+id)
|
|
.toPromise()
|
|
.then(response => response.json() as Hero)
|
|
.catch(this.handleError);
|
|
}
|
|
|
|
save(hero: Hero): Promise<Hero> {
|
|
if (hero.id) {
|
|
return this.put(hero);
|
|
}
|
|
return this.post(hero);
|
|
}
|
|
|
|
delete(hero: Hero): Promise<Response> {
|
|
let headers = new Headers();
|
|
headers.append('Content-Type', 'application/json');
|
|
|
|
let url = `${this.heroesUrl}/${hero.id}`;
|
|
|
|
return this.http
|
|
.delete(url, { headers: headers })
|
|
.toPromise()
|
|
.catch(this.handleError);
|
|
}
|
|
|
|
// Add new Hero
|
|
private post(hero: Hero): Promise<Hero> {
|
|
let headers = new Headers({
|
|
'Content-Type': 'application/json'
|
|
});
|
|
|
|
return this.http
|
|
.post(this.heroesUrl, JSON.stringify(hero), { headers: headers })
|
|
.toPromise()
|
|
.then(res => res.json())
|
|
.catch(this.handleError);
|
|
}
|
|
|
|
// Update existing Hero
|
|
private put(hero: Hero): Promise<Hero> {
|
|
let headers = new Headers();
|
|
headers.append('Content-Type', 'application/json');
|
|
|
|
let 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);
|
|
}
|
|
}
|