import { Injectable } from '@angular/core'; import { HttpClient, HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import * as moment from 'moment'; import { SERVER_API_URL } from 'app/app.constants'; import { createRequestOption } from 'app/shared'; import { IComanda } from 'app/shared/model/comanda.model'; export type EntityResponseType = HttpResponse; export type EntityArrayResponseType = HttpResponse; @Injectable() export class ComandaService { private resourceUrl = SERVER_API_URL + 'api/comandas'; constructor(private http: HttpClient) {} create(comanda: IComanda): Observable { const copy = this.convert(comanda); return this.http .post(this.resourceUrl, copy, { observe: 'response' }) .map((res: EntityResponseType) => this.convertResponse(res)); } update(comanda: IComanda): Observable { const copy = this.convert(comanda); return this.http .put(this.resourceUrl, copy, { observe: 'response' }) .map((res: EntityResponseType) => this.convertResponse(res)); } find(id: number): Observable { return this.http .get(`${this.resourceUrl}/${id}`, { observe: 'response' }) .map((res: EntityResponseType) => this.convertResponse(res)); } query(req?: any): Observable { const options = createRequestOption(req); return this.http .get(this.resourceUrl, { params: options, observe: 'response' }) .map((res: EntityArrayResponseType) => this.convertArrayResponse(res)); } delete(id: number): Observable> { return this.http.delete(`${this.resourceUrl}/${id}`, { observe: 'response' }); } private convertResponse(res: EntityResponseType): EntityResponseType { const body: IComanda = this.convertItemFromServer(res.body); return res.clone({ body }); } private convertArrayResponse(res: EntityArrayResponseType): EntityArrayResponseType { const jsonResponse: IComanda[] = res.body; const body: IComanda[] = []; for (let i = 0; i < jsonResponse.length; i++) { body.push(this.convertItemFromServer(jsonResponse[i])); } return res.clone({ body }); } /** * Convert a returned JSON object to Comanda. */ private convertItemFromServer(comanda: IComanda): IComanda { const copy: IComanda = Object.assign({}, comanda, { datePedido: comanda.datePedido != null ? moment(comanda.datePedido) : comanda.datePedido, dateServido: comanda.dateServido != null ? moment(comanda.dateServido) : comanda.dateServido }); return copy; } /** * Convert a Comanda to a JSON which can be sent to the server. */ private convert(comanda: IComanda): IComanda { const copy: IComanda = Object.assign({}, comanda, { datePedido: comanda.datePedido != null && comanda.datePedido.isValid() ? comanda.datePedido.toJSON() : null, dateServido: comanda.dateServido != null && comanda.dateServido.isValid() ? comanda.dateServido.toJSON() : null }); return copy; } }