source: FarmatikoServices/Services/ProcessJSONService.cs@ 1454207

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

Change structure, Add repo, services & controllers

  • Property mode set to 100644
File size: 8.0 KB
Line 
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;
12using System.Threading.Tasks;
13using OfficeOpenXml;
14using System.IO;
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
29 public async Task<Medicine> ReadPharmaciesFromExcel()
30 {
31 var client = new WebClient();
32 string Path = client.DownloadString(@"C:\Users\dslez\Desktop\apteki-fzo.xlsx");
33 using (var package = new ExcelPackage(new FileInfo(Path)))
34 {
35 var Sheet = package.Workbook.Worksheets[1];
36 for (int i = 2; i < Sheet.Dimension.End.Row; ++i)
37 {
38 Pharmacy pharmacy = new Pharmacy()
39 {
40 Name = Sheet.Cells[i, 2].Value.ToString(),
41 Address = Sheet.Cells[i, 3].Value.ToString(),
42 Location = Sheet.Cells[i, 4].Value.ToString(),
43 WorkAllTime = false
44 };
45 await _service.AddPharmacy(pharmacy);
46 }
47 }
48 return null;
49 }
50
51
52 public async Task<HealthFacility> GetProcessedHealthFacilitiesFromJSON()
53 {
54 try
55 {
56 var client = new WebClient();
57 var json = client.DownloadString(@"C:\Users\dslez\Desktop\ustanovi.json");
58
59 var jsonResponse = JObject.Parse(json);
60 var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
61
62 foreach (var rec in records)
63 {
64 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
65 var Name = obj[2];
66 var Municipality = obj[6];
67 var Address = obj[9];
68 var Email = obj[10];
69 var Phone = obj[11];
70 var Type = obj[5];
71 HealthFacility healthFacility = new HealthFacility(Name, Municipality, Address, Type, Email, Phone);
72 await Task.Run(() => _service.AddFacility(healthFacility));
73
74 }
75
76 }
77 catch (Exception e)
78 {
79 _logger.LogInformation(e.Message);
80 throw new Exception("Cannot process health facilities from JSON.");
81 }
82 return null;
83 }
84
85 public async Task<Pandemic> GetProcessedPandemicsFromJSONApi()
86 {
87 try
88 {
89 var client = new RestClient("https://api.covid19api.com/summary");
90 var response = client.Execute(new RestRequest());
91 string original = response.Content;
92 var jsonResponsePandemic = JObject.Parse(original);
93 var global = JObject.Parse(jsonResponsePandemic.GetValue("Global").ToString());
94 var TotalConfirmed = long.Parse(global.GetValue("TotalConfirmed").ToString());
95 var TotalDeaths = long.Parse(global.GetValue("TotalDeaths").ToString());
96 var TotalRecovered = long.Parse(global.GetValue("TotalRecovered").ToString());
97
98 var mk = JArray.Parse(jsonResponsePandemic.GetValue("Countries").ToString());
99 dynamic objP = mk[100];
100 var TotalMk = Int32.Parse(objP.GetValue("TotalConfirmed").ToString());
101 var TotalDeathsMK = Int32.Parse(objP.GetValue("TotalDeaths").ToString());
102 var TotalRecoveredMK = Int32.Parse(objP.GetValue("TotalRecovered").ToString());
103 var NewMK = Int32.Parse(objP.GetValue("NewConfirmed").ToString());
104
105 var Name = "Coronavirus";
106 var ActiveMk = TotalMk - (TotalRecoveredMK + TotalDeathsMK);
107 var ActiveGlobal = TotalConfirmed - (TotalRecovered + TotalDeaths);
108
109 Pandemic pandemic = new Pandemic(Name, TotalMk, ActiveMk, TotalDeathsMK, NewMK, TotalConfirmed, TotalDeaths, ActiveGlobal);
110 await Task.Run(() => _service.AddPandemic(pandemic));
111 }
112 catch (Exception e)
113 {
114 _logger.LogInformation(e.Message);
115 }
116 return null;
117 }
118
119 public async Task<HealthcareWorker> GetProcessedHealthcareWorkersFromJSON()
120 {
121 try
122 {
123 var client = new WebClient();
124 var jsonW = client.DownloadString(@"C:\Users\dslez\Desktop\rabotnici.json");
125
126 var jsonResponseW = JObject.Parse(jsonW);
127 var recordsW = JArray.Parse(jsonResponseW.GetValue("records").ToString());
128
129 foreach (var rec in recordsW)
130 {
131 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
132 var Name = obj[4];
133 var Branch = obj[2];
134 var FacilityName = obj[1];
135 var Title = obj[3];
136 HealthFacility facility = await _service.GetFacilityJSON(FacilityName);
137 HealthFacility Facility = new HealthFacility(facility.Name, facility.Municipality, facility.Address,
138 facility.Type, facility.Email, facility.Phone);
139 HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
140 /*Facility.Name = obj[1];
141 Facility.Municipality = "WorkerFacilityMunicipality";
142 Facility.Address = "WorkerFacilityAddress";*/
143 /*healthcareWorker.Name = Name;
144 healthcareWorker.Branch = Branch;
145 healthcareWorker.Facility = Facility;
146 healthcareWorker.Title = Title;*/
147 await Task.Run(() => _service.AddWorker(healthcareWorker));
148 }
149 }
150 catch (Exception e)
151 {
152 _logger.LogInformation(e.Message);
153 }
154 return null;
155 }
156
157 public async Task<Medicine> GetProcessedMedicinesFromJSON()
158 {
159 try
160 {
161 var client = new WebClient();
162 var jsonM = client.DownloadString(@"C:\Users\dslez\Desktop\lekovi.json");
163
164 var jsonResponseM = JObject.Parse(jsonM);
165 var recordsM = JArray.Parse(jsonResponseM.GetValue("records").ToString());
166
167 foreach (var rec in recordsM)
168 {
169 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
170 var Name = obj[1];
171 var Strength = obj[7];
172 var Form = obj[6];
173 var WayOfIssuing = obj[9];
174 var Manufacturer = obj[11];
175 var Price = float.Parse(obj[17]);
176 var Packaging = obj[8];
177 Medicine medicine = new Medicine(Name, Strength, Form, WayOfIssuing, Manufacturer, Price, Packaging);
178 /*medicine.Name = Name;
179 medicine.Strength = Strength;
180 medicine.Form = Form;
181 medicine.WayOfIssuing = WayOfIssuing;
182 medicine.Manufacturer = Manufacturer;
183 medicine.Price = Price;
184 medicine.Packaging = Packaging;*/
185 await Task.Run(() => _service.AddMedicines(medicine));
186 }
187 }
188 catch (Exception e)
189 {
190 _logger.LogInformation(e.Message);
191 }
192 return null;
193 }
194 }
195}
Note: See TracBrowser for help on using the repository browser.