From 48caa14cd8ef13d1fb1665ea98023c32ddbb0e65 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 13 Jul 2017 16:55:02 +0200 Subject: [PATCH] Add account service. --- src/accounts/account.service.ts | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/accounts/account.service.ts diff --git a/src/accounts/account.service.ts b/src/accounts/account.service.ts new file mode 100644 index 0000000..05a3a43 --- /dev/null +++ b/src/accounts/account.service.ts @@ -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 { + this.logger.log("Query accounts"); + return this.http.get(this.url) + .map((res: Response) => res.json()); + } + + get(id: number): Observable { + return this.http.get(this.url + '/' + id) + .map((res: Response) => res.json()); + } + + create(account: Account): Observable { + 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 { + 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 { + 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()); + } +}