source: FarmatikoServices/Services/ProcessJSONService.cs

Last change on this file was f554983, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 22 months ago

Add cron job for updating data, with Quartz.NET

  • Property mode set to 100644
File size: 8.8 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;
15using FarmatikoData.FarmatikoRepoInterfaces;
16
17namespace FarmatikoServices.Services
18{
19 public class ProcessJSONService : IProcessJSONService
20 {
21
22 private readonly ILogger _logger;
23 private readonly IUpdateDataRepo _repo;
24 private readonly IService _service;
25 public ProcessJSONService(ILogger logger, IUpdateDataRepo repo, IService service)
26 {
27 _logger = logger;
28 _repo = repo;
29 _service = service;
30 }
31 //Excel reader
32 private bool ReadPharmaciesFromExcel(string Path)
33 {
34 string path = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
35
36 FileInfo fileInfo = new FileInfo(path);
37 if (fileInfo != null)
38 {
39 using (var package = new ExcelPackage(fileInfo))
40 {
41 var Sheet = package.Workbook.Worksheets.First();
42 //var table = Sheet.Tables.First();
43 int rowCount = Sheet.Dimension.End.Row;
44 for (int i = 2; i < rowCount; ++i)
45 {
46 //Console.WriteLine();
47 Pharmacy pharmacy = new Pharmacy()
48 {
49 Name = Sheet.Cells[i, 2].Value.ToString(),
50 Address = Sheet.Cells[i, 3].Value.ToString(),
51 Location = Sheet.Cells[i, 4].Value.ToString(),
52 WorkAllTime = false
53 };
54 _service.AddPharmacy(pharmacy);
55 }
56 return true;
57 }
58
59 }
60
61 return false;
62
63 }
64 public void DownloadPharmaciesExcel()
65 {
66 try
67 {
68 string pathToSave1 = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
69
70 string pathToSave2 = Directory.GetCurrentDirectory() + @"\ExcellDocs\2.xlsx";
71 var client = new WebClient();
72 string url1 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/4806b744-f6f6-42d0-b0f0-d82e66d3b177/download/-.xlsxs";
73 /*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";*/
74 Uri uri1 = new Uri(url1);
75 //Uri uri2 = new Uri(url2);
76 client.DownloadFileAsync(uri1, pathToSave1);
77 //client.DownloadFile(uri2, @pathToSave2);
78
79
80 bool Success = ReadPharmaciesFromExcel(pathToSave1);
81 _logger.LogInformation(Success.ToString() + "1");
82
83
84 }
85 catch (Exception e)
86 {
87 _logger.LogInformation(e.Message);
88 throw new Exception("Cannot process Pharmacies from Excel.");
89 }
90 }
91
92 //Healthfacilities
93 public void GetProcessedHealthFacilitiesFromJSON()
94 {
95 try
96 {
97 var client = new WebClient();
98 var json = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/505db453-4de2-4761-8a81-2800f7820b06?format=json");
99
100 var jsonResponse = JObject.Parse(json);
101 var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
102
103 foreach (var rec in records)
104 {
105 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
106 var Name = obj[2];
107 var Municipality = obj[6];
108 var Address = obj[9];
109 var Email = obj[10];
110 var Phone = obj[11];
111 var Type = obj[5];
112 HealthFacility healthFacility = new HealthFacility();
113 //Name, Municipality, Address, Type, Email, Phone
114 healthFacility.Name = Convert.ToString(Name);
115 healthFacility.Municipality = Convert.ToString(Municipality);
116 healthFacility.Address = Convert.ToString(Address);
117 healthFacility.Type = Convert.ToString(Type);
118 healthFacility.Email = Convert.ToString(Email);
119 healthFacility.Phone = Convert.ToString(Phone);
120 _service.AddFacility(healthFacility);
121
122 }
123
124 }
125 catch (Exception e)
126 {
127 _logger.LogInformation(e.Message);
128 throw new Exception("Cannot process health facilities from JSON." + e.Message);
129 }
130 }
131
132 //Healthcare workers
133 public void GetProcessedHealthcareWorkersFromJSON()
134 {
135 try
136 {
137 var client = new WebClient();
138 var jsonW = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/5b661887-685b-4189-b6bb-9b52eb8ace16?format=json");
139
140 var jsonResponseW = JObject.Parse(jsonW);
141 var recordsW = JArray.Parse(jsonResponseW.GetValue("records").ToString());
142
143 foreach (var rec in recordsW)
144 {
145 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
146 var Name = Convert.ToString(obj[4]);
147 var Branch = Convert.ToString(obj[2]);
148 var FacilityName = Convert.ToString(obj[1]);
149 var Title = Convert.ToString(obj[3]);
150
151 HealthFacility facility = _repo.GetFacilityJSON(Convert.ToString(FacilityName));
152
153 if (facility != null && facility != default)
154 {
155 HealthFacility Facility = new HealthFacility(
156 facility.Name,
157 facility.Municipality,
158 facility.Address,
159 facility.Type,
160 facility.Email,
161 facility.Phone
162 );
163 HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
164 _service.AddWorker(healthcareWorker);
165 }
166 else
167 {
168 HealthFacility Facility = new HealthFacility(
169 Convert.ToString(FacilityName),
170 "",
171 "",
172 Convert.ToString(Branch),
173 "",
174 ""
175 );
176 HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
177 _service.AddWorker(healthcareWorker);
178 }
179
180
181 }
182
183 }
184 catch (Exception e)
185 {
186 _logger.LogInformation(e.Message);
187 }
188 }
189 //Medicines
190 public void GetProcessedMedicinesFromJSON()
191 {
192 try
193 {
194 var client = new WebClient();
195 var jsonM = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/ecff2aef-9c8e-4efd-a557-96df4fff9adb?format=json");
196
197 var jsonResponseM = JObject.Parse(jsonM);
198 var recordsM = JArray.Parse(jsonResponseM.GetValue("records").ToString());
199
200 foreach (var rec in recordsM)
201 {
202 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
203 var Name = obj[1];
204 var Strength = obj[7];
205 var Form = obj[6];
206 var WayOfIssuing = obj[9];
207 var Manufacturer = obj[11];
208 var Price = float.Parse(Convert.ToString(obj[17]));
209 var Packaging = obj[8];
210 string price = Convert.ToString(Price);
211 Medicine medicine = new Medicine(Convert.ToString(Name), Convert.ToString(Strength), Convert.ToString(Form), Convert.ToString(WayOfIssuing), Convert.ToString(Manufacturer), Price, Convert.ToString(Packaging));
212
213 _service.AddMedicines(medicine);
214 }
215 }
216 catch (Exception e)
217 {
218 _logger.LogInformation(e.Message);
219 throw new Exception("medicine");
220 }
221 }
222 }
223}
Note: See TracBrowser for help on using the repository browser.