source: FarmatikoServices/Services/ProcessJSONService.cs@ e0cdea2

Last change on this file since e0cdea2 was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 18 months ago

Fix all bugs

  • Property mode set to 100644
File size: 10.9 KB
Line 
1using FarmatikoData.Models;
2using Newtonsoft.Json;
3using Newtonsoft.Json.Linq;
4using System;
5using Microsoft.Extensions.Logging;
6using System.Net;
7using System.Linq;
8using FarmatikoServices.FarmatikoServiceInterfaces;
9using RestSharp;
10using System.Threading.Tasks;
11using OfficeOpenXml;
12using System.IO;
13using GemBox.Spreadsheet;
14using System.Text;
15
16namespace FarmatikoServices.Services
17{
18 public class ProcessJSONService : IProcessJSONService
19 {
20
21 private readonly ILogger _logger;
22 private readonly IService _service;
23 public ProcessJSONService(ILogger logger, IService service)
24 {
25 _logger = logger;
26 _service = service;
27 }
28 //Excel reader
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)
42 {
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 }
53 return true;
54 }
55
56 }
57
58 return false;
59
60 }
61 public void DownloadPharmaciesExcel()
62 {
63 try
64 {
65 string pathToSave1 = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
66
67 string pathToSave2 = Directory.GetCurrentDirectory() + @"\ExcellDocs\2.xlsx";
68 var client = new WebClient();
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";*/
71 Uri uri1 = new Uri(url1);
72 //Uri uri2 = new Uri(url2);
73 client.DownloadFileAsync(uri1, pathToSave1);
74 //client.DownloadFile(uri2, @pathToSave2);
75
76
77 bool Success = ReadPharmaciesFromExcel(pathToSave1);
78 _logger.LogInformation(Success.ToString() + "1");
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.");
86 }
87 }
88
89 //Healthfacilities
90 public async void GetProcessedHealthFacilitiesFromJSON()
91 {
92 try
93 {
94 var client = new WebClient();
95 var json = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/505db453-4de2-4761-8a81-2800f7820b06?format=json");
96
97 var jsonResponse = JObject.Parse(json);
98 var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
99
100 foreach (var rec in records)
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];
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);
118
119 }
120
121 }
122 catch (Exception e)
123 {
124 _logger.LogInformation(e.Message);
125 throw new Exception("Cannot process health facilities from JSON." + e.Message);
126 }
127 }
128 //Pandemics
129 public async void GetProcessedPandemicsFromJSONApi()
130 {
131 try
132 {
133 var Date = DateTime.UtcNow.ToString("yyyy-MM-dd");
134 var client = new RestClient($"https://api.covid19tracking.narrativa.com/api/{Date}/country/north_macedonia");
135 var response = client.Execute(new RestRequest());
136 string original = response.Content;
137 var jsonResponsePandemic = JObject.Parse(original);
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());
153
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);
159 await _service.AddPandemic(pandemic);
160 }
161 catch (Exception e)
162 {
163 _logger.LogInformation(e.Message);
164 }
165 }
166 //Healthcare workers
167 public async void GetProcessedHealthcareWorkersFromJSON()
168 {
169 try
170 {
171 var client = new WebClient();
172 var jsonW = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/5b661887-685b-4189-b6bb-9b52eb8ace16?format=json");
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());
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
187 if (facility != null && facility != default)
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
215 }
216 }
217 catch (Exception e)
218 {
219 _logger.LogInformation(e.Message);
220 }
221 }
222 //Medicines
223 public async void GetProcessedMedicinesFromJSON()
224 {
225 try
226 {
227 var client = new WebClient();
228 var jsonM = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/ecff2aef-9c8e-4efd-a557-96df4fff9adb?format=json");
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];
241 var Price = float.Parse(Convert.ToString(obj[17]));
242 var Packaging = obj[8];
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);
247 }
248 }
249 catch (Exception e)
250 {
251 _logger.LogInformation(e.Message);
252 throw new Exception("medicine");
253 }
254 }
255 }
256}
Note: See TracBrowser for help on using the repository browser.