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