public async Task <IIdentifierResult> HandleAsync(ICreateSale command, ICorrelationContext context) { await Task.WhenAll( _agVerifier.AssertExists(command.AccountingGroupId), _userVerifier.AssertExists(command.UserId), _posVerifier.AssertExists(command.PointOfSaleId), _amVerifier.AssertExists(command.AuthenticationMeansId), _productVerifier.AssertExists(command.ProductId), _offerVerifier.AssertExists(command.OfferId) ); var sale = new Domain.Sale(command.Id, command.Cost, command.Quantity, command.AccountingGroupId, command.UserId, command.AuthenticationMeansId, command.PointOfSaleId, command.ProductId, command.OfferId); var initialSaleStateChange = new Domain.SaleStateChange(Guid.NewGuid(), "Created", default(SaleState), command.PointOfSaleId, null); sale.AddStateChange(initialSaleStateChange); await _salesRepository.AddAsync(sale); try { await _salesRepository.SaveChanges(); } catch (EntityAlreadyExistsException) { throw new BaristaException("sale_already_exists", $"A sale with the ID '{command.Id}' already exists."); } await _busPublisher.Publish(new SaleCreated(sale)); await _busPublisher.Publish(new SaleStateChangeCreated(initialSaleStateChange.Id, sale.Id, initialSaleStateChange.Created, initialSaleStateChange.Reason, initialSaleStateChange.State.ToString(), initialSaleStateChange.CausedByPointOfSaleId, initialSaleStateChange.CausedByUserId)); return(new IdentifierResult(sale.Id)); }
public async Task <IIdentifierResult> HandleAsync(ICreateSaleStateChange command, ICorrelationContext context) { var sale = await _salesRepository.GetAsync(command.ParentSaleId); if (sale is null) { throw new BaristaException("sale_not_found", $"Sale with ID '{command.ParentSaleId}' was not found"); } if (command.CausedByUserId != null) { await _userVerifier.AssertExists(command.CausedByUserId.Value); } if (command.CausedByPointOfSaleId != null) { await _posVerifier.AssertExists(command.CausedByPointOfSaleId.Value); } var saleStateChange = new Domain.SaleStateChange(command.Id, command.Reason, Enum.Parse <SaleState>(command.State), command.CausedByPointOfSaleId, command.CausedByUserId); sale.AddStateChange(saleStateChange); await _salesRepository.UpdateAsync(sale); await _salesRepository.SaveChanges(); await _busPublisher.Publish(new SaleStateChangeCreated(saleStateChange.Id, sale.Id, saleStateChange.Created, saleStateChange.Reason, saleStateChange.State.ToString(), saleStateChange.CausedByPointOfSaleId, saleStateChange.CausedByUserId)); return(new IdentifierResult(sale.Id)); }