Use ngx-restangular to handle account API.
This commit is contained in:
@@ -4,13 +4,15 @@ import { NgModule } from '@angular/core';
|
||||
import { HttpModule } from '@angular/http';
|
||||
|
||||
import { NgLoggerModule, Level } from '@nsalaun/ng-logger';
|
||||
import { RestangularModule } from 'ngx-restangular';
|
||||
|
||||
import { AccountService } from './account.service';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
HttpModule,
|
||||
NgLoggerModule.forRoot(Level.LOG)
|
||||
NgLoggerModule.forRoot(Level.LOG),
|
||||
RestangularModule
|
||||
],
|
||||
providers: [
|
||||
AccountService
|
||||
|
||||
@@ -3,10 +3,9 @@ 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 { Restangular } from "ngx-restangular";
|
||||
|
||||
import { Account } from './account';
|
||||
|
||||
@@ -14,49 +13,35 @@ import { Account } from './account';
|
||||
export class AccountService {
|
||||
private url = '/api/account/';
|
||||
|
||||
static $inject = ['Logger']
|
||||
constructor(
|
||||
private logger: Logger,
|
||||
private restangular: Restangular) {}
|
||||
|
||||
constructor(private logger: Logger, private http: Http) { }
|
||||
private all() {
|
||||
return this.restangular.all('account');
|
||||
}
|
||||
|
||||
private one(id: number) {
|
||||
return this.restangular.one('account', id);
|
||||
}
|
||||
|
||||
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());
|
||||
return this.all().getList();
|
||||
}
|
||||
|
||||
get(id: number): Observable<Account> {
|
||||
return this.http.get(this.url + id)
|
||||
.catch((res: Response) => Observable.throw(res.json()))
|
||||
.map((res: Response) => res.json());
|
||||
return this.one(id).get();
|
||||
}
|
||||
|
||||
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());
|
||||
return this.all().post(account);
|
||||
}
|
||||
|
||||
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());
|
||||
return this.one(account.id).post(null, account);
|
||||
}
|
||||
|
||||
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());
|
||||
return this.one(account.id).delete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user