source: FarmatikoServices/Services/ProcessJSONService.cs@ a55ef91

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

Update & add service

  • Property mode set to 100644
File size: 2.8 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 readonly ILogger _logger;
19 public ProcessJSONService(IHealthFacilityRepository healthFacilityRepository)
20 {
21 //_logger = logger;
22 _healthFacilityRepository = healthFacilityRepository;
23 }
24
25 public List<HealthFacilities> GetProcessedHealthFacilitiesFromJSON()
26 {
27 try
28 {
29 var client = new WebClient();
30 var json = client.DownloadString(@"C:\Users\dslez\Desktop\ustanovi.json");
31
32 var jsonResponse = JObject.Parse(json);
33 var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
34
35
36 foreach(var rec in records)
37 {
38 dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
39 var Name = obj[2];
40 var Municipality = obj[6];
41 var Address = obj[9];
42 var Email = obj[10];
43 var Phone = obj[11];
44 var Type = obj[5];
45 HealthFacilities healthFacility = new HealthFacilities();
46 healthFacility.Name = Name;
47 healthFacility.Municipality = Municipality;
48 healthFacility.Address = Address;
49 healthFacility.Email = Email;
50 healthFacility.Phone = Phone;
51 healthFacility.Type = Type;
52 var obje = healthFacility;
53 _healthFacilityRepository.Add(healthFacility);
54
55 }
56
57
58
59 /*var client1 = new RestClient("https://api.covid19api.com/summary");
60 var response = client1.Execute(new RestRequest());
61 string original = response.Content;
62 var jsonResponse = JObject.Parse(original);
63 var global = JObject.Parse(jsonResponse.GetValue("Global").ToString());
64 var total = Int32.Parse(global.GetValue("TotalConfirmed").ToString());
65 Pandemic pandemic = new Pandemic();
66 pandemic.TotalGlobal = total;*/
67
68 }
69 catch (Exception e)
70 {
71 //_logger.LogError(e.Message);
72 Console.WriteLine(e.Message);
73 throw e;
74 return null;
75 }
76 return null;
77 }
78
79 }
80}
Note: See TracBrowser for help on using the repository browser.