Changeset e0cdea2 for FarmatikoServices/Services
- Timestamp:
- 02/08/21 16:39:25 (4 years ago)
- Branches:
- master
- Children:
- 8eb1e21, dae4cde
- Parents:
- 0a694bb
- Location:
- FarmatikoServices/Services
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
FarmatikoServices/Services/AdminService.cs
r0a694bb re0cdea2 36 36 } 37 37 38 public bool RejectRequest( int Id)38 public bool RejectRequest(RequestPharmacyHead req) 39 39 { 40 if ( Id >= 0)40 if (req != null) 41 41 { 42 _adminRepo.RemoveClaimRequest( Id);42 _adminRepo.RemoveClaimRequest(req); 43 43 return true; 44 44 } -
FarmatikoServices/Services/PHService.cs
r0a694bb re0cdea2 65 65 66 66 phead.Medicines = _repository.GetPHMedicines(phead.Email).ToList(); 67 67 68 var phmeds = await _repository.GetAllPHMedicines(); 69 68 70 List<Medicine> medicines = _repository.GetMedicines().ToList(); 69 70 List<Medicine> PHMedicines = medicines.Where(x => x.Id == phead.Medicines.Select(x => x.MedicineId).Single()).ToList();71 71 72 List<Medicine> PHMedicines = new List<Medicine>(); 73 72 74 List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>(); 73 75 74 75 if (pharmacyHead.Medicines != null || pharmacyHead.Medicines.Count() > 0) 76 77 if (pharmacyHead.Medicines != null && pharmacyHead.Medicines.Count() > 0) 78 { 79 foreach (var med in phead.Medicines) 80 { 81 var medicine = medicines.Where(x => x.Id == med.MedicineId).FirstOrDefault(); 82 if (medicine != null) 83 PHMedicines.Add(medicine); 84 } 85 76 86 if (!pharmacyHead.Medicines.Equals(PHMedicines)) 77 87 { … … 86 96 return; 87 97 } 88 foreach (var med in pharmacyHead.Medicines) 89 { 90 91 PharmacyHeadMedicine PHMObj = phead.Medicines.Select(x => new PharmacyHeadMedicine 98 if (phead.Medicines != null && phead.Medicines.Count() > 0) 99 { 100 foreach (var med in pharmacyHead.Medicines) 92 101 { 93 Id = x.Id, 94 PheadId = x.PheadId, 95 Head = x.Head, 96 MedicineId = x.MedicineId, 97 Medicine = x.Medicine 98 }).Where(x => !x.Medicine.Equals(med)).Single(); 99 if (PHMObj == null || PHMObj == default) 100 break; 101 if (PHMObj.MedicineId == med.Id) 102 list.Add(PHMObj); 103 104 } 102 Medicine medicine = new Medicine() 103 { 104 Name = med.Name, 105 Form = med.Form, 106 Manufacturer = med.Manufacturer, 107 Medicines = med.Medicines, 108 Packaging = med.Packaging, 109 Price = med.Price, 110 Strength = med.Strength, 111 WayOfIssuing = med.WayOfIssuing 112 }; 113 114 PharmacyHeadMedicine phm = new PharmacyHeadMedicine() 115 { 116 PheadId = phead.Id, 117 Head = phead, 118 MedicineId = med.Id, 119 Medicine = medicine 120 }; 121 122 bool ifExists = phead.Medicines.Contains(phm); 123 if (!ifExists) 124 list.Add(phm); 125 126 } 127 } 128 else 129 { 130 foreach (var med in pharmacyHead.Medicines) 131 { 132 PharmacyHead head1 = new PharmacyHead() 133 { 134 Id = pharmacyHead.Id, 135 Name = pharmacyHead.Name, 136 Email = pharmacyHead.Email, 137 Password = pharmacyHead.Password 138 }; 139 PharmacyHeadMedicine phMed = new PharmacyHeadMedicine() 140 { 141 Head = head1, 142 Medicine = med 143 }; 144 list.Add(phMed); 145 } 146 } 147 105 148 106 149 phead.Medicines = list; … … 109 152 110 153 } 111 112 if (pharmacyHead.Pharmacies != null || pharmacyHead.Pharmacies.Count() > 0) 113 { 114 phead.Pharmacies = pharmacyHead.Pharmacies; 115 } 154 } 155 116 156 PharmacyHead head = new PharmacyHead() 117 157 { … … 120 160 Password = pharmacyHead.Password 121 161 }; 162 163 if (pharmacyHead.Pharmacies != null) 164 { 165 if (pharmacyHead.Pharmacies.Count() > 0) 166 { 167 phead.Pharmacies = pharmacyHead.Pharmacies; 168 head.Pharmacies = pharmacyHead.Pharmacies; 169 } 170 } 171 122 172 if (!phead.Equals(head)) 123 173 { -
FarmatikoServices/Services/ProcessJSONService.cs
r0a694bb re0cdea2 1 using FarmatikoData.FarmatikoRepoInterfaces; 2 using FarmatikoData.Models; 3 using System.Collections.Generic; 1 using FarmatikoData.Models; 4 2 using Newtonsoft.Json; 5 3 using Newtonsoft.Json.Linq; … … 13 11 using OfficeOpenXml; 14 12 using System.IO; 15 using Microsoft.VisualBasic; 13 using GemBox.Spreadsheet; 14 using System.Text; 16 15 17 16 namespace FarmatikoServices.Services … … 28 27 } 29 28 //Excel reader 30 private async Task<bool> ReadPharmaciesFromExcel(string Path) 31 { 32 using (var package = new ExcelPackage(new FileInfo(Path))) 33 { 34 var Sheet = package.Workbook.Worksheets[1]; 35 for (int i = 2; i < Sheet.Dimension.End.Row; ++i) 36 { 37 Pharmacy pharmacy = new Pharmacy() 29 private bool ReadPharmaciesFromExcel(string Path) 30 { 31 string path = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx"; 32 33 FileInfo fileInfo = new FileInfo(path); 34 if (fileInfo != null) 35 { 36 using (var package = new ExcelPackage(fileInfo)) 37 { 38 var Sheet = package.Workbook.Worksheets.First(); 39 //var table = Sheet.Tables.First(); 40 int rowCount = Sheet.Dimension.End.Row; 41 for (int i = 2; i < rowCount; ++i) 38 42 { 39 Name = Sheet.Cells[i, 2].Value.ToString(), 40 Address = Sheet.Cells[i, 3].Value.ToString(), 41 Location = Sheet.Cells[i, 4].Value.ToString(), 42 WorkAllTime = false 43 }; 44 await _service.AddPharmacy(pharmacy); 43 //Console.WriteLine(); 44 Pharmacy pharmacy = new Pharmacy() 45 { 46 Name = Sheet.Cells[i, 2].Value.ToString(), 47 Address = Sheet.Cells[i, 3].Value.ToString(), 48 Location = Sheet.Cells[i, 4].Value.ToString(), 49 WorkAllTime = false 50 }; 51 _service.AddPharmacy(pharmacy); 52 } 45 53 return true; 46 54 } 47 } 55 56 } 57 48 58 return false; 49 } 50 public async void DownloadPharmaciesExcel() 59 60 } 61 public void DownloadPharmaciesExcel() 51 62 { 52 63 try … … 56 67 string pathToSave2 = Directory.GetCurrentDirectory() + @"\ExcellDocs\2.xlsx"; 57 68 var client = new WebClient(); 58 string url1 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/ ce446f5c-e541-46f6-9e8c-67568059cbc6/download/registar-na-apteki-vnatre-vo-mreza-na-fzo-12.08.2020.xlsx";59 string url2 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/a16379b4-ec81-4de7-994d-0ee503d71b55/download/registar-na-apteki-nadvor-od-mreza-na-fzo-12.08.2020.xlsx";69 string url1 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/4806b744-f6f6-42d0-b0f0-d82e66d3b177/download/-.xlsxs"; 70 /*string url2 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/a16379b4-ec81-4de7-994d-0ee503d71b55/download/registar-na-apteki-nadvor-od-mreza-na-fzo-12.08.2020.xlsx";*/ 60 71 Uri uri1 = new Uri(url1); 61 Uri uri2 = new Uri(url2);62 client.DownloadFile (uri1, @pathToSave1);63 client.DownloadFile(uri2, @pathToSave2);64 65 66 bool Success = awaitReadPharmaciesFromExcel(pathToSave1);72 //Uri uri2 = new Uri(url2); 73 client.DownloadFileAsync(uri1, pathToSave1); 74 //client.DownloadFile(uri2, @pathToSave2); 75 76 77 bool Success = ReadPharmaciesFromExcel(pathToSave1); 67 78 _logger.LogInformation(Success.ToString() + "1"); 68 Success = await ReadPharmaciesFromExcel(pathToSave2);69 _logger.LogInformation(Success.ToString() + "2"); 70 } 71 catch (Exception e) 72 { 73 _logger.LogInformation(e.Message); 74 throw new Exception("Cannot process Medicines from Excel.");79 /* Success = await ReadPharmaciesFromExcel(pathToSave2); 80 _logger.LogInformation(Success.ToString() + "2");*/ 81 } 82 catch (Exception e) 83 { 84 _logger.LogInformation(e.Message); 85 throw new Exception("Cannot process Pharmacies from Excel."); 75 86 } 76 87 } -
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.