import { Observable } from 'rxjs'; import { Task } from './task.model'; import { AuthHttp } from 'angular2-jwt'; import { TaskSerializer } from './task.serializer'; import { Response } from '@angular/http'; import { TaskConverter } from './task.converter'; import { UrlParamBuilderService } from '../service/url-param-builder.service'; import { IErrorHandler } from '../error/error-handler.interface'; import { TaskCollection } from './task.collection'; import { TaskCollectionConverter } from './task.collection.converter'; import { ApiPathComponent } from '../api-path-component'; export class TaskService { public constructor( private authHttp: AuthHttp, private taskConverter: TaskConverter, private collectionConverter: TaskCollectionConverter, private serializer: TaskSerializer, private baseUrl: string, private urlParamBuilderService: UrlParamBuilderService, private errorHandler: IErrorHandler ) {} public getMany(filters?: {[key: string]: any}, sort?: string[], page?: number, limit?: number): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.tasks + '?' + this.urlParamBuilderService.build(filters, sort, page, limit) ; return this.authHttp.get(url) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.collectionConverter.toCollection(res.json())); } public create(task: Task): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.tasks; return this.authHttp.post(url, this.serializer.serialize(task)) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.taskConverter.toOne(res.json())); } public update(task: Task): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.tasks + '/' + task.id; return this.authHttp.patch(url, this.serializer.serialize(task)) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.taskConverter.toOne(res.json())); } public delete(id: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.tasks + '/' + id; return this.authHttp.delete(url) .catch((error?: any) => this.errorHandler.handle(error)) .map(() => Observable.create()); } }