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

View File

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