[a55ef91] | 1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
| 2 | using FarmatikoData.Models;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using Newtonsoft.Json;
|
---|
| 5 | using Newtonsoft.Json.Linq;
|
---|
| 6 | using System;
|
---|
| 7 | using Microsoft.Extensions.Logging;
|
---|
| 8 | using System.Net;
|
---|
| 9 | using System.Linq;
|
---|
| 10 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
| 11 | using RestSharp;
|
---|
[c406ae5] | 12 | using System.Threading.Tasks;
|
---|
[1454207] | 13 | using OfficeOpenXml;
|
---|
| 14 | using System.IO;
|
---|
[d23bf72] | 15 | using Microsoft.VisualBasic;
|
---|
[a55ef91] | 16 |
|
---|
| 17 | namespace FarmatikoServices.Services
|
---|
| 18 | {
|
---|
| 19 | public class ProcessJSONService : IProcessJSONService
|
---|
| 20 | {
|
---|
[1454207] | 21 |
|
---|
[c406ae5] | 22 | private readonly ILogger _logger;
|
---|
[1454207] | 23 | private readonly IService _service;
|
---|
| 24 | public ProcessJSONService(ILogger logger, IService service)
|
---|
[a55ef91] | 25 | {
|
---|
[c406ae5] | 26 | _logger = logger;
|
---|
[1454207] | 27 | _service = service;
|
---|
| 28 | }
|
---|
[d23bf72] | 29 | //Excel reader
|
---|
| 30 | private async Task<bool> ReadPharmaciesFromExcel(string Path)
|
---|
[1454207] | 31 | {
|
---|
[d23bf72] | 32 | using (var package = new ExcelPackage(new FileInfo(Path)))
|
---|
[1454207] | 33 | {
|
---|
[d23bf72] | 34 | var Sheet = package.Workbook.Worksheets[1];
|
---|
| 35 | for (int i = 2; i < Sheet.Dimension.End.Row; ++i)
|
---|
[1454207] | 36 | {
|
---|
[d23bf72] | 37 | Pharmacy pharmacy = new Pharmacy()
|
---|
[1454207] | 38 | {
|
---|
[d23bf72] | 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);
|
---|
| 45 | return true;
|
---|
[1454207] | 46 | }
|
---|
[d23bf72] | 47 | }
|
---|
| 48 | return false;
|
---|
| 49 | }
|
---|
| 50 | public async void DownloadPharmaciesExcel()
|
---|
| 51 | {
|
---|
| 52 | try
|
---|
| 53 | {
|
---|
| 54 | string pathToSave1 = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
|
---|
[6f203af] | 55 |
|
---|
[d23bf72] | 56 | string pathToSave2 = Directory.GetCurrentDirectory() + @"\ExcellDocs\2.xlsx";
|
---|
| 57 | 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";
|
---|
| 60 | 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 = await ReadPharmaciesFromExcel(pathToSave1);
|
---|
| 67 | _logger.LogInformation(Success.ToString() + "1");
|
---|
| 68 | Success = await ReadPharmaciesFromExcel(pathToSave2);
|
---|
| 69 | _logger.LogInformation(Success.ToString() + "2");
|
---|
[6f203af] | 70 | }
|
---|
[d23bf72] | 71 | catch (Exception e)
|
---|
[6f203af] | 72 | {
|
---|
| 73 | _logger.LogInformation(e.Message);
|
---|
| 74 | throw new Exception("Cannot process Medicines from Excel.");
|
---|
[1454207] | 75 | }
|
---|
[a55ef91] | 76 | }
|
---|
[c406ae5] | 77 |
|
---|
[d23bf72] | 78 | //Healthfacilities
|
---|
| 79 | public async void GetProcessedHealthFacilitiesFromJSON()
|
---|
[a55ef91] | 80 | {
|
---|
| 81 | try
|
---|
| 82 | {
|
---|
[c406ae5] | 83 | var client = new WebClient();
|
---|
[d23bf72] | 84 | var json = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/505db453-4de2-4761-8a81-2800f7820b06?format=json");
|
---|
[a55ef91] | 85 |
|
---|
| 86 | var jsonResponse = JObject.Parse(json);
|
---|
| 87 | var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
|
---|
| 88 |
|
---|
[c406ae5] | 89 | foreach (var rec in records)
|
---|
[a55ef91] | 90 | {
|
---|
| 91 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
| 92 | var Name = obj[2];
|
---|
| 93 | var Municipality = obj[6];
|
---|
| 94 | var Address = obj[9];
|
---|
| 95 | var Email = obj[10];
|
---|
| 96 | var Phone = obj[11];
|
---|
| 97 | var Type = obj[5];
|
---|
[d23bf72] | 98 | HealthFacility healthFacility = new HealthFacility();
|
---|
| 99 | //Name, Municipality, Address, Type, Email, Phone
|
---|
| 100 | healthFacility.Name = Convert.ToString(Name);
|
---|
| 101 | healthFacility.Municipality = Convert.ToString(Municipality);
|
---|
| 102 | healthFacility.Address = Convert.ToString(Address);
|
---|
| 103 | healthFacility.Type = Convert.ToString(Type);
|
---|
| 104 | healthFacility.Email = Convert.ToString(Email);
|
---|
| 105 | healthFacility.Phone = Convert.ToString(Phone);
|
---|
| 106 | await _service.AddFacility(healthFacility);
|
---|
[a55ef91] | 107 |
|
---|
[c406ae5] | 108 | }
|
---|
[a55ef91] | 109 |
|
---|
[c406ae5] | 110 | }
|
---|
| 111 | catch (Exception e)
|
---|
| 112 | {
|
---|
| 113 | _logger.LogInformation(e.Message);
|
---|
[d23bf72] | 114 | throw new Exception("Cannot process health facilities from JSON." + e.Message);
|
---|
[c406ae5] | 115 | }
|
---|
| 116 | }
|
---|
[d23bf72] | 117 | //Pandemics
|
---|
| 118 | public async void GetProcessedPandemicsFromJSONApi()
|
---|
[c406ae5] | 119 | {
|
---|
| 120 | try
|
---|
| 121 | {
|
---|
[d23bf72] | 122 | var Date = DateTime.UtcNow.ToString("yyyy-MM-dd");
|
---|
| 123 | var client = new RestClient($"https://api.covid19tracking.narrativa.com/api/{Date}/country/north_macedonia");
|
---|
[c406ae5] | 124 | var response = client.Execute(new RestRequest());
|
---|
[a55ef91] | 125 | string original = response.Content;
|
---|
[c73269d] | 126 | var jsonResponsePandemic = JObject.Parse(original);
|
---|
[d23bf72] | 127 | var global = JObject.Parse(jsonResponsePandemic.GetValue("total").ToString());
|
---|
| 128 | var TotalConfirmed = long.Parse(global.GetValue("today_confirmed").ToString());
|
---|
| 129 | var TotalDeaths = long.Parse(global.GetValue("today_deaths").ToString());
|
---|
| 130 | var TotalRecovered = long.Parse(global.GetValue("today_new_recovered").ToString());
|
---|
| 131 |
|
---|
| 132 | var mk = JObject.Parse(jsonResponsePandemic.GetValue("dates").ToString());
|
---|
| 133 |
|
---|
| 134 | var date = JObject.Parse(mk.GetValue(Date).ToString());
|
---|
| 135 | var country = JObject.Parse(date.GetValue("countries").ToString());
|
---|
| 136 | var mkd = JObject.Parse(country.GetValue("North Macedonia").ToString());
|
---|
| 137 | dynamic objP = mkd;
|
---|
| 138 | var TotalMk = Int32.Parse(objP.GetValue("today_confirmed").ToString());
|
---|
| 139 | var TotalDeathsMK = Int32.Parse(objP.GetValue("today_deaths").ToString());
|
---|
| 140 | var TotalRecoveredMK = Int32.Parse(objP.GetValue("today_recovered").ToString());
|
---|
| 141 | var NewMK = Int32.Parse(objP.GetValue("today_new_confirmed").ToString());
|
---|
[c73269d] | 142 |
|
---|
[c406ae5] | 143 | var Name = "Coronavirus";
|
---|
| 144 | var ActiveMk = TotalMk - (TotalRecoveredMK + TotalDeathsMK);
|
---|
| 145 | var ActiveGlobal = TotalConfirmed - (TotalRecovered + TotalDeaths);
|
---|
| 146 |
|
---|
| 147 | Pandemic pandemic = new Pandemic(Name, TotalMk, ActiveMk, TotalDeathsMK, NewMK, TotalConfirmed, TotalDeaths, ActiveGlobal);
|
---|
[d23bf72] | 148 | await _service.AddPandemic(pandemic);
|
---|
[c406ae5] | 149 | }
|
---|
| 150 | catch (Exception e)
|
---|
| 151 | {
|
---|
| 152 | _logger.LogInformation(e.Message);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
[d23bf72] | 155 | //Healthcare workers
|
---|
| 156 | public async void GetProcessedHealthcareWorkersFromJSON()
|
---|
[c406ae5] | 157 | {
|
---|
| 158 | try
|
---|
| 159 | {
|
---|
| 160 | var client = new WebClient();
|
---|
[d23bf72] | 161 | var jsonW = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/5b661887-685b-4189-b6bb-9b52eb8ace16?format=json");
|
---|
[c73269d] | 162 |
|
---|
| 163 | var jsonResponseW = JObject.Parse(jsonW);
|
---|
| 164 | var recordsW = JArray.Parse(jsonResponseW.GetValue("records").ToString());
|
---|
| 165 |
|
---|
| 166 | foreach (var rec in recordsW)
|
---|
| 167 | {
|
---|
| 168 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
[d23bf72] | 169 | var Name = Convert.ToString(obj[4]);
|
---|
| 170 | var Branch = Convert.ToString(obj[2]);
|
---|
| 171 | var FacilityName = Convert.ToString(obj[1]);
|
---|
| 172 | var Title = Convert.ToString(obj[3]);
|
---|
| 173 |
|
---|
| 174 | HealthFacility facility = _service.GetFacilityJSON(Convert.ToString(FacilityName));
|
---|
| 175 |
|
---|
| 176 | if (facility != null)
|
---|
| 177 | {
|
---|
| 178 | HealthFacility Facility = new HealthFacility(
|
---|
| 179 | facility.Name,
|
---|
| 180 | facility.Municipality,
|
---|
| 181 | facility.Address,
|
---|
| 182 | facility.Type,
|
---|
| 183 | facility.Email,
|
---|
| 184 | facility.Phone
|
---|
| 185 | );
|
---|
| 186 | HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
|
---|
| 187 | await _service.AddWorker(healthcareWorker);
|
---|
| 188 | }
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
| 191 | HealthFacility Facility = new HealthFacility(
|
---|
| 192 | Convert.ToString(FacilityName),
|
---|
| 193 | "",
|
---|
| 194 | "",
|
---|
| 195 | Convert.ToString(Branch),
|
---|
| 196 | "",
|
---|
| 197 | ""
|
---|
| 198 | );
|
---|
| 199 | HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
|
---|
| 200 | await _service.AddWorker(healthcareWorker);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
[c73269d] | 204 | }
|
---|
[c406ae5] | 205 | }
|
---|
| 206 | catch (Exception e)
|
---|
| 207 | {
|
---|
| 208 | _logger.LogInformation(e.Message);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
[d23bf72] | 211 | //Medicines
|
---|
| 212 | public async void GetProcessedMedicinesFromJSON()
|
---|
[c406ae5] | 213 | {
|
---|
| 214 | try
|
---|
| 215 | {
|
---|
| 216 | var client = new WebClient();
|
---|
[d23bf72] | 217 | var jsonM = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/ecff2aef-9c8e-4efd-a557-96df4fff9adb?format=json");
|
---|
[c73269d] | 218 |
|
---|
| 219 | var jsonResponseM = JObject.Parse(jsonM);
|
---|
| 220 | var recordsM = JArray.Parse(jsonResponseM.GetValue("records").ToString());
|
---|
| 221 |
|
---|
| 222 | foreach (var rec in recordsM)
|
---|
| 223 | {
|
---|
| 224 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
| 225 | var Name = obj[1];
|
---|
| 226 | var Strength = obj[7];
|
---|
| 227 | var Form = obj[6];
|
---|
| 228 | var WayOfIssuing = obj[9];
|
---|
| 229 | var Manufacturer = obj[11];
|
---|
[d23bf72] | 230 | var Price = float.Parse(Convert.ToString(obj[17]));
|
---|
[c73269d] | 231 | var Packaging = obj[8];
|
---|
[d23bf72] | 232 | string price = Convert.ToString(Price);
|
---|
| 233 | Medicine medicine = new Medicine(Convert.ToString(Name), Convert.ToString(Strength), Convert.ToString(Form), Convert.ToString(WayOfIssuing), Convert.ToString(Manufacturer), Price, Convert.ToString(Packaging));
|
---|
| 234 |
|
---|
| 235 | await _service.AddMedicines(medicine);
|
---|
[c406ae5] | 236 | }
|
---|
[a55ef91] | 237 | }
|
---|
| 238 | catch (Exception e)
|
---|
| 239 | {
|
---|
[c406ae5] | 240 | _logger.LogInformation(e.Message);
|
---|
[d23bf72] | 241 | throw new Exception("medicine");
|
---|
[a55ef91] | 242 | }
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | }
|
---|