source: Farmatiko/ClientApp/src/app/shared/data.service.ts@ 7d80751

Last change on this file since 7d80751 was 68454c6, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

fix bugs

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[d23bf72]1import { Injectable, } from '@angular/core';
2import { HttpClient, HttpErrorResponse } from '@angular/common/http';
3
4import { Observable } from 'rxjs';
5import { map, catchError } from 'rxjs/operators';
6
7import { IHealthFacilities, IHealthcareWorkers, IMedicine, IPandemic, IPharmacy, IPharmacyHead, IPharmacyHeadRequest } from './interfaces';
[1db5673]8import { environment } from '../../environments/environment';
[d23bf72]9
10@Injectable()
11export 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 //PharmacyHead PUT
181 updatePharmacyHead(head: IPharmacyHead) : Observable<IPharmacyHead> {
[68454c6]182 return this.http.put<IPharmacyHead>(this.basePharmacyHead + '/update', head)
[d23bf72]183 .pipe(
184 map((data) => {
185 return data;
186 }),
187 catchError(this.handleError)
188 );
189 }
190 //PharmacyHead DELETE
191 deletePharmacyHead(id: string) : Observable<boolean> {
192 return this.http.delete<boolean>(this.basePharmacyHead + '/delete/' + id)
193 .pipe(catchError(this.handleError));
194 }
195 deleteClaimingRequest(id: string) : Observable<boolean> {
196 return this.http.delete<boolean>(this.basePharmacyHead + '/requests/' + id)
197 .pipe(catchError(this.handleError));
198 }
199
200 private handleError(error: HttpErrorResponse) {
201 console.error('server error:', error);
202 if (error.error instanceof Error) {
203 let errMessage = error.error.message;
204 return Observable.throw(errMessage);
205 }
206 return Observable.throw(error || 'ASP.NET Core server error');
207 }
208
209}
Note: See TracBrowser for help on using the repository browser.