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/medicines';
|
---|
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 | //PharmacyHead POST
|
---|
147 | insertPharmacyHead(head: IPharmacyHead) : Observable<IPharmacyHead> {
|
---|
148 | return this.http.post<IPharmacyHead>(this.basePharmacyHead + '/add', head)
|
---|
149 | .pipe(
|
---|
150 | map((data) => {
|
---|
151 | return data;
|
---|
152 | }),
|
---|
153 | catchError(this.handleError)
|
---|
154 | );
|
---|
155 | }
|
---|
156 | claimPharmacy(req: IPharmacyHeadRequest) : Observable<IPharmacyHeadRequest> {
|
---|
157 | return this.http.post<IPharmacyHeadRequest>(this.basePharmacyHead + '/requests', req)
|
---|
158 | .pipe(
|
---|
159 | map((data) => {
|
---|
160 | return data;
|
---|
161 | }),
|
---|
162 | catchError(this.handleError)
|
---|
163 | );
|
---|
164 | }
|
---|
165 | //PharmacyHead PUT
|
---|
166 | updatePharmacyHead(head: IPharmacyHead) : Observable<IPharmacyHead> {
|
---|
167 | return this.http.put<IPharmacyHead>(this.basePharmacyHead + '/' + head.id, head)
|
---|
168 | .pipe(
|
---|
169 | map((data) => {
|
---|
170 | return data;
|
---|
171 | }),
|
---|
172 | catchError(this.handleError)
|
---|
173 | );
|
---|
174 | }
|
---|
175 | //PharmacyHead DELETE
|
---|
176 | deletePharmacyHead(id: string) : Observable<boolean> {
|
---|
177 | return this.http.delete<boolean>(this.basePharmacyHead + '/delete/' + id)
|
---|
178 | .pipe(catchError(this.handleError));
|
---|
179 | }
|
---|
180 | deleteClaimingRequest(id: string) : Observable<boolean> {
|
---|
181 | return this.http.delete<boolean>(this.basePharmacyHead + '/requests/' + id)
|
---|
182 | .pipe(catchError(this.handleError));
|
---|
183 | }
|
---|
184 |
|
---|
185 | private handleError(error: HttpErrorResponse) {
|
---|
186 | console.error('server error:', error);
|
---|
187 | if (error.error instanceof Error) {
|
---|
188 | let errMessage = error.error.message;
|
---|
189 | return Observable.throw(errMessage);
|
---|
190 | }
|
---|
191 | return Observable.throw(error || 'ASP.NET Core server error');
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|