示例#1
0
        public async Task <BaseResponse> Handle(FinishRentCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Finishing rent : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao encerrar o empréstimo!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                if (!await _rentService.CheckIfRentExists(request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Empréstimo não existe na base de dados!", HttpStatusCode.NotFound, request));
                }

                await _repository.Finish(request.Id);

                _logger.LogInformation($"[End] - Rent successfully finished: { JsonSerializer.Serialize(request) }");

                return(new BaseResponse(true, "Empréstimo encerrado com sucesso!", HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while finishing the rent: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao encerrar o empréstimo!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }