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

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

Change methods & add error controller

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