source: FarmatikoServices/Services/ProcessJSONService.cs@ d23bf72

Last change on this file since d23bf72 was d23bf72, checked in by DimitarSlezenkovski <dslezenkovski@…>, 2 years ago

Add SystemService, Auth, fix a lil bugs :)

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