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'; import { environment } from '../../environments/environment'; @Injectable() export class DataService { baseFacilitiesUrl: string = `${environment.baseApiUrl}api/facilities`; baseWorkersUrl: string = `${environment.baseApiUrl}api/workers`; baseMedicineUrl: string = `${environment.baseApiUrl}api/medicines`; basePandemicUrl: string = `${environment.baseApiUrl}api/pandemic`; basePharmacyUrl: string = `${environment.baseApiUrl}api/pharmacy`; basePharmacyHead: string = `${environment.baseApiUrl}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 = {username : 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 + '/add', 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) ); } updatePharmacyHead(head: IPharmacyHead) : Observable { return this.http.post(this.basePharmacyHead + '/update', head) .pipe( map((data) => { return data; }), catchError(this.handleError) ); } deletePharmacyHead(id: string) : Observable { return this.http.post(this.basePharmacyHead + '/delete/' + id, id) .pipe( map((data) => { return data; }), catchError(this.handleError) ); } deleteClaimingRequest(req: IPharmacyHeadRequest) : Observable { return this.http.post(this.basePharmacyHead + '/requests/' + req.id, req) .pipe( map((data) => { return data; }), 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'); } }