[d23bf72] | 1 | import { Injectable, } from '@angular/core';
|
---|
| 2 | import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
---|
| 3 |
|
---|
| 4 | import { Observable } from 'rxjs';
|
---|
| 5 | import { map, catchError } from 'rxjs/operators';
|
---|
| 6 |
|
---|
| 7 | import { IHealthFacilities, IHealthcareWorkers, IMedicine, IPandemic, IPharmacy, IPharmacyHead, IPharmacyHeadRequest } from './interfaces';
|
---|
[1db5673] | 8 | import { environment } from '../../environments/environment';
|
---|
[d23bf72] | 9 |
|
---|
| 10 | @Injectable()
|
---|
| 11 | export class DataService {
|
---|
[1db5673] | 12 | baseFacilitiesUrl: string = `${environment.baseApiUrl}api/facilities`;
|
---|
| 13 | baseWorkersUrl: string = `${environment.baseApiUrl}api/workers`;
|
---|
| 14 | baseMedicineUrl: string = `${environment.baseApiUrl}api/medicines`;
|
---|
| 15 | basePandemicUrl: string = `${environment.baseApiUrl}api/pandemic`;
|
---|
| 16 | basePharmacyUrl: string = `${environment.baseApiUrl}api/pharmacy`;
|
---|
| 17 | basePharmacyHead: string = `${environment.baseApiUrl}api/pharmacyhead`;
|
---|
[d23bf72] | 18 |
|
---|
| 19 | constructor(private http: HttpClient) {
|
---|
| 20 |
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | //Facility GET
|
---|
| 24 | getFacilities() : Observable<IHealthFacilities[]> {
|
---|
| 25 | return this.http.get<IHealthFacilities[]>(this.baseFacilitiesUrl)
|
---|
| 26 | .pipe(
|
---|
| 27 | map((facilities: IHealthFacilities[]) => {
|
---|
| 28 | return facilities;
|
---|
| 29 | }),
|
---|
| 30 | catchError(this.handleError)
|
---|
| 31 | );
|
---|
| 32 | }
|
---|
| 33 | searchFacilities(searchquery: string) : Observable<IHealthFacilities[]> {
|
---|
| 34 | return this.http.get<IHealthFacilities[]>(this.baseFacilitiesUrl + '/search/' + searchquery)
|
---|
| 35 | .pipe(
|
---|
| 36 | map((facilities: IHealthFacilities[]) => {
|
---|
| 37 | return facilities;
|
---|
| 38 | }),
|
---|
| 39 | catchError(this.handleError)
|
---|
| 40 | );
|
---|
| 41 | }
|
---|
| 42 | getFacility(id: string) : Observable<IHealthFacilities> {
|
---|
| 43 | return this.http.get<IHealthFacilities>(this.baseFacilitiesUrl + '/' + id)
|
---|
| 44 | .pipe(catchError(this.handleError));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | //Worker GET
|
---|
| 49 | getWorkers() : Observable<IHealthcareWorkers[]> {
|
---|
| 50 | return this.http.get<IHealthcareWorkers[]>(this.baseWorkersUrl)
|
---|
| 51 | .pipe(
|
---|
| 52 | map((workers: IHealthcareWorkers[]) => {
|
---|
| 53 | return workers;
|
---|
| 54 | }),
|
---|
| 55 | catchError(this.handleError)
|
---|
| 56 | );
|
---|
| 57 | }
|
---|
| 58 | searchWorkers(searchquery: string) : Observable<IHealthcareWorkers[]> {
|
---|
| 59 | return this.http.get<IHealthcareWorkers[]>(this.baseWorkersUrl + '/search/' + searchquery)
|
---|
| 60 | .pipe(
|
---|
| 61 | map((workers: IHealthcareWorkers[]) => {
|
---|
| 62 | return workers;
|
---|
| 63 | }),
|
---|
| 64 | catchError(this.handleError)
|
---|
| 65 | );
|
---|
| 66 | }
|
---|
| 67 | getWorker(id: string) : Observable<IHealthcareWorkers> {
|
---|
| 68 | return this.http.get<IHealthcareWorkers>(this.baseWorkersUrl + '/' + id)
|
---|
| 69 | .pipe(catchError(this.handleError));
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | //Medicine GET
|
---|
| 74 | getMedicines() : Observable<IMedicine[]> {
|
---|
| 75 | return this.http.get<IMedicine[]>(this.baseMedicineUrl)
|
---|
| 76 | .pipe(
|
---|
| 77 | map((medicines: IMedicine[]) => {
|
---|
| 78 | return medicines;
|
---|
| 79 | }),
|
---|
| 80 | catchError(this.handleError)
|
---|
| 81 | );
|
---|
| 82 | }
|
---|
| 83 | searchMedicines(searchquery: string) : Observable<IMedicine[]> {
|
---|
| 84 | return this.http.get<IMedicine[]>(this.baseMedicineUrl + '/search/' + searchquery)
|
---|
| 85 | .pipe(
|
---|
| 86 | map((medicines: IMedicine[]) => {
|
---|
| 87 | return medicines;
|
---|
| 88 | }),
|
---|
| 89 | catchError(this.handleError)
|
---|
| 90 | );
|
---|
| 91 | }
|
---|
| 92 | getMedicine(id: string) : Observable<IMedicine> {
|
---|
| 93 | return this.http.get<IMedicine>(this.baseMedicineUrl + '/' + id)
|
---|
| 94 | .pipe(catchError(this.handleError));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
[ad60966] | 98 | getPandemic() : Observable<IPandemic> {
|
---|
| 99 | return this.http.get<IPandemic>(this.basePandemicUrl)
|
---|
[d23bf72] | 100 | .pipe(catchError(this.handleError));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | //Pharmacy GET
|
---|
| 105 | getPharmacies() : Observable<IPharmacy[]> {
|
---|
| 106 | return this.http.get<IPharmacy[]>(this.basePharmacyUrl)
|
---|
| 107 | .pipe(
|
---|
| 108 | map((pharmacies: IPharmacy[]) => {
|
---|
| 109 | return pharmacies;
|
---|
| 110 | }),
|
---|
| 111 | catchError(this.handleError)
|
---|
| 112 | );
|
---|
| 113 | }
|
---|
| 114 | searchPharmacies(searchquery: string) : Observable<IPharmacy[]> {
|
---|
| 115 | return this.http.get<IPharmacy[]>(this.basePharmacyUrl + '/search/' + searchquery)
|
---|
| 116 | .pipe(
|
---|
| 117 | map((pharmacies: IPharmacy[]) => {
|
---|
| 118 | return pharmacies;
|
---|
| 119 | }),
|
---|
| 120 | catchError(this.handleError)
|
---|
| 121 | );
|
---|
| 122 | }
|
---|
| 123 | getPharmacy(id: string) : Observable<IPharmacy> {
|
---|
| 124 | return this.http.get<IPharmacy>(this.basePharmacyUrl + '/' + id)
|
---|
| 125 | .pipe(catchError(this.handleError));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | //PharmacyHead GET
|
---|
| 129 | getPharmacyHeads() : Observable<IPharmacyHead[]> {
|
---|
| 130 | return this.http.get<IPharmacyHead[]>(this.basePharmacyHead)
|
---|
| 131 | .pipe(
|
---|
| 132 | map((pharmacyheads: IPharmacyHead[]) => {
|
---|
| 133 | return pharmacyheads;
|
---|
| 134 | }),
|
---|
| 135 | catchError(this.handleError)
|
---|
| 136 | );
|
---|
| 137 | }
|
---|
| 138 | getClaimingRequests() : Observable<IPharmacyHeadRequest[]> {
|
---|
| 139 | return this.http.get<IPharmacyHeadRequest[]>(this.basePharmacyHead + '/requests')
|
---|
| 140 | .pipe(
|
---|
| 141 | map((requests: IPharmacyHeadRequest[]) => {
|
---|
| 142 | return requests;
|
---|
| 143 | }),
|
---|
| 144 | catchError(this.handleError)
|
---|
| 145 | );
|
---|
| 146 | }
|
---|
| 147 | loginPharmacyHead(email: string, passwd: string) : Observable<any> {
|
---|
| 148 | let postData = {username : email ,password :passwd};
|
---|
| 149 | return this.http.post<any>(this.basePharmacyHead + '/login', postData)
|
---|
| 150 | .pipe(
|
---|
| 151 | map((data) => {
|
---|
| 152 | return data;
|
---|
| 153 | }),
|
---|
| 154 | catchError(this.handleError)
|
---|
| 155 | );
|
---|
| 156 | }
|
---|
| 157 | getPharmacyHead(id: string) : Observable<IPharmacyHead> {
|
---|
| 158 | return this.http.get<IPharmacyHead>(this.basePharmacyHead + '/' + id)
|
---|
| 159 | .pipe(catchError(this.handleError));
|
---|
| 160 | }
|
---|
| 161 | //PharmacyHead POST
|
---|
| 162 | insertPharmacyHead(head: IPharmacyHead) : Observable<IPharmacyHead> {
|
---|
| 163 | return this.http.post<IPharmacyHead>(this.basePharmacyHead + '/add', head)
|
---|
| 164 | .pipe(
|
---|
| 165 | map((data) => {
|
---|
| 166 | return data;
|
---|
| 167 | }),
|
---|
| 168 | catchError(this.handleError)
|
---|
| 169 | );
|
---|
| 170 | }
|
---|
| 171 | claimPharmacy(req: IPharmacyHeadRequest) : Observable<IPharmacyHeadRequest> {
|
---|
| 172 | return this.http.post<IPharmacyHeadRequest>(this.basePharmacyHead + '/requests', req)
|
---|
| 173 | .pipe(
|
---|
| 174 | map((data) => {
|
---|
| 175 | return data;
|
---|
| 176 | }),
|
---|
| 177 | catchError(this.handleError)
|
---|
| 178 | );
|
---|
| 179 | }
|
---|
| 180 | updatePharmacyHead(head: IPharmacyHead) : Observable<IPharmacyHead> {
|
---|
[de9d697] | 181 | return this.http.post<IPharmacyHead>(this.basePharmacyHead + '/update', head)
|
---|
[d23bf72] | 182 | .pipe(
|
---|
| 183 | map((data) => {
|
---|
| 184 | return data;
|
---|
| 185 | }),
|
---|
| 186 | catchError(this.handleError)
|
---|
| 187 | );
|
---|
| 188 | }
|
---|
[de9d697] | 189 | deletePharmacyHead(id: string) : Observable<any> {
|
---|
| 190 | return this.http.post<any>(this.basePharmacyHead + '/delete/' + id, id)
|
---|
| 191 | .pipe(
|
---|
| 192 | map((data) => {
|
---|
| 193 | return data;
|
---|
| 194 | }),
|
---|
| 195 | catchError(this.handleError)
|
---|
| 196 | );
|
---|
[d23bf72] | 197 | }
|
---|
[e0cdea2] | 198 | deleteClaimingRequest(req: IPharmacyHeadRequest) : Observable<boolean> {
|
---|
| 199 | return this.http.post<boolean>(this.basePharmacyHead + '/requests/' + req.id, req)
|
---|
[de9d697] | 200 | .pipe(
|
---|
| 201 | map((data) => {
|
---|
| 202 | return data;
|
---|
| 203 | }),
|
---|
| 204 | catchError(this.handleError)
|
---|
| 205 | );
|
---|
[d23bf72] | 206 | }
|
---|
| 207 |
|
---|
| 208 | private handleError(error: HttpErrorResponse) {
|
---|
| 209 | console.error('server error:', error);
|
---|
| 210 | if (error.error instanceof Error) {
|
---|
| 211 | let errMessage = error.error.message;
|
---|
| 212 | return Observable.throw(errMessage);
|
---|
| 213 | }
|
---|
| 214 | return Observable.throw(error || 'ASP.NET Core server error');
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | }
|
---|