Add account service.
This commit is contained in:
parent
f55efca9fd
commit
48caa14cd8
56
src/accounts/account.service.ts
Normal file
56
src/accounts/account.service.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
// 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 { 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)
|
||||||
|
.map((res: Response) => res.json());
|
||||||
|
}
|
||||||
|
|
||||||
|
get(id: number): Observable<Account> {
|
||||||
|
return this.http.get(this.url + '/' + id)
|
||||||
|
.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)
|
||||||
|
.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)
|
||||||
|
.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)
|
||||||
|
.map((res: Response) => res.json());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user