source: FarmatikoServices/Services/ProcessJSONService.cs@ 6e6f04b

Last change on this file since 6e6f04b was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Fix all bugs

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