Update weather section on get configuration detail
This commit is contained in:
parent
b87256e29d
commit
68b45333da
@ -129,15 +129,20 @@ namespace ManagerService.Controllers
|
||||
|
||||
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id);
|
||||
|
||||
if (configuration.WeatherCity != null && configuration.WeatherCity.Length >= 2 &&
|
||||
(configuration.WeatherUpdatedDate == null || configuration.WeatherUpdatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
|
||||
var weatherSections = _sectionService.GetAllWeatherSectionsFromConfiguration(id);
|
||||
|
||||
foreach (var weatherSection in weatherSections)
|
||||
{
|
||||
WeatherDTO weatherDTO = JsonConvert.DeserializeObject<WeatherDTO>(weatherSection.Data);
|
||||
if (weatherDTO.city != null && weatherDTO.city.Length >= 2 &&
|
||||
(weatherDTO.updatedDate == null || weatherDTO.updatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
|
||||
{
|
||||
// Call Openweather api with token from appSettings and update result with json
|
||||
var apiKey = _configuration.GetSection("OpenWeatherApiKey").Get<string>();
|
||||
|
||||
if (apiKey != null && apiKey.Length > 0)
|
||||
{
|
||||
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={configuration.WeatherCity}&limit=1&appid={apiKey}";
|
||||
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={weatherDTO.city}&limit=1&appid={apiKey}";
|
||||
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
@ -161,10 +166,11 @@ namespace ManagerService.Controllers
|
||||
callResponse.EnsureSuccessStatusCode();
|
||||
string callResponseBody = await callResponse.Content.ReadAsStringAsync();
|
||||
|
||||
configuration.WeatherUpdatedDate = DateTimeOffset.Now;
|
||||
configuration.WeatherResult = callResponseBody;
|
||||
weatherDTO.updatedDate = DateTimeOffset.Now;
|
||||
weatherDTO.result = callResponseBody;
|
||||
weatherSection.Data = JsonConvert.SerializeObject(weatherDTO);
|
||||
|
||||
_configurationService.Update(configuration.Id, configuration);
|
||||
_sectionService.Update(weatherSection.Id, weatherSection);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -179,6 +185,8 @@ namespace ManagerService.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new OkObjectResult(configuration.ToDTO(sectionIds));
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
|
||||
@ -39,6 +39,11 @@ namespace Manager.Services
|
||||
return _Sections.Find(s => !s.IsSubSection && s.ConfigurationId == configurationId).ToList().Select(s => s.Id).ToList();
|
||||
}
|
||||
|
||||
public List<Section> GetAllWeatherSectionsFromConfiguration(string configurationId)
|
||||
{
|
||||
return _Sections.Find(s => s.ConfigurationId == configurationId && s.Type == SectionType.Weather).ToList();
|
||||
}
|
||||
|
||||
public List<Section> GetAllSubSection(string parentId)
|
||||
{
|
||||
return _Sections.Find(s => s.IsSubSection && s.ParentId == parentId).ToList();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user