source: FarmatikoServices/Services/ProcessJSONService.cs@ c406ae5

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

Update Models, Repos, Services and Controllers

  • Property mode set to 100644
File size: 8.6 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;
13
14namespace FarmatikoServices.Services
15{
16 public class ProcessJSONService : IProcessJSONService
17 {
18 private IHealthFacilityRepository _healthFacilityRepository;
19 private IPandemicRepository _pandemicRepository;
20 private IHealthcareWorkerRepository _healthcareWorkerRepository;
21 private IMedicineRepository _medicineRepository;
22 private readonly ILogger _logger;
23 public ProcessJSONService(IHealthFacilityRepository healthFacilityRepository, IPandemicRepository pandemicRepository,
24 IHealthcareWorkerRepository healthcareWorkerRepository, IMedicineRepository medicineRepository, ILogger logger)
25 {
26 _logger = logger;
27 _healthFacilityRepository = healthFacilityRepository;
28 _pandemicRepository = pandemicRepository;
29 _healthcareWorkerRepository = healthcareWorkerRepository;
30 _medicineRepository = medicineRepository;
31 }
32
33 public async Task<HashSet<HealthFacilities>> GetProcessedHealthFacilitiesFromJSON()
34 {
35 try
36 {
37 HashSet<HealthFacilities> hashSet = new HashSet<HealthFacilities>();
38 var client = new WebClient();
39 var json = client.DownloadString(@"C:\Users\dslez\Desktop\ustanovi.json");
40
41 var jsonResponse = JObject.Parse(json);
42 var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
43
44 foreach (var rec in records)
45 {
46 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
47 var Name = obj[2];
48 var Municipality = obj[6];
49 var Address = obj[9];
50 var Email = obj[10];
51 var Phone = obj[11];
52 var Type = obj[5];
53 HealthFacilities healthFacility = new HealthFacilities(Name, Municipality, Address, Type, Email, Phone);
54 /*healthFacility.Name = Name;
55 healthFacility.Municipality = Municipality;
56 healthFacility.Address = Address;
57 healthFacility.Email = Email;
58 healthFacility.Phone = Phone;
59 healthFacility.Type = Type;*/
60 //hashSet.Add(healthFacility);
61 await Task.Run(() => _healthFacilityRepository.Add(healthFacility));
62
63 }
64
65 }
66 catch (Exception e)
67 {
68 _logger.LogInformation(e.Message);
69 throw new Exception("Cannot process health facilities from JSON.");
70 }
71 return null;
72 }
73
74 public async Task<HashSet<Pandemic>> GetProcessedPandemicsFromJSONApi()
75 {
76 try
77 {
78 var client = new RestClient("https://api.covid19api.com/summary");
79 var response = client.Execute(new RestRequest());
80 string original = response.Content;
81 var jsonResponsePandemic = JObject.Parse(original);
82 var global = JObject.Parse(jsonResponsePandemic.GetValue("Global").ToString());
83 var TotalConfirmed = long.Parse(global.GetValue("TotalConfirmed").ToString());
84 var TotalDeaths = long.Parse(global.GetValue("TotalDeaths").ToString());
85 var TotalRecovered = long.Parse(global.GetValue("TotalRecovered").ToString());
86
87 var mk = JArray.Parse(jsonResponsePandemic.GetValue("Countries").ToString());
88 dynamic objP = mk[100];
89 var TotalMk = Int32.Parse(objP.GetValue("TotalConfirmed").ToString());
90 var TotalDeathsMK = Int32.Parse(objP.GetValue("TotalDeaths").ToString());
91 var TotalRecoveredMK = Int32.Parse(objP.GetValue("TotalRecovered").ToString());
92 var NewMK = Int32.Parse(objP.GetValue("NewConfirmed").ToString());
93
94 var Name = "Coronavirus";
95 var ActiveMk = TotalMk - (TotalRecoveredMK + TotalDeathsMK);
96 var ActiveGlobal = TotalConfirmed - (TotalRecovered + TotalDeaths);
97
98 Pandemic pandemic = new Pandemic(Name, TotalMk, ActiveMk, TotalDeathsMK, NewMK, TotalConfirmed, TotalDeaths, ActiveGlobal);
99 /*pandemic.TotalGlobal = TotalConfirmed;
100 pandemic.ActiveGlobal = TotalConfirmed - (TotalRecovered + TotalDeaths);
101 pandemic.DeathsGlobal = TotalDeaths;
102 pandemic.TotalMK = TotalMk;
103 pandemic.ActiveMK = TotalMk - (TotalRecoveredMK + TotalDeathsMK);
104 pandemic.DeathsMK = TotalDeathsMK;
105 pandemic.NewMK = NewMK;
106 pandemic.Name = "Coronavirus";*/
107 await Task.Run(() => _pandemicRepository.Add(pandemic));
108 }
109 catch (Exception e)
110 {
111 _logger.LogInformation(e.Message);
112 }
113 return null;
114 }
115
116 public async Task<HashSet<HealthcareWorkers>> GetProcessedHealthcareWorkersFromJSON()
117 {
118 try
119 {
120 var client = new WebClient();
121 var jsonW = client.DownloadString(@"C:\Users\dslez\Desktop\rabotnici.json");
122
123 var jsonResponseW = JObject.Parse(jsonW);
124 var recordsW = JArray.Parse(jsonResponseW.GetValue("records").ToString());
125
126 foreach (var rec in recordsW)
127 {
128 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
129 var Name = obj[4];
130 var Branch = obj[2];
131 var FacilityName = obj[1];
132 var Title = obj[3];
133 HealthFacilities facility = _healthFacilityRepository.GetByName(FacilityName);
134 HealthFacilities Facility = new HealthFacilities(facility.Name, facility.Municipality, facility.Address,
135 facility.Type, facility.Email, facility.Phone);
136 HealthcareWorkers healthcareWorker = new HealthcareWorkers(Name, Branch, Facility, Title);
137 /*Facility.Name = obj[1];
138 Facility.Municipality = "WorkerFacilityMunicipality";
139 Facility.Address = "WorkerFacilityAddress";*/
140 /*healthcareWorker.Name = Name;
141 healthcareWorker.Branch = Branch;
142 healthcareWorker.Facility = Facility;
143 healthcareWorker.Title = Title;*/
144 await Task.Run(() => _healthcareWorkerRepository.Add(healthcareWorker));
145 }
146 }
147 catch (Exception e)
148 {
149 _logger.LogInformation(e.Message);
150 }
151 return null;
152 }
153
154 public async Task<HashSet<Medicine>> GetProcessedMedicinesFromJSON()
155 {
156 try
157 {
158 var client = new WebClient();
159 var jsonM = client.DownloadString(@"C:\Users\dslez\Desktop\lekovi.json");
160
161 var jsonResponseM = JObject.Parse(jsonM);
162 var recordsM = JArray.Parse(jsonResponseM.GetValue("records").ToString());
163
164 foreach (var rec in recordsM)
165 {
166 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
167 var Name = obj[1];
168 var Strength = obj[7];
169 var Form = obj[6];
170 var WayOfIssuing = obj[9];
171 var Manufacturer = obj[11];
172 var Price = float.Parse(obj[17]);
173 var Packaging = obj[8];
174 Medicine medicine = new Medicine(Name, Strength, Form, WayOfIssuing, Manufacturer, Price, Packaging);
175 /*medicine.Name = Name;
176 medicine.Strength = Strength;
177 medicine.Form = Form;
178 medicine.WayOfIssuing = WayOfIssuing;
179 medicine.Manufacturer = Manufacturer;
180 medicine.Price = Price;
181 medicine.Packaging = Packaging;*/
182 await Task.Run(() => _medicineRepository.Add(medicine));
183 }
184 }
185 catch (Exception e)
186 {
187 _logger.LogInformation(e.Message);
188 }
189 return null;
190 }
191 }
192}
Note: See TracBrowser for help on using the repository browser.