accountant-ui/src/accounts/account.service.ts
2017-07-14 10:05:09 +02:00

63 lines
2.0 KiB
TypeScript

// vim: set tw=80 ts=2 sw=2 sts=2 :
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Logger } from "@nsalaun/ng-logger";
import { Account } from './account';
@Injectable()
export class AccountService {
private url = '/api/account/';
static $inject = ['Logger']
constructor(private logger: Logger, private http: Http) { }
query(): Observable<Account[]> {
this.logger.log("Query accounts");
return this.http.get(this.url)
.catch((res: Response) => Observable.throw(res.json()))
.map((res: Response) => res.json());
}
get(id: number): Observable<Account> {
return this.http.get(this.url + '/' + id)
.catch((res: Response) => Observable.throw(res.json()))
.map((res: Response) => res.json());
}
create(account: Account): Observable<Account> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(account);
return this.http.post(this.url, body, options)
.catch((res: Response) => Observable.throw(res.json()))
.map((res: Response) => res.json());
}
update(account: Account): Observable<Account> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(account);
return this.http.post(this.url + '/' + account.id, body, options)
.catch((res: Response) => Observable.throw(res.json()))
.map((res: Response) => res.json());
}
delete(account: Account): Observable<Account> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.delete(this.url + '/' + account.id)
.catch((res: Response) => Observable.throw(res.json()))
.map((res: Response) => res.json());
}
}