Migrate to HttpClient in Schedule module.

This commit is contained in:
Alexis Lahouze 2017-07-31 13:15:45 +02:00
parent d71656412a
commit dc30f38662
2 changed files with 19 additions and 23 deletions

View File

@ -3,9 +3,8 @@
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 { RestangularModule } from 'ngx-restangular';
import { NgLoggerModule, Level } from '@nsalaun/ng-logger'; import { NgLoggerModule, Level } from '@nsalaun/ng-logger';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr'; import { ToastrModule } from 'ngx-toastr';
@ -24,11 +23,10 @@ export function accountIdServiceFactory(i: any) {
@NgModule({ @NgModule({
imports: [ imports: [
HttpModule, HttpClientModule,
CommonModule, CommonModule,
FormsModule, FormsModule,
NgLoggerModule, NgLoggerModule,
RestangularModule,
ToastrModule, ToastrModule,
NgbModule, NgbModule,
TextMaskModule TextMaskModule

View File

@ -1,49 +1,47 @@
// 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 { Schedule } from './schedule'; import { Schedule } from './schedule';
@Injectable() @Injectable()
export class ScheduleService { export class ScheduleService {
constructor( constructor(
private restangular: Restangular private http: HttpClient
) {} ) {}
private all() { private url(id?: number): string {
//return this.accountService.one(accountId).all('scheduled_operation'); if(id) {
return this.restangular.all('scheduled_operation'); return `/api/scheduled_operation/${id}`;
} }
private one(id: number) { return `/api/scheduled_operation`;
//return this.accountService.one(accountId).one('scheduled_operation', id);
return this.restangular.one('scheduled_operation', id);
} }
query(accountId: number): Observable<Schedule[]> { query(accountId: number): Observable<Schedule[]> {
return this.all().getList({ let params = new HttpParams().set('account_id', `${accountId}`);
account_id: accountId
}); return this.http.get<Schedule[]>(this.url(), { params: params });
} }
get(accountId: number, id: number): Observable<Schedule> { get(accountId: number, id: number): Observable<Schedule> {
return this.one(id).get({ let params = new HttpParams().set('account_id', `${accountId}`);
account_id: accountId
}); return this.http.get<Schedule>(this.url(id), { params: params });
} }
create(schedule: Schedule): Observable<Schedule> { create(schedule: Schedule): Observable<Schedule> {
return this.all().post(schedule); return this.http.post<Schedule>(this.url(), schedule);
} }
update(schedule: Schedule): Observable<Schedule> { update(schedule: Schedule): Observable<Schedule> {
return this.one(schedule.id).post(null, schedule); return this.http.post<Schedule>(this.url(schedule.id), schedule);
} }
delete(schedule: Schedule): Observable<Schedule> { delete(schedule: Schedule): Observable<Schedule> {
return this.one(schedule.id).remove(); return this.http.delete<Schedule>(this.url(schedule.id));
} }
} }