import { Injectable, } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import { IHealthFacilities, IHealthcareWorkers, IMedicine, IPandemic, IPharmacy, IPharmacyHead, IPharmacyHeadRequest } from './interfaces'; @Injectable() export class DataService { baseFacilitiesUrl: string = '/api/facilities'; baseWorkersUrl: string = '/api/workers'; baseMedicineUrl: string = '/api/medicine'; basePandemicUrl: string = '/api/pandemic'; basePharmacyUrl: string = '/api/pharmacy'; basePharmacyHead: string = '/api/pharmacyhead'; constructor(private http: HttpClient) { } //Facility GET getFacilities() : Observable { return this.http.get(this.baseFacilitiesUrl) .pipe( map((facilities: IHealthFacilities[]) => { return facilities; }), catchError(this.handleError) ); } searchFacilities(searchquery: string) : Observable { return this.http.get(this.baseFacilitiesUrl + '/search/' + searchquery) .pipe( map((facilities: IHealthFacilities[]) => { return facilities; }), catchError(this.handleError) ); } getFacility(id: string) : Observable { return this.http.get(this.baseFacilitiesUrl + '/' + id) .pipe(catchError(this.handleError)); } //Worker GET getWorkers() : Observable { return this.http.get(this.baseWorkersUrl) .pipe( map((workers: IHealthcareWorkers[]) => { return workers; }), catchError(this.handleError) ); } searchWorkers(searchquery: string) : Observable { return this.http.get(this.baseWorkersUrl + '/search/' + searchquery) .pipe( map((workers: IHealthcareWorkers[]) => { return workers; }), catchError(this.handleError) ); } getWorker(id: string) : Observable { return this.http.get(this.baseWorkersUrl + '/' + id) .pipe(catchError(this.handleError)); } //Medicine GET getMedicines() : Observable { return this.http.get(this.baseMedicineUrl) .pipe( map((medicines: IMedicine[]) => { return medicines; }), catchError(this.handleError) ); } searchMedicines(searchquery: string) : Observable { return this.http.get(this.baseMedicineUrl + '/search/' + searchquery) .pipe( map((medicines: IMedicine[]) => { return medicines; }), catchError(this.handleError) ); } getMedicine(id: string) : Observable { return this.http.get(this.baseMedicineUrl + '/' + id) .pipe(catchError(this.handleError)); } getPandemic() : Observable { return this.http.get(this.basePandemicUrl) .pipe(catchError(this.handleError)); } //Pharmacy GET getPharmacies() : Observable { return this.http.get(this.basePharmacyUrl) .pipe( map((pharmacies: IPharmacy[]) => { return pharmacies; }), catchError(this.handleError) ); } searchPharmacies(searchquery: string) : Observable { return this.http.get(this.basePharmacyUrl + '/search/' + searchquery) .pipe( map((pharmacies: IPharmacy[]) => { return pharmacies; }), catchError(this.handleError) ); } getPharmacy(id: string) : Observable { return this.http.get(this.basePharmacyUrl + '/' + id) .pipe(catchError(this.handleError)); } //PharmacyHead GET getPharmacyHeads() : Observable { return this.http.get(this.basePharmacyHead) .pipe( map((pharmacyheads: IPharmacyHead[]) => { return pharmacyheads; }), catchError(this.handleError) ); } getClaimingRequests() : Observable { return this.http.get(this.basePharmacyHead + '/requests') .pipe( map((requests: IPharmacyHeadRequest[]) => { return requests; }), catchError(this.handleError) ); } loginPharmacyHead(email: string, passwd: string) : Observable { let postData = {email : email ,password :passwd}; return this.http.post(this.basePharmacyHead + '/login', postData) .pipe( map((data) => { return data; }), catchError(this.handleError) ); } getPharmacyHead(id: string) : Observable { return this.http.get(this.basePharmacyHead + '/' + id) .pipe(catchError(this.handleError)); } //PharmacyHead POST insertPharmacyHead(head: IPharmacyHead) : Observable { return this.http.post(this.basePharmacyHead, head) .pipe( map((data) => { return data; }), catchError(this.handleError) ); } claimPharmacy(req: IPharmacyHeadRequest) : Observable { return this.http.post(this.basePharmacyHead + '/requests', req) .pipe( map((data) => { return data; }), catchError(this.handleError) ); } //PharmacyHead PUT updatePharmacyHead(head: IPharmacyHead) : Observable { return this.http.put(this.basePharmacyHead + '/' + head.id, head) .pipe( map((data) => { return data; }), catchError(this.handleError) ); } //PharmacyHead DELETE deletePharmacyHead(id: string) : Observable { return this.http.delete(this.basePharmacyHead + '/' + id) .pipe(catchError(this.handleError)); } deleteClaimingRequest(id: string) : Observable { return this.http.delete(this.basePharmacyHead + '/requests/' + id) .pipe(catchError(this.handleError)); } private handleError(error: HttpErrorResponse) { console.error('server error:', error); if (error.error instanceof Error) { let errMessage = error.error.message; return Observable.throw(errMessage); } return Observable.throw(error || 'ASP.NET Core server error'); } }