Пример #1
0
        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));
        }
Пример #2
0
        public async Task <IOperationResult> HandleAsync(IUpdateOffer command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var offer = await _repository.GetAsync(command.Id);

            if (offer is null)
            {
                throw new BaristaException("offer_not_found", $"Could not find offer with ID '{command.Id}'");
            }

            await Task.WhenAll(
                _posVerifier.AssertExists(command.PointOfSaleId),
                _productVerifier.AssertExists(command.ProductId),
                command.StockItemId != null?_stockItemVerifier.AssertExists(command.StockItemId.Value) : Task.CompletedTask
                );

            offer.SetPointOfSaleId(command.PointOfSaleId);
            offer.SetProductId(command.ProductId);
            offer.SetRecommendedPrice(command.RecommendedPrice);
            offer.SetStockItemId(command.StockItemId);
            offer.SetValidity(command.ValidSince, command.ValidUntil);

            await _repository.UpdateAsync(offer);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new OfferUpdated(offer.Id, offer.PointOfSaleId, offer.ProductId,
                                                         offer.RecommendedPrice, offer.StockItemId, offer.ValidSince, offer.ValidUntil));

            return(OperationResult.Ok());
        }
Пример #3
0
        public async Task <IIdentifierResult> HandleAsync(ICreateOffer command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            await Task.WhenAll(
                _posVerifier.AssertExists(command.PointOfSaleId),
                _productVerifier.AssertExists(command.ProductId),
                command.StockItemId != null?_stockItemVerifier.AssertExists(command.StockItemId.Value) : Task.CompletedTask
                );

            var offer = new Domain.Offer(command.Id, command.PointOfSaleId, command.ProductId, command.RecommendedPrice, command.StockItemId, command.ValidSince, command.ValidUntil);
            await _repository.AddAsync(offer);

            try
            {
                await _repository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("offer_already_exists", $"An offer with the ID '{command.Id}' already exists.");
            }


            await _busPublisher.Publish(new OfferCreated(offer.Id, offer.PointOfSaleId, offer.ProductId,
                                                         offer.RecommendedPrice, offer.StockItemId, offer.ValidSince, offer.ValidUntil));

            return(new IdentifierResult(offer.Id));
        }
Пример #4
0
        public async Task <IOperationResult> HandleAsync(IUpdateSale command, ICorrelationContext context)
        {
            var sale = await _salesRepository.GetAsync(command.Id);

            if (sale is null)
            {
                throw new BaristaException("sale_not_found", $"Sale with ID '{command.Id}' was not found");
            }

            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)
                );

            sale.SetAuthenticationMeansId(command.AuthenticationMeansId);
            sale.SetAccountingGroupId(command.AccountingGroupId);
            sale.SetCost(command.Cost);
            sale.SetPointOfSaleId(command.PointOfSaleId);
            sale.SetQuantity(command.Quantity);
            sale.SetUserId(command.UserId);
            sale.SetProductId(command.ProductId);
            sale.SetOfferId(command.OfferId);

            await _salesRepository.UpdateAsync(sale);

            await _salesRepository.SaveChanges();

            await _busPublisher.Publish(new SaleUpdated(sale));

            return(OperationResult.Ok());
        }