[a55ef91] | 1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
| 2 | using FarmatikoData.Models;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using Newtonsoft.Json;
|
---|
| 5 | using Newtonsoft.Json.Linq;
|
---|
| 6 | using System;
|
---|
| 7 | using Microsoft.Extensions.Logging;
|
---|
| 8 | using System.Net;
|
---|
| 9 | using System.Linq;
|
---|
| 10 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
| 11 | using RestSharp;
|
---|
[c406ae5] | 12 | using System.Threading.Tasks;
|
---|
[a55ef91] | 13 |
|
---|
| 14 | namespace FarmatikoServices.Services
|
---|
| 15 | {
|
---|
| 16 | public class ProcessJSONService : IProcessJSONService
|
---|
| 17 | {
|
---|
[c406ae5] | 18 | private IHealthFacilityRepository _healthFacilityRepository;
|
---|
[c73269d] | 19 | private IPandemicRepository _pandemicRepository;
|
---|
[c406ae5] | 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)
|
---|
[a55ef91] | 25 | {
|
---|
[c406ae5] | 26 | _logger = logger;
|
---|
| 27 | _healthFacilityRepository = healthFacilityRepository;
|
---|
[c73269d] | 28 | _pandemicRepository = pandemicRepository;
|
---|
[c406ae5] | 29 | _healthcareWorkerRepository = healthcareWorkerRepository;
|
---|
| 30 | _medicineRepository = medicineRepository;
|
---|
[a55ef91] | 31 | }
|
---|
[c406ae5] | 32 |
|
---|
| 33 | public async Task<HashSet<HealthFacilities>> GetProcessedHealthFacilitiesFromJSON()
|
---|
[a55ef91] | 34 | {
|
---|
| 35 | try
|
---|
| 36 | {
|
---|
[c406ae5] | 37 | HashSet<HealthFacilities> hashSet = new HashSet<HealthFacilities>();
|
---|
| 38 | var client = new WebClient();
|
---|
| 39 | var json = client.DownloadString(@"C:\Users\dslez\Desktop\ustanovi.json");
|
---|
[a55ef91] | 40 |
|
---|
| 41 | var jsonResponse = JObject.Parse(json);
|
---|
| 42 | var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
|
---|
| 43 |
|
---|
[c406ae5] | 44 | foreach (var rec in records)
|
---|
[a55ef91] | 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];
|
---|
[c406ae5] | 53 | HealthFacilities healthFacility = new HealthFacilities(Name, Municipality, Address, Type, Email, Phone);
|
---|
| 54 | /*healthFacility.Name = Name;
|
---|
[a55ef91] | 55 | healthFacility.Municipality = Municipality;
|
---|
| 56 | healthFacility.Address = Address;
|
---|
| 57 | healthFacility.Email = Email;
|
---|
| 58 | healthFacility.Phone = Phone;
|
---|
[c406ae5] | 59 | healthFacility.Type = Type;*/
|
---|
| 60 | //hashSet.Add(healthFacility);
|
---|
| 61 | await Task.Run(() => _healthFacilityRepository.Add(healthFacility));
|
---|
[a55ef91] | 62 |
|
---|
[c406ae5] | 63 | }
|
---|
[a55ef91] | 64 |
|
---|
[c406ae5] | 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 | }
|
---|
[a55ef91] | 73 |
|
---|
[c406ae5] | 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());
|
---|
[a55ef91] | 80 | string original = response.Content;
|
---|
[c73269d] | 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 |
|
---|
[c406ae5] | 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;
|
---|
[c73269d] | 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;
|
---|
[c406ae5] | 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 | }
|
---|
[c73269d] | 115 |
|
---|
[c406ae5] | 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");
|
---|
[c73269d] | 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];
|
---|
[c406ae5] | 131 | var FacilityName = obj[1];
|
---|
[c73269d] | 132 | var Title = obj[3];
|
---|
[c406ae5] | 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;
|
---|
[c73269d] | 141 | healthcareWorker.Branch = Branch;
|
---|
[c406ae5] | 142 | healthcareWorker.Facility = Facility;
|
---|
| 143 | healthcareWorker.Title = Title;*/
|
---|
| 144 | await Task.Run(() => _healthcareWorkerRepository.Add(healthcareWorker));
|
---|
[c73269d] | 145 | }
|
---|
[c406ae5] | 146 | }
|
---|
| 147 | catch (Exception e)
|
---|
| 148 | {
|
---|
| 149 | _logger.LogInformation(e.Message);
|
---|
| 150 | }
|
---|
| 151 | return null;
|
---|
| 152 | }
|
---|
[c73269d] | 153 |
|
---|
[c406ae5] | 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");
|
---|
[c73269d] | 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];
|
---|
[c406ae5] | 174 | Medicine medicine = new Medicine(Name, Strength, Form, WayOfIssuing, Manufacturer, Price, Packaging);
|
---|
| 175 | /*medicine.Name = Name;
|
---|
[c73269d] | 176 | medicine.Strength = Strength;
|
---|
| 177 | medicine.Form = Form;
|
---|
| 178 | medicine.WayOfIssuing = WayOfIssuing;
|
---|
| 179 | medicine.Manufacturer = Manufacturer;
|
---|
| 180 | medicine.Price = Price;
|
---|
[c406ae5] | 181 | medicine.Packaging = Packaging;*/
|
---|
| 182 | await Task.Run(() => _medicineRepository.Add(medicine));
|
---|
| 183 | }
|
---|
[a55ef91] | 184 | }
|
---|
| 185 | catch (Exception e)
|
---|
| 186 | {
|
---|
[c406ae5] | 187 | _logger.LogInformation(e.Message);
|
---|
[a55ef91] | 188 | }
|
---|
| 189 | return null;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|