[afefe75] | 1 | using FarmatikoData.DTOs;
|
---|
| 2 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
[5d02859] | 3 | using FarmatikoData.Models;
|
---|
| 4 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
[e0cdea2] | 5 | using Microsoft.Extensions.Logging;
|
---|
| 6 | using Newtonsoft.Json.Linq;
|
---|
| 7 | using RestSharp;
|
---|
[5d02859] | 8 | using System;
|
---|
[6f203af] | 9 | using System.Collections.Generic;
|
---|
[5d02859] | 10 | using System.Linq;
|
---|
| 11 | using System.Threading.Tasks;
|
---|
| 12 |
|
---|
| 13 | namespace FarmatikoServices.Services
|
---|
| 14 | {
|
---|
| 15 | public class Service : IService
|
---|
| 16 | {
|
---|
| 17 | private readonly IRepository _repository;
|
---|
[e0cdea2] | 18 | private readonly IPHRepo _phrepo;
|
---|
| 19 | private readonly ILogger _logger;
|
---|
| 20 | public Service(IRepository repository, IPHRepo phrepo, ILogger logger)
|
---|
[5d02859] | 21 | {
|
---|
| 22 | _repository = repository;
|
---|
[e0cdea2] | 23 | _phrepo = phrepo;
|
---|
| 24 | _logger = logger;
|
---|
[5d02859] | 25 | }
|
---|
| 26 |
|
---|
| 27 | //GET
|
---|
[6f203af] | 28 | public async Task<IEnumerable<HealthcareWorker>> GetAllWorkers()
|
---|
[5d02859] | 29 | {
|
---|
| 30 | var Workers = await _repository.GetAllWorkers();
|
---|
| 31 | return Workers;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[6f203af] | 34 | public async Task<IEnumerable<HealthFacility>> GetFacilities()
|
---|
[5d02859] | 35 | {
|
---|
| 36 | var Facilities = await _repository.GetFacilities();
|
---|
| 37 | return Facilities;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[1454207] | 40 | public async Task<HealthFacility> GetFacility(int id)
|
---|
[5d02859] | 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 |
|
---|
[afefe75] | 52 | public async Task<List<MedicineDTO>> GetMedicines()
|
---|
[5d02859] | 53 | {
|
---|
[1db5673] | 54 | var Medicines = await _repository.GetMedicinesAsync();
|
---|
[afefe75] | 55 | List<MedicineDTO> list = new List<MedicineDTO>();
|
---|
| 56 | var listPHMedicines = await _repository.GetAllPHMedicines();
|
---|
[0a694bb] | 57 | List<string> headNames = new List<string>();
|
---|
| 58 | List<PharmacyHead> heads = new List<PharmacyHead>();
|
---|
| 59 | foreach (var med in Medicines)
|
---|
[afefe75] | 60 | {
|
---|
[0a694bb] | 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 | }
|
---|
[afefe75] | 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;
|
---|
[5d02859] | 83 | }
|
---|
| 84 |
|
---|
[e0cdea2] | 85 | public Pandemic GetPandemic()
|
---|
[5d02859] | 86 | {
|
---|
[e0cdea2] | 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;
|
---|
[5d02859] | 140 | }
|
---|
| 141 |
|
---|
[afefe75] | 142 | public async Task<List<PharmacyDTO>> GetPharmacies()
|
---|
[5d02859] | 143 | {
|
---|
| 144 | var Pharmacies = await _repository.GetPharmacies();
|
---|
[afefe75] | 145 | List<PharmacyDTO> pharmacies = new List<PharmacyDTO>();
|
---|
[e0cdea2] | 146 |
|
---|
[afefe75] | 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,
|
---|
[0a694bb] | 154 | WorkAllTime = pharm.WorkAllTime
|
---|
[afefe75] | 155 | };
|
---|
[0a694bb] | 156 | if (pharm.PharmacyHead != null)
|
---|
| 157 | {
|
---|
| 158 | pharmacyDTO.HeadName = pharm.PharmacyHead.Name;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[afefe75] | 161 | pharmacies.Add(pharmacyDTO);
|
---|
| 162 | }
|
---|
| 163 | return pharmacies;
|
---|
[5d02859] | 164 | }
|
---|
| 165 |
|
---|
| 166 | public async Task<Pharmacy> GetPharmacy(int id)
|
---|
| 167 | {
|
---|
| 168 | var Pharmacy = await _repository.GetPharmacy(id);
|
---|
| 169 | return Pharmacy;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[1454207] | 172 | public async Task<HealthcareWorker> GetWorker(int id)
|
---|
[5d02859] | 173 | {
|
---|
| 174 | var Worker = await _repository.GetWorker(id);
|
---|
| 175 | return Worker;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[6f203af] | 178 | public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
|
---|
[5d02859] | 179 | {
|
---|
| 180 | var SearchQuery = await _repository.SearchFacilities(query);
|
---|
| 181 | return SearchQuery;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[e0cdea2] | 184 | public async Task<IEnumerable<MedicineDTO>> SearchMedicines(string query)
|
---|
[5d02859] | 185 | {
|
---|
| 186 | var SearchQuery = await _repository.SearchMedicines(query);
|
---|
[e0cdea2] | 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;
|
---|
[5d02859] | 217 | }
|
---|
| 218 |
|
---|
[e0cdea2] | 219 | public async Task<IEnumerable<PharmacyDTO>> SearchPharmacies(string query)
|
---|
[5d02859] | 220 | {
|
---|
| 221 | var SearchQuery = await _repository.SearchPharmacies(query);
|
---|
[e0cdea2] | 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;
|
---|
[5d02859] | 247 | }
|
---|
| 248 |
|
---|
[6f203af] | 249 | public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
|
---|
[5d02859] | 250 | {
|
---|
| 251 | var SearchQuery = await _repository.SearchWorkers(query);
|
---|
| 252 | return SearchQuery;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 | //POST (ADD NEW OBJECTS)
|
---|
[1454207] | 257 | //za json(Sys updateer)
|
---|
| 258 | public async Task AddFacility(HealthFacility healthFacilities)
|
---|
[5d02859] | 259 | {
|
---|
| 260 | if (healthFacilities != null)
|
---|
[1454207] | 261 | await _repository.AddFacility(healthFacilities);
|
---|
[d23bf72] | 262 | else throw new Exception("Facility is null");
|
---|
[5d02859] | 263 | }
|
---|
[1454207] | 264 | //za json(Sys updateer)
|
---|
| 265 | public async Task AddMedicines(Medicine medicine)
|
---|
[5d02859] | 266 | {
|
---|
| 267 | if (medicine != null)
|
---|
[1454207] | 268 | await _repository.AddMedicines(medicine);
|
---|
[d23bf72] | 269 | else throw new Exception("Medicine is null");
|
---|
[5d02859] | 270 | }
|
---|
[1454207] | 271 | //za json(Sys updateer)
|
---|
| 272 | public async Task AddPandemic(Pandemic pandemic)
|
---|
[5d02859] | 273 | {
|
---|
| 274 | if (pandemic != null)
|
---|
[1454207] | 275 | await _repository.AddPandemic(pandemic);
|
---|
[d23bf72] | 276 | else throw new Exception("Pandemic is null");
|
---|
[5d02859] | 277 | }
|
---|
[1454207] | 278 | // Samo PharmacyHead i Admin imaat pristap
|
---|
[e0cdea2] | 279 | public void AddPharmacy(Pharmacy pharmacy)
|
---|
[5d02859] | 280 | {
|
---|
| 281 | if (pharmacy != null)
|
---|
[e0cdea2] | 282 | _repository.AddPharmacy(pharmacy);
|
---|
[d23bf72] | 283 | else throw new Exception("Pharmacy is null");
|
---|
[5d02859] | 284 | }
|
---|
[1db5673] | 285 |
|
---|
[1454207] | 286 | // Ovaa kontrola ja ima samo admin
|
---|
[e0cdea2] | 287 |
|
---|
[0a694bb] | 288 | public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead)
|
---|
[5d02859] | 289 | {
|
---|
| 290 | if (pharmacyHead != null)
|
---|
[1454207] | 291 | {
|
---|
[0a694bb] | 292 | var phead = new PharmacyHead()
|
---|
| 293 | {
|
---|
| 294 | Name = pharmacyHead.Name,
|
---|
| 295 | Email = pharmacyHead.Email,
|
---|
| 296 | Password = pharmacyHead.Password
|
---|
| 297 | };
|
---|
[e0cdea2] | 298 | User user = new User()
|
---|
| 299 | {
|
---|
| 300 | Name = phead.Name,
|
---|
| 301 | Password = phead.Password,
|
---|
| 302 | Email = phead.Email,
|
---|
| 303 | UserRole = User.Role.PharmacyHead
|
---|
| 304 | };
|
---|
[0a694bb] | 305 | if (user is null)
|
---|
| 306 | {
|
---|
| 307 | return false;
|
---|
| 308 | }
|
---|
| 309 | User user1 = new User()
|
---|
| 310 | {
|
---|
[e0cdea2] | 311 | Name = user.Name,
|
---|
| 312 | Password = user.Password,
|
---|
| 313 | Email = user.Email,
|
---|
| 314 | UserRole = user.UserRole
|
---|
[0a694bb] | 315 | };
|
---|
| 316 | phead.User = user1;
|
---|
| 317 | await _repository.AddPharmacyHead(phead);
|
---|
| 318 | return true;
|
---|
[1454207] | 319 | }
|
---|
[0a694bb] | 320 | else throw new Exception("PharmacyHeadDto is null");
|
---|
[5d02859] | 321 | }
|
---|
[afc9a9a] | 322 | //za json(Sys updater)
|
---|
[1454207] | 323 | public async Task AddWorker(HealthcareWorker worker)
|
---|
| 324 | {
|
---|
| 325 | if (worker != null)
|
---|
| 326 | await _repository.AddWorker(worker);
|
---|
[d23bf72] | 327 | else throw new Exception("Worker is null");
|
---|
[1454207] | 328 | }
|
---|
[5d02859] | 329 |
|
---|
[1454207] | 330 | //za json(Sys updateer)
|
---|
| 331 | public async Task UpdateFacility(HealthFacility healthFacilities)
|
---|
| 332 | {
|
---|
| 333 | if (healthFacilities != null)
|
---|
| 334 | await _repository.UpdateFacility(healthFacilities);
|
---|
[d23bf72] | 335 | else throw new Exception("Facility is null");
|
---|
[1454207] | 336 | }
|
---|
| 337 | //PharmacyHead
|
---|
| 338 | public async Task RemoveMedicine(Medicine medicine)
|
---|
| 339 | {
|
---|
| 340 | if (medicine != null)
|
---|
| 341 | await _repository.RemoveMedicine(medicine);
|
---|
[d23bf72] | 342 | else throw new Exception("Medicine is null");
|
---|
[1454207] | 343 | }
|
---|
| 344 | //PharmacyHead
|
---|
| 345 | public async Task UpdateMedicine(Medicine medicine)
|
---|
| 346 | {
|
---|
| 347 | if (medicine != null)
|
---|
| 348 | await _repository.UpdateMedicine(medicine);
|
---|
[d23bf72] | 349 | else throw new Exception("Medicine is null");
|
---|
[1454207] | 350 | }
|
---|
| 351 | //za json(Sys updateer)
|
---|
| 352 | public async Task UpdatePandemic(Pandemic pandemic)
|
---|
| 353 | {
|
---|
| 354 | if (pandemic != null)
|
---|
| 355 | await _repository.UpdatePandemic(pandemic);
|
---|
[d23bf72] | 356 | else throw new Exception("Pandemic is null");
|
---|
[1454207] | 357 | }
|
---|
| 358 | //PharmacyHead
|
---|
| 359 | public async Task RemovePharmacy(Pharmacy pharmacy)
|
---|
| 360 | {
|
---|
| 361 | if (pharmacy != null)
|
---|
| 362 | await _repository.RemovePharmacy(pharmacy);
|
---|
[d23bf72] | 363 | else throw new Exception("Pharmacy is null");
|
---|
[1454207] | 364 | }
|
---|
| 365 | //PharamcyHead
|
---|
| 366 | public async Task UpdatePharmacy(Pharmacy pharmacy)
|
---|
| 367 | {
|
---|
| 368 | if (pharmacy != null)
|
---|
| 369 | await _repository.UpadatePharmacy(pharmacy);
|
---|
[d23bf72] | 370 | else throw new Exception("Pharmacy is null");
|
---|
[1454207] | 371 | }
|
---|
| 372 | //za json(Sys updateer)
|
---|
| 373 | public async Task UpdateWorker(HealthcareWorker worker)
|
---|
[5d02859] | 374 | {
|
---|
| 375 | if (worker != null)
|
---|
[1454207] | 376 | await _repository.UpdateWorker(worker);
|
---|
[d23bf72] | 377 | else throw new Exception("Worker is null");
|
---|
[5d02859] | 378 | }
|
---|
| 379 |
|
---|
[6f203af] | 380 | public async Task RemovePharmacyHead(int Id)
|
---|
[1454207] | 381 | {
|
---|
[6f203af] | 382 | if (Id > 0)
|
---|
[1454207] | 383 | {
|
---|
[6f203af] | 384 | await _repository.RemovePharmacyHead(Id);
|
---|
[1454207] | 385 | }
|
---|
[d23bf72] | 386 | else throw new Exception("Index out of bounds.");
|
---|
[1454207] | 387 | }
|
---|
| 388 |
|
---|
[d23bf72] | 389 | public HealthFacility GetFacilityJSON(string healthFacility)
|
---|
[1454207] | 390 | {
|
---|
| 391 | if (healthFacility != null)
|
---|
[d23bf72] | 392 | return _repository.GetFacilityJSON(healthFacility);
|
---|
[1454207] | 393 | return null;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[5d02859] | 396 | //PUT (EDIT OBJECTS)
|
---|
| 397 |
|
---|
| 398 |
|
---|
| 399 | //DELETE
|
---|
| 400 |
|
---|
| 401 | }
|
---|
| 402 | }
|
---|