Migrate to HttpClient in Account module.

This commit is contained in:
Alexis Lahouze 2017-07-31 13:00:17 +02:00
parent c9e1483206
commit 2d8d39442d
4 changed files with 22 additions and 25 deletions

View File

@ -3,10 +3,9 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http'; import { HttpClientModule } from '@angular/common/http';
import { NgLoggerModule, Level } from '@nsalaun/ng-logger'; import { NgLoggerModule, Level } from '@nsalaun/ng-logger';
import { RestangularModule } from 'ngx-restangular';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr'; import { ToastrModule } from 'ngx-toastr';
@ -21,11 +20,10 @@ import { DailyBalanceService } from './dailyBalance.service';
@NgModule({ @NgModule({
imports: [ imports: [
HttpModule, HttpClientModule,
CommonModule, CommonModule,
FormsModule, FormsModule,
NgLoggerModule, NgLoggerModule,
RestangularModule,
ToastrModule, ToastrModule,
NgbModule NgbModule
], ],

View File

@ -1,42 +1,41 @@
// vim: set tw=80 ts=2 sw=2 sts=2 : // vim: set tw=80 ts=2 sw=2 sts=2 :
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx'; import { HttpClient } from '@angular/common/http';
import { Restangular } from "ngx-restangular"; import { Observable } from 'rxjs/Rx';
import { Account } from './account'; import { Account } from './account';
@Injectable() @Injectable()
export class AccountService { export class AccountService {
constructor( constructor(
private restangular: Restangular private http: HttpClient
) {} ) {}
private all() { url(id?: Number): string {
return this.restangular.all('account'); if(id) {
return `/api/account/${id}`;
} }
private one(id: number) { return `/api/account`;
return this.restangular.one('account', id);
} }
query(): Observable<Account[]> { query(): Observable<Account[]> {
return this.all().getList(); return this.http.get<Account[]>(this.url());
} }
get(id: number): Observable<Account> { get(id: number): Observable<Account> {
return this.one(id).get(); return this.http.get<Account>(this.url(id));
} }
create(account: Account): Observable<Account> { create(account: Account): Observable<Account> {
return this.all().post(account); return this.http.post<Account>(this.url(), account);
} }
update(account: Account): Observable<Account> { update(account: Account): Observable<Account> {
return this.one(account.id).post(null, account); return this.http.post<Account>(this.url(account.id), account);
} }
delete(account: Account): Observable<Account> { delete(account: Account): Observable<Account> {
return this.one(account.id).remove(); return this.http.delete<Account>(this.url(account.id));
} }
} }

View File

@ -2,17 +2,17 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { Restangular } from "ngx-restangular"; import { HttpClient, HttpParams } from "@angular/common/http";
import { AccountBalances } from './accountBalances'; import { AccountBalances } from './accountBalances';
@Injectable() @Injectable()
export class AccountBalancesService { export class AccountBalancesService {
constructor( constructor(
private restangular: Restangular private http: HttpClient
) {} ) {}
get(id: number): Observable<AccountBalances> { get(id: number): Observable<AccountBalances> {
return this.restangular.one('account', id).one('balances').get(); return this.http.get<AccountBalances>(`/api/account/${id}/balances`);
} }
} }

View File

@ -1,19 +1,19 @@
// vim: set tw=80 ts=2 sw=2 sts=2: // vim: set tw=80 ts=2 sw=2 sts=2:
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx'; import { HttpClient, HttpParams } from '@angular/common/http';
import { Restangular } from "ngx-restangular"; import { Observable } from 'rxjs/Rx';
import { DailyBalance } from './dailyBalance'; import { DailyBalance } from './dailyBalance';
@Injectable() @Injectable()
export class DailyBalanceService { export class DailyBalanceService {
constructor( constructor(
private restangular: Restangular private http: HttpClient
) {} ) {}
query(id: number): Observable<DailyBalance[]> { query(id: number): Observable<DailyBalance[]> {
return this.restangular.one('account', id).one('daily_balances').getList(); return this.http.get<DailyBalance[]>(`/api/account/${id}/daily_balances`);
} }
} }