public async Task <ApplicationDataResult <PurchaseDto> > AddAsync(AddOrUpdatePurchaseDto data) { ValidationResult validationResult = await _validatorPurchase.ValidateAsync(data); if (validationResult.IsValid is false) { return(FactoryFromValidationResult <PurchaseDto>(validationResult)); } Purchase purchase = _mapper.Map <Purchase>(data); await _purchaseRepository.AddAsync(purchase); await _purchaseRepository.UnitOfWork.CommitAsync(); return(FactoryResultFromData(_mapper.Map <PurchaseDto>(purchase))); }
public async Task PurchaseApplicationService_AddAsync_DeveRetornarErrosDeValidacaoQuandoEmEstadoInvalido() { // Arange ConfigureMock(); FactoryService(); AddOrUpdatePurchaseDto addOrUpdatePurchaseDto = AddOrUpdatePurchaseDtoFaker.GenerateInvalidFaker().Generate(); // Act ApplicationDataResult <PurchaseDto> result = await _purchaseApplicationService.AddAsync(addOrUpdatePurchaseDto); // Assert result.IsSuccess.Should().BeFalse(); result.Errors.Should().HaveCount(3); result.Data.Should().BeNull(); _purchaseRepositoryMock.Verify(x => x.AddAsync(It.IsAny <Purchase>()), Times.Never); _unitOfWork.Verify(x => x.CommitAsync(), Times.Never); }
public async Task PurchaseApplicationService_AddAsync_DeveCriarCorretamenteORegistroQuandoEmEstadoValido() { // Arange ConfigureMock(); FactoryService(); AddOrUpdatePurchaseDto addOrUpdatePurchaseDto = AddOrUpdatePurchaseDtoFaker.GenerateValidFaker().Generate(); // Act ApplicationDataResult <PurchaseDto> result = await _purchaseApplicationService.AddAsync(addOrUpdatePurchaseDto); // Assert result.IsSuccess.Should().BeTrue(); result.Errors.Should().BeEmpty(); result.Data.Should().NotBeNull(); _purchaseRepositoryMock.Verify(x => x.AddAsync(It.IsAny <Purchase>()), Times.Once); _unitOfWork.Verify(x => x.CommitAsync(), Times.Once); }
public async Task <ApplicationDataResult <PurchaseDto> > UpdateAsync(Guid purchaseId, AddOrUpdatePurchaseDto data) { ValidationResult validationResult = await _validatorPurchase.ValidateAsync(data); if (validationResult.IsValid is false) { return(FactoryFromValidationResult <PurchaseDto>(validationResult)); } Purchase purchase = await _purchaseRepository.FindAsync(purchaseId); if (purchase is null) { throw ExceptionsFactory.FactoryNotFoundException <Purchase>(purchaseId); } purchase.Update(data.Description, data.Date); _purchaseRepository.Update(purchase); await _purchaseRepository.UnitOfWork.CommitAsync(); return(FactoryResultFromData(_mapper.Map <PurchaseDto>(purchase))); }