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