Example #1
0
        public async Task <IActionResult> UpdateWeatherForecast(UpdateWeatherForecastRequest request)
        {
            // HACK: This try/catch should not be here. We only have to capture the ArgumentException because
            // the mapper is handling the validation for the three input temperature values in the WeatherForecastViewModel.
            // This is an example of technical debt; a quick solution in the short term to meet a deadline (say, a training
            // exercise that's occuring in T - 5 hours...), but one that needs to be tracked. The correct solution here would
            // be to write a custom model binder for the WeatherForecastViewModel object where it can validate that the three
            // input temperatures match and fail to bind if they don't.
            WeatherForecast newForecast;

            try
            {
                newForecast = _mapper.Map <WeatherForecastViewModel, WeatherForecast>(request.WeatherForecast);
            }
            catch (ArgumentException e)
            {
                return(BadRequest(e.Message));
            }


            newForecast = await _weatherForecastService.UpdateWeatherForecast(newForecast);

            var result = _mapper.Map <WeatherForecast, WeatherForecastViewModel>(newForecast);

            var response = new WeatherForecastResponse
            {
                Forecast = result
            };

            return(Ok(response));
        }
Example #2
0
        public async Task <WeatherForecastData> UpdateWeatherForecast(WeatherForecastData weatherForecastData)
        {
            var forecast = _mapper.Map <WeatherForecastData, WeatherForecast>(weatherForecastData);

            var request = new UpdateWeatherForecastRequest
            {
                Forecast = forecast
            };

            var response = await _client.UpdateWeatherForecastAsync(request);

            return(_mapper.Map <WeatherForecast, WeatherForecastData>(response.Forecast));
        }
Example #3
0
        public async Task Execute(Guid id, UpdateWeatherForecastRequest input, string username)
        {
            var query  = new GetWeatherForecastByIdQuery(id);
            var entity = await _queryDispatcher.Execute <GetWeatherForecastByIdQuery, WeatherForecastEntity>(query);

            if (entity == null)
            {
                throw new NotFoundException($"No weather forecast found with ID = '{id}'.");
            }

            entity.Temperature = input.TemperatureInCelsius;
            entity.Date        = input.Date;
            entity.Summary     = input.Summary;

            var command = new UpdateWeatherForecastCommand(entity, username);
            await _commandDispatcher.Execute(command);
        }
Example #4
0
        public async Task <IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateWeatherForecastRequest input)
        {
            try
            {
                var useCase = _serviceProvider.GetService <IUpdateWeatherForecastUseCase>();
                await useCase.Execute(id, input, PrincipalAccessor.Principal.GetUsernameFromClaim());

                return(Ok());
            }
            catch (NotFoundException ex)
            {
                _logger.LogWarning(ex, ex.Message);
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #5
0
        public override async Task <UpdateWeatherForecastResponse> UpdateWeatherForecast(UpdateWeatherForecastRequest request, ServerCallContext context)
        {
            var forecastId = request.Forecast.Id;
            var date       = request.Forecast.Date;
            var locationId = request.Forecast.Location.Id;

            if (forecastId <= 0)
            {
                const string error = "Forecast id must be a positive integer";
                throw new RpcException(new Status(StatusCode.InvalidArgument, error));
            }

            if (string.IsNullOrEmpty(date))
            {
                const string error = "date cannot be null or empty.";
                throw new RpcException(new Status(StatusCode.InvalidArgument, error));
            }

            if (locationId <= 0)
            {
                const string error = "Location id must be a positive integer";
                throw new RpcException(new Status(StatusCode.InvalidArgument, error));
            }

            var weatherForecast = request.Forecast;

            var weatherForecastStoredData = _mapper.Map <WeatherForecast, WeatherForecastStoredData>(weatherForecast);

            var updatedData = await _repository.UpdateWeatherForecast(weatherForecastStoredData);

            var result = _mapper.Map <WeatherForecastStoredData, WeatherForecast>(updatedData);

            return(new UpdateWeatherForecastResponse
            {
                Forecast = result
            });
        }