import { Injectable } from '@angular/core'; import { HttpClient, HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { SERVER_API_URL } from 'app/app.constants'; import { createRequestOption } from 'app/shared'; import { IMesa } from 'app/shared/model/mesa.model'; export type EntityResponseType = HttpResponse; export type EntityArrayResponseType = HttpResponse; @Injectable() export class MesaService { private resourceUrl = SERVER_API_URL + 'api/mesas'; constructor(private http: HttpClient) {} create(mesa: IMesa): Observable { const copy = this.convert(mesa); return this.http .post(this.resourceUrl, copy, { observe: 'response' }) .map((res: EntityResponseType) => this.convertResponse(res)); } update(mesa: IMesa): Observable { const copy = this.convert(mesa); 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: IMesa = this.convertItemFromServer(res.body); return res.clone({ body }); } private convertArrayResponse(res: EntityArrayResponseType): EntityArrayResponseType { const jsonResponse: IMesa[] = res.body; const body: IMesa[] = []; for (let i = 0; i < jsonResponse.length; i++) { body.push(this.convertItemFromServer(jsonResponse[i])); } return res.clone({ body }); } /** * Convert a returned JSON object to Mesa. */ private convertItemFromServer(mesa: IMesa): IMesa { const copy: IMesa = Object.assign({}, mesa, {}); return copy; } /** * Convert a Mesa to a JSON which can be sent to the server. */ private convert(mesa: IMesa): IMesa { const copy: IMesa = Object.assign({}, mesa, {}); return copy; } }