source: FarmatikoServices/Services/ProcessJSONService.cs@ c73269d

Last change on this file since c73269d was c73269d, checked in by Mile Jankuloski <mile.jankuloski@…>, 2 years ago

JSON processing added

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