source: FarmatikoServices/Services/Service.cs@ 472dd7e

Last change on this file since 472dd7e was f554983, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Add cron job for updating data, with Quartz.NET

  • Property mode set to 100644
File size: 15.4 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 healthFacility)
259 {
260 if (healthFacility != null)
261 {
262 var facilities = await _repository.GetFacilities();
263 if (!facilities.Contains(healthFacility))
264 {
265 await _repository.AddFacility(healthFacility);
266 }
267 else throw new Exception("The facility already exists.");
268 }
269 else throw new Exception("Facility is null");
270 }
271 //za json(Sys updateer)
272 public async Task AddMedicines(Medicine medicine)
273 {
274 if (medicine != null)
275 {
276 var medicines = await _repository.GetMedicinesAsync();
277 if (!medicines.Contains(medicine))
278 await _repository.AddMedicines(medicine);
279 else throw new Exception("Medicine already exists.");
280 }
281 else throw new Exception("Medicine is null");
282 }
283 //za json(Sys updateer)
284 public async Task AddPandemic(Pandemic pandemic)
285 {
286 if (pandemic != null)
287 await _repository.AddPandemic(pandemic);
288 else throw new Exception("Pandemic is null");
289 }
290 // Samo PharmacyHead i Admin imaat pristap
291 public async void AddPharmacy(Pharmacy pharmacy)
292 {
293 if (pharmacy != null)
294 {
295 var pharmacies = await _repository.GetPharmacies();
296 if (!pharmacies.Contains(pharmacy))
297 await _repository.AddPharmacy(pharmacy);
298 else throw new Exception("Pharmacy already exists.");
299 }
300 else throw new Exception("Pharmacy is null");
301 }
302
303 // Ovaa kontrola ja ima samo admin
304
305 public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead)
306 {
307 if (pharmacyHead != null)
308 {
309 var phead = new PharmacyHead()
310 {
311 Name = pharmacyHead.Name,
312 Email = pharmacyHead.Email,
313 Password = pharmacyHead.Password
314 };
315 User user = new User()
316 {
317 Name = phead.Name,
318 Password = phead.Password,
319 Email = phead.Email,
320 UserRole = User.Role.PharmacyHead
321 };
322 if (user is null)
323 {
324 return false;
325 }
326 User user1 = new User()
327 {
328 Name = user.Name,
329 Password = user.Password,
330 Email = user.Email,
331 UserRole = user.UserRole
332 };
333 phead.User = user1;
334 await _repository.AddPharmacyHead(phead);
335 return true;
336 }
337 else throw new Exception("PharmacyHeadDto is null");
338 }
339 //za json(Sys updater)
340 public async Task AddWorker(HealthcareWorker worker)
341 {
342 if (worker != null)
343 {
344 var workers = await _repository.GetAllWorkers();
345 if (!workers.Contains(worker))
346 await _repository.AddWorker(worker);
347 else throw new Exception("Worker already exists.");
348 }
349 else throw new Exception("Worker is null");
350 }
351
352 //za json(Sys updateer)
353 public async Task UpdateFacility(HealthFacility healthFacilities)
354 {
355 if (healthFacilities != null)
356 await _repository.UpdateFacility(healthFacilities);
357 else throw new Exception("Facility is null");
358 }
359 //PharmacyHead
360 public async Task RemoveMedicine(Medicine medicine)
361 {
362 if (medicine != null)
363 await _repository.RemoveMedicine(medicine);
364 else throw new Exception("Medicine is null");
365 }
366 //PharmacyHead
367 public async Task UpdateMedicine(Medicine medicine)
368 {
369 if (medicine != null)
370 await _repository.UpdateMedicine(medicine);
371 else throw new Exception("Medicine is null");
372 }
373 //za json(Sys updateer)
374 public async Task UpdatePandemic(Pandemic pandemic)
375 {
376 if (pandemic != null)
377 await _repository.UpdatePandemic(pandemic);
378 else throw new Exception("Pandemic is null");
379 }
380 //PharmacyHead
381 public async Task RemovePharmacy(Pharmacy pharmacy)
382 {
383 if (pharmacy != null)
384 await _repository.RemovePharmacy(pharmacy);
385 else throw new Exception("Pharmacy is null");
386 }
387 //PharamcyHead
388 public async Task UpdatePharmacy(Pharmacy pharmacy)
389 {
390 if (pharmacy != null)
391 await _repository.UpadatePharmacy(pharmacy);
392 else throw new Exception("Pharmacy is null");
393 }
394 //za json(Sys updateer)
395 public async Task UpdateWorker(HealthcareWorker worker)
396 {
397 if (worker != null)
398 await _repository.UpdateWorker(worker);
399 else throw new Exception("Worker is null");
400 }
401
402 public async Task RemovePharmacyHead(int Id)
403 {
404 if (Id > 0)
405 {
406 await _repository.RemovePharmacyHead(Id);
407 }
408 else throw new Exception("Index out of bounds.");
409 }
410
411 public HealthFacility GetFacilityJSON(string healthFacility)
412 {
413 if (healthFacility != null)
414 return _repository.GetFacilityJSON(healthFacility);
415 return null;
416 }
417
418 //PUT (EDIT OBJECTS)
419
420
421 //DELETE
422
423 }
424}
Note: See TracBrowser for help on using the repository browser.