import { Injectable } from "@angular/core"; import { HttpClient } from "@angular/common/http"; import { Observable } from "rxjs"; import { map } from "rxjs/operators"; import { GearResp, GearReq } from "../models/gear"; import { BaseService } from "./base.service"; import { CacheApiKey } from "../models/cache-api-key.enum"; @Injectable() export class GearService extends BaseService { constructor(private http: HttpClient) { super(); } public getListOfGears(): Observable> { let callToApi = this.http.get>(`${this.apiUrl}/Gear`, { headers: this.headers, }); return this.serviceCacheApi.get>( CacheApiKey.Gear, callToApi, ); } public addGear( name: string, manufacturer: string, minSize: number, maxSize: number, aad: string, mainCanopy: string, reserveCanopy: string, ) { const bodyNewGear: GearReq = { id: 0, name: name, manufacturer: manufacturer, minSize: minSize, maxSize: maxSize, aad: aad, mainCanopy: mainCanopy, reserveCanopy: reserveCanopy, }; this.serviceCacheApi.delete(CacheApiKey.Gear); return this.http.post(`${this.apiUrl}/Gear`, bodyNewGear, { headers: this.headers, }); } public getById(id: number): Observable { return this.serviceCacheApi .getByKey>(CacheApiKey.Gear) .pipe( map((data) => { return data.find((f) => f.id === id); }), ); } public getFromCache(): Observable> { return this.serviceCacheApi.getByKey>(CacheApiKey.Gear); } public updateGear( id: number, name: string, manufacturer: string, minSize: number, maxSize: number, aad: string, mainCanopy: string, reserveCanopy: string, ) { const gearData = { id: id, name: name, manufacturer: manufacturer, minSize: minSize, maxSize: maxSize, aad: aad, mainCanopy: mainCanopy, reserveCanopy: reserveCanopy, }; const bodyUpdatedGear = new GearReq(gearData); this.serviceCacheApi.delete(CacheApiKey.Gear); return this.http.put(`${this.apiUrl}/Gear/${id}`, bodyUpdatedGear, { headers: this.headers, }); } }