Changeset e0cdea2 for FarmatikoServices/Services/Service.cs
- Timestamp:
- 02/08/21 16:39:25 (4 years ago)
- Branches:
- master
- Children:
- 8eb1e21, dae4cde
- Parents:
- 0a694bb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
FarmatikoServices/Services/Service.cs
r0a694bb re0cdea2 3 3 using FarmatikoData.Models; 4 4 using FarmatikoServices.FarmatikoServiceInterfaces; 5 using Microsoft.Extensions.Logging; 6 using Newtonsoft.Json.Linq; 7 using RestSharp; 5 8 using System; 6 9 using System.Collections.Generic; … … 13 16 { 14 17 private readonly IRepository _repository; 15 public Service(IRepository repository) 18 private readonly IPHRepo _phrepo; 19 private readonly ILogger _logger; 20 public Service(IRepository repository, IPHRepo phrepo, ILogger logger) 16 21 { 17 22 _repository = repository; 23 _phrepo = phrepo; 24 _logger = logger; 18 25 } 19 26 … … 76 83 } 77 84 78 public async Task<Pandemic> GetPandemic() 79 { 80 var Pandemic = await _repository.GetPandemic(); 81 return Pandemic; 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; 82 140 } 83 141 … … 86 144 var Pharmacies = await _repository.GetPharmacies(); 87 145 List<PharmacyDTO> pharmacies = new List<PharmacyDTO>(); 88 146 89 147 foreach(var pharm in Pharmacies) 90 148 { … … 124 182 } 125 183 126 public async Task<IEnumerable<Medicine >> SearchMedicines(string query)184 public async Task<IEnumerable<MedicineDTO>> SearchMedicines(string query) 127 185 { 128 186 var SearchQuery = await _repository.SearchMedicines(query); 129 return SearchQuery; 130 } 131 132 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string 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) 133 220 { 134 221 var SearchQuery = await _repository.SearchPharmacies(query); 135 return SearchQuery; 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; 136 247 } 137 248 … … 166 277 } 167 278 // Samo PharmacyHead i Admin imaat pristap 168 public async TaskAddPharmacy(Pharmacy pharmacy)279 public void AddPharmacy(Pharmacy pharmacy) 169 280 { 170 281 if (pharmacy != null) 171 await_repository.AddPharmacy(pharmacy);282 _repository.AddPharmacy(pharmacy); 172 283 else throw new Exception("Pharmacy is null"); 173 284 } 174 285 175 286 // Ovaa kontrola ja ima samo admin 176 public async Task<User> MakeUser(PharmacyHead head) 177 { 178 User user = new User() 179 { 180 Name = head.Name, 181 Password = head.Password, 182 Email = head.Email, 183 UserRole = User.Role.PharmacyHead 184 }; 185 bool Success = await _repository.AddUser(user); 186 if (!Success) 187 return null; 188 return user; 189 } 287 190 288 public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead) 191 289 { … … 198 296 Password = pharmacyHead.Password 199 297 }; 200 var user = await MakeUser(phead); 298 User user = new User() 299 { 300 Name = phead.Name, 301 Password = phead.Password, 302 Email = phead.Email, 303 UserRole = User.Role.PharmacyHead 304 }; 201 305 if (user is null) 202 306 { 203 307 return false; 204 308 } 205 var users = _repository.GetUsers();206 var thisUser = users.Where(usr => usr.Value.Email.Equals(user.Email)).Select(x => x.Value).FirstOrDefault();207 309 User user1 = new User() 208 310 { 209 Name = thisUser.Name,210 Password = thisUser.Password,211 Email = thisUser.Email,212 UserRole = thisUser.UserRole311 Name = user.Name, 312 Password = user.Password, 313 Email = user.Email, 314 UserRole = user.UserRole 213 315 }; 214 316 phead.User = user1;
Note:
See TracChangeset
for help on using the changeset viewer.