source: FarmatikoServices/Services/Service.cs@ 6e6f04b

Last change on this file since 6e6f04b was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 3 years ago

Fix all bugs

  • Property mode set to 100644
File size: 14.6 KB
Line 
1using FarmatikoData.DTOs;
2using FarmatikoData.FarmatikoRepoInterfaces;
3using FarmatikoData.Models;
4using FarmatikoServices.FarmatikoServiceInterfaces;
5using Microsoft.Extensions.Logging;
6using Newtonsoft.Json.Linq;
7using RestSharp;
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Threading.Tasks;
12
13namespace FarmatikoServices.Services
14{
15 public class Service : IService
16 {
17 private readonly IRepository _repository;
18 private readonly IPHRepo _phrepo;
19 private readonly ILogger _logger;
20 public Service(IRepository repository, IPHRepo phrepo, ILogger logger)
21 {
22 _repository = repository;
23 _phrepo = phrepo;
24 _logger = logger;
25 }
26
27 //GET
28 public async Task<IEnumerable<HealthcareWorker>> GetAllWorkers()
29 {
30 var Workers = await _repository.GetAllWorkers();
31 return Workers;
32 }
33
34 public async Task<IEnumerable<HealthFacility>> GetFacilities()
35 {
36 var Facilities = await _repository.GetFacilities();
37 return Facilities;
38 }
39
40 public async Task<HealthFacility> GetFacility(int id)
41 {
42 var Facility = await _repository.GetFacility(id);
43 return Facility;
44 }
45
46 public async Task<Medicine> GetMedicine(int id)
47 {
48 var Medicine = await _repository.GetMedicine(id);
49 return Medicine;
50 }
51
52 public async Task<List<MedicineDTO>> GetMedicines()
53 {
54 var Medicines = await _repository.GetMedicinesAsync();
55 List<MedicineDTO> list = new List<MedicineDTO>();
56 var listPHMedicines = await _repository.GetAllPHMedicines();
57 List<string> headNames = new List<string>();
58 List<PharmacyHead> heads = new List<PharmacyHead>();
59 foreach (var med in Medicines)
60 {
61 var meds = listPHMedicines.Where(x => x.MedicineId == med.Id).ToList();
62 if (meds != null)
63 {
64 heads = meds.Select(x => x.Head).ToList();
65 }
66 headNames = heads.Select(x => x.Name).ToList();
67 MedicineDTO medicine = new MedicineDTO()
68 {
69 Name = med.Name,
70 Manufacturer = med.Manufacturer,
71 Packaging = med.Packaging,
72 Form = med.Form,
73 Price = med.Price,
74 Strength = med.Strength,
75 WayOfIssuing = med.WayOfIssuing,
76 HeadNames = headNames
77 };
78
79 list.Add(medicine);
80 }
81
82 return list;
83 }
84
85 public Pandemic GetPandemic()
86 {
87 //var Pandemic = await _repository.GetPandemic();
88
89 try
90 {
91 var Date = DateTime.UtcNow.ToString("yyyy-MM-dd");
92 var client = new RestClient($"https://api.covid19tracking.narrativa.com/api/{Date}/country/north_macedonia");
93 var response = client.Execute(new RestRequest());
94 string original = response.Content;
95 var jsonResponsePandemic = JObject.Parse(original);
96 if (!jsonResponsePandemic.ContainsKey("total"))
97 {
98 Date = DateTime.UtcNow.AddDays(-1).ToString("yyyy-MM-dd");
99 client = new RestClient($"https://api.covid19tracking.narrativa.com/api/{Date}/country/north_macedonia");
100 response = client.Execute(new RestRequest());
101 original = response.Content;
102 jsonResponsePandemic = JObject.Parse(original);
103 if (!jsonResponsePandemic.ContainsKey("total"))
104 {
105 Date = DateTime.UtcNow.AddDays(-2).ToString("yyyy-MM-dd");
106 client = new RestClient($"https://api.covid19tracking.narrativa.com/api/{Date}/country/north_macedonia");
107 response = client.Execute(new RestRequest());
108 original = response.Content;
109 jsonResponsePandemic = JObject.Parse(original);
110 }
111 }
112 var global = JObject.Parse(jsonResponsePandemic.GetValue("total").ToString());
113 var TotalConfirmed = long.Parse(global.GetValue("today_confirmed").ToString());
114 var TotalDeaths = long.Parse(global.GetValue("today_deaths").ToString());
115 var TotalRecovered = long.Parse(global.GetValue("today_new_recovered").ToString());
116
117 var mk = JObject.Parse(jsonResponsePandemic.GetValue("dates").ToString());
118
119 var date = JObject.Parse(mk.GetValue(Date).ToString());
120 var country = JObject.Parse(date.GetValue("countries").ToString());
121 var mkd = JObject.Parse(country.GetValue("North Macedonia").ToString());
122 dynamic objP = mkd;
123 var TotalMk = Int32.Parse(objP.GetValue("today_confirmed").ToString());
124 var TotalDeathsMK = Int32.Parse(objP.GetValue("today_deaths").ToString());
125 var TotalRecoveredMK = Int32.Parse(objP.GetValue("today_recovered").ToString());
126 var NewMK = Int32.Parse(objP.GetValue("today_new_confirmed").ToString());
127
128 var Name = "Coronavirus";
129 var ActiveMk = TotalMk - (TotalRecoveredMK + TotalDeathsMK);
130 var ActiveGlobal = TotalConfirmed - (TotalRecovered + TotalDeaths);
131
132 Pandemic pandemic = new Pandemic(Name, TotalMk, ActiveMk, TotalDeathsMK, NewMK, TotalConfirmed, TotalDeaths, ActiveGlobal);
133 return pandemic;
134 }
135 catch (Exception e)
136 {
137 _logger.LogInformation(e.Message);
138 }
139 return null;
140 }
141
142 public async Task<List<PharmacyDTO>> GetPharmacies()
143 {
144 var Pharmacies = await _repository.GetPharmacies();
145 List<PharmacyDTO> pharmacies = new List<PharmacyDTO>();
146
147 foreach(var pharm in Pharmacies)
148 {
149 PharmacyDTO pharmacyDTO = new PharmacyDTO()
150 {
151 Name = pharm.Name,
152 Location = pharm.Location,
153 Address = pharm.Address,
154 WorkAllTime = pharm.WorkAllTime
155 };
156 if (pharm.PharmacyHead != null)
157 {
158 pharmacyDTO.HeadName = pharm.PharmacyHead.Name;
159 }
160
161 pharmacies.Add(pharmacyDTO);
162 }
163 return pharmacies;
164 }
165
166 public async Task<Pharmacy> GetPharmacy(int id)
167 {
168 var Pharmacy = await _repository.GetPharmacy(id);
169 return Pharmacy;
170 }
171
172 public async Task<HealthcareWorker> GetWorker(int id)
173 {
174 var Worker = await _repository.GetWorker(id);
175 return Worker;
176 }
177
178 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
179 {
180 var SearchQuery = await _repository.SearchFacilities(query);
181 return SearchQuery;
182 }
183
184 public async Task<IEnumerable<MedicineDTO>> SearchMedicines(string query)
185 {
186 var SearchQuery = await _repository.SearchMedicines(query);
187 List<MedicineDTO> list = new List<MedicineDTO>();
188 var listPHMedicines = await _repository.GetAllPHMedicines();
189 List<string> headNames = new List<string>();
190 List<PharmacyHead> heads = new List<PharmacyHead>();
191 foreach (var med in SearchQuery)
192 {
193 var meds = listPHMedicines.Where(x => x.MedicineId == med.Id).ToList();
194 if (meds != null)
195 {
196 heads = meds.Select(x => x.Head).ToList();
197 }
198 if (heads != null)
199 headNames = heads?.Select(x => x?.Name).ToList();
200 MedicineDTO medicine = new MedicineDTO()
201 {
202 Name = med.Name,
203 Manufacturer = med.Manufacturer,
204 Packaging = med.Packaging,
205 Form = med.Form,
206 Price = med.Price,
207 Strength = med.Strength,
208 WayOfIssuing = med.WayOfIssuing,
209 HeadNames = headNames
210 };
211
212 list.Add(medicine);
213 headNames = new List<string>();
214 }
215
216 return list;
217 }
218
219 public async Task<IEnumerable<PharmacyDTO>> SearchPharmacies(string query)
220 {
221 var SearchQuery = await _repository.SearchPharmacies(query);
222 List<PharmacyDTO> pharmacies = new List<PharmacyDTO>();
223 var heads = await _phrepo.GetPharmacyHeadInfo();
224
225 foreach (var pharm in SearchQuery)
226 {
227 PharmacyDTO pharmacyDTO = new PharmacyDTO()
228 {
229 Name = pharm.Name,
230 Location = pharm.Location,
231 Address = pharm.Address,
232 WorkAllTime = pharm.WorkAllTime
233 };
234
235 foreach(var head in heads.ToList())
236 {
237 if (head.Pharmacies.Contains(pharm))
238 {
239 pharmacyDTO.HeadName = head.Name;
240 break;
241 }
242 }
243
244 pharmacies.Add(pharmacyDTO);
245 }
246 return pharmacies;
247 }
248
249 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
250 {
251 var SearchQuery = await _repository.SearchWorkers(query);
252 return SearchQuery;
253 }
254
255
256 //POST (ADD NEW OBJECTS)
257 //za json(Sys updateer)
258 public async Task AddFacility(HealthFacility healthFacilities)
259 {
260 if (healthFacilities != null)
261 await _repository.AddFacility(healthFacilities);
262 else throw new Exception("Facility is null");
263 }
264 //za json(Sys updateer)
265 public async Task AddMedicines(Medicine medicine)
266 {
267 if (medicine != null)
268 await _repository.AddMedicines(medicine);
269 else throw new Exception("Medicine is null");
270 }
271 //za json(Sys updateer)
272 public async Task AddPandemic(Pandemic pandemic)
273 {
274 if (pandemic != null)
275 await _repository.AddPandemic(pandemic);
276 else throw new Exception("Pandemic is null");
277 }
278 // Samo PharmacyHead i Admin imaat pristap
279 public void AddPharmacy(Pharmacy pharmacy)
280 {
281 if (pharmacy != null)
282 _repository.AddPharmacy(pharmacy);
283 else throw new Exception("Pharmacy is null");
284 }
285
286 // Ovaa kontrola ja ima samo admin
287
288 public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead)
289 {
290 if (pharmacyHead != null)
291 {
292 var phead = new PharmacyHead()
293 {
294 Name = pharmacyHead.Name,
295 Email = pharmacyHead.Email,
296 Password = pharmacyHead.Password
297 };
298 User user = new User()
299 {
300 Name = phead.Name,
301 Password = phead.Password,
302 Email = phead.Email,
303 UserRole = User.Role.PharmacyHead
304 };
305 if (user is null)
306 {
307 return false;
308 }
309 User user1 = new User()
310 {
311 Name = user.Name,
312 Password = user.Password,
313 Email = user.Email,
314 UserRole = user.UserRole
315 };
316 phead.User = user1;
317 await _repository.AddPharmacyHead(phead);
318 return true;
319 }
320 else throw new Exception("PharmacyHeadDto is null");
321 }
322 //za json(Sys updater)
323 public async Task AddWorker(HealthcareWorker worker)
324 {
325 if (worker != null)
326 await _repository.AddWorker(worker);
327 else throw new Exception("Worker is null");
328 }
329
330 //za json(Sys updateer)
331 public async Task UpdateFacility(HealthFacility healthFacilities)
332 {
333 if (healthFacilities != null)
334 await _repository.UpdateFacility(healthFacilities);
335 else throw new Exception("Facility is null");
336 }
337 //PharmacyHead
338 public async Task RemoveMedicine(Medicine medicine)
339 {
340 if (medicine != null)
341 await _repository.RemoveMedicine(medicine);
342 else throw new Exception("Medicine is null");
343 }
344 //PharmacyHead
345 public async Task UpdateMedicine(Medicine medicine)
346 {
347 if (medicine != null)
348 await _repository.UpdateMedicine(medicine);
349 else throw new Exception("Medicine is null");
350 }
351 //za json(Sys updateer)
352 public async Task UpdatePandemic(Pandemic pandemic)
353 {
354 if (pandemic != null)
355 await _repository.UpdatePandemic(pandemic);
356 else throw new Exception("Pandemic is null");
357 }
358 //PharmacyHead
359 public async Task RemovePharmacy(Pharmacy pharmacy)
360 {
361 if (pharmacy != null)
362 await _repository.RemovePharmacy(pharmacy);
363 else throw new Exception("Pharmacy is null");
364 }
365 //PharamcyHead
366 public async Task UpdatePharmacy(Pharmacy pharmacy)
367 {
368 if (pharmacy != null)
369 await _repository.UpadatePharmacy(pharmacy);
370 else throw new Exception("Pharmacy is null");
371 }
372 //za json(Sys updateer)
373 public async Task UpdateWorker(HealthcareWorker worker)
374 {
375 if (worker != null)
376 await _repository.UpdateWorker(worker);
377 else throw new Exception("Worker is null");
378 }
379
380 public async Task RemovePharmacyHead(int Id)
381 {
382 if (Id > 0)
383 {
384 await _repository.RemovePharmacyHead(Id);
385 }
386 else throw new Exception("Index out of bounds.");
387 }
388
389 public HealthFacility GetFacilityJSON(string healthFacility)
390 {
391 if (healthFacility != null)
392 return _repository.GetFacilityJSON(healthFacility);
393 return null;
394 }
395
396 //PUT (EDIT OBJECTS)
397
398
399 //DELETE
400
401 }
402}
Note: See TracBrowser for help on using the repository browser.