public async Task <IActionResult> AddTariffAsync([FromBody] VisitTariffDto tariff) { _logger.LogInformation($"Starting method '{nameof(AddTariffAsync)}'."); try { // Ignore Id if the client set it. Id of entity is set internally by the server. tariff.Id = null; var tariffToBeAdded = MapToDomainModel(tariff); var addedTariff = await _tariffDbService.RestrictedAddAsync(tariffToBeAdded); // Reverse map only for response to the client. var addedTariffDto = MapToDto(addedTariff); var response = new ResponseWrapper(addedTariffDto); string addedTariffUrl = $"{ControllerPrefix}/{addedTariff.Id}"; _logger.LogInformation($"Finished method '{nameof(AddTariffAsync)}'."); return(Created(addedTariffUrl, response)); } catch (InvalidOperationException ex) { return(OnInvalidParameterError($"Element '{typeof(VisitTariff).Name}' already exists. Value of '{nameof(tariff.Name)}' must be unique, but this one is not.", ex)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, _tariffDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }
public void Validate__Name_is_null_or_empty__Should_be_invalid([Values(null, "")] string name) { var invalidTariff = new VisitTariffDto { Name = name }; _validator.ShouldHaveValidationErrorFor(x => x.Name, invalidTariff); }
public void Validate__Name_lenght_is_less_than_50__Should_be_valid() { var validTariff = new VisitTariffDto { Name = "1234567890123456789012345678901234567890123456789" }; validTariff.Name.Length.Should().BeLessThan(50); _validator.ShouldNotHaveValidationErrorFor(x => x.Name, validTariff); }
public void Validate__Name_lenght_is_greater_than_50__Should_be_invalid() { var invalidTariff = new VisitTariffDto { Name = "123456789012345678901234567890123456789012345678901" }; invalidTariff.Name.Length.Should().BeGreaterThan(50); _validator.ShouldHaveValidationErrorFor(x => x.Name, invalidTariff); }
public async Task AddTariffAsync__An_unexpected_internal_error_occurred__Should_throw_Exception() { _tariffDbServiceMock.Setup(x => x.AddAsync(It.IsAny <VisitTariff>())).ThrowsAsync(new Exception()); _tariffDbServiceMock.Setup(x => x.RestrictedAddAsync(It.IsAny <VisitTariff>())).ThrowsAsync(new Exception()); var tariff = new VisitTariffDto { Id = "1", Name = "test" }; var controller = new VisitTariffsController(_tariffDbServiceMock.Object, _logger, _mapperMock.Object); Func <Task> result = async() => await controller.AddTariffAsync(tariff); await result.Should().ThrowExactlyAsync <Exception>(); }
public async Task AddTariffAsync__An_internal_error_reffered_to_the_database_occurred__Should_throw_InternalDbServiceException() { // Example of these errors: database does not exist, table does not exist etc. _tariffDbServiceMock.Setup(x => x.AddAsync(It.IsAny <VisitTariff>())).ThrowsAsync(new InternalDbServiceException()); _tariffDbServiceMock.Setup(x => x.RestrictedAddAsync(It.IsAny <VisitTariff>())).ThrowsAsync(new InternalDbServiceException()); var tariff = new VisitTariffDto { Id = "1", Name = "test" }; var controller = new VisitTariffsController(_tariffDbServiceMock.Object, _logger, _mapperMock.Object); Func <Task> result = async() => await controller.AddTariffAsync(tariff); await result.Should().ThrowExactlyAsync <InternalDbServiceException>(); }
public async Task <IActionResult> UpdateTariffAsync(string id, [FromBody] VisitTariffDto tariff) { _logger.LogInformation($"Starting method '{nameof(UpdateTariffAsync)}'."); if (string.IsNullOrEmpty(id)) { return(OnInvalidParameterError($"An argument '{nameof(id)}' cannot be null or empty.")); } if (!id.Equals(tariff.Id)) { return(OnMismatchParameterError($"An '{nameof(id)}' in URL end field '{nameof(tariff.Id).ToLower()}' in request body mismatches. Value in URL: '{id}'. Value in body: '{tariff.Id}'.")); } try { var tariffToBeUpdated = MapToDomainModel(tariff); var updatedTariff = await _tariffDbService.RestrictedUpdateAsync(tariffToBeUpdated); // Revers map for client response. tariff = MapToDto(updatedTariff); _logger.LogInformation($"Finished method '{nameof(UpdateTariffAsync)}'."); var response = new ResponseWrapper(tariff); return(Ok(response)); } catch (InvalidOperationException ex) { return(OnNotFoundError($"Cannot found element '{typeof(VisitTariff).Name}' with specified id: '{id}'.", ex)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, _tariffDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }
private VisitTariff MapToDomainModel(VisitTariffDto tariffDto) => _mapper.Map <VisitTariff>(tariffDto);