public async Task <ActionResult <DashboardDto> > GetDashboardData([FromQuery] LatLonParameters latLonParameters) { BeachConditionsDto beachConditions = null; try { beachConditions = await _weatherService.GetBeachConditions(); } catch (Exception e) { _logger.LogWarning("Beach Conditions could not be grabbed: " + e.Message); } var uvDto = await _weatherService.GetCurrentUVIndex(latLonParameters); var weatherData = await _weatherService.GetWeather(latLonParameters); var usersFromRepo = await _beachBuddyRepository.GetUsers(); var dashboardDto = new DashboardDto { BeachConditions = beachConditions, DashboardUvDto = _mapper.Map <DashboardUVDto>(uvDto), // Todo: this can be deleted // DashboardUvDto = new DashboardUVDto // { // uv = 8.3, // uv_time = "2020-06-29T13:32:07.067Z", // uv_max = 12.1, // uv_max_time = "2020-06-29T16:32:07.067Z", // safe_exposure_time = new SafeExposureTimeDto // { // st1 = 10, // st2 = 20, // st3 = 30, // st4 = 40, // st5 = 50, // st6 = 60 // } // }, WeatherInfo = weatherData, Users = _mapper.Map <IEnumerable <UserDto> >(usersFromRepo), }; return(Ok(dashboardDto)); }
public async Task <BeachConditionsDto> GetBeachConditions() { // Look for cached version if (_memoryCache.TryGetValue("GetBeachConditions", out BeachConditionsDto cachedBeachConditionsDto)) { // We found it in cache, use this. return(cachedBeachConditionsDto); } const string requestUri = "https://api.visitbeaches.org/graphql"; var request = new HttpRequestMessage(HttpMethod.Post, requestUri); const string requestBody = "{\n\t\"query\": \"query GetBeach($id: ID!) {\\n beach(id: $id) {\\n ...WithLastThreeReports\\n }\\n}\\n\\nfragment WithLastThreeReports on Beach {\\n lastThreeDaysOfReports {\\n ...BeachReport\\n }\\n}\\n\\nfragment BeachReport on Report {\\n id\\n createdAt\\n latitude\\n longitude\\n beachReport {\\n parameterCategory {\\n ...ParameterCategory\\n }\\n reportParameters {\\n parameterValues {\\n ...ParameterValue\\n }\\n value\\n }\\n }\\n}\\n\\nfragment ParameterCategory on ParameterCategory {\\n id\\n name\\n description\\n}\\n\\nfragment ParameterValue on ParameterValue {\\n id\\n name\\n description\\n value\\n}\\n\",\n\t\"variables\": {\n\t\t\"id\": \"2\"\n\t}\n}"; request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { return(null); } await using var responseStream = await response.Content.ReadAsStreamAsync(); var visitBeachesDto = await JsonSerializer.DeserializeAsync <VisitBeachesDto>(responseStream, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, }); // Strip out the info we really care about BeachConditionsDto beachConditionsDto; try { var beachReport = visitBeachesDto.Data.Beach.LastThreeDaysOfReports[0]; var updatedTime = beachReport.CreatedAt; var flagColor = beachReport.BeachReport[0].ReportParameters[0].ParameterValues[0].Name; var respiratoryIrritation = beachReport.BeachReport[6].ReportParameters[0].ParameterValues[0].Name; var surfHeight = beachReport.BeachReport[2].ReportParameters[1].ParameterValues[0].Name; var surfCondition = beachReport.BeachReport[2].ReportParameters[3].ParameterValues[0].Name; var jellyFish = beachReport.BeachReport[8].ReportParameters[0].ParameterValues[0].Name; beachConditionsDto = new BeachConditionsDto() { UpdatedTime = updatedTime, FlagColor = flagColor, RespiratoryIrritation = respiratoryIrritation, SurfHeight = surfHeight, SurfCondition = surfCondition, JellyFishPresent = jellyFish, }; } catch (Exception e) { // Something messed up! _logger.LogError(e, e.InnerException?.Message); return(null); } // Save to cache var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetAbsoluteExpiration(TimeSpan .FromMinutes(15)); // Save data in cache. _memoryCache.Set("GetBeachConditions", beachConditionsDto, cacheEntryOptions); return(beachConditionsDto); }