Example #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));
        }
        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));
        }
Example #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));
        }
        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());
        }
Example #5
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());
        }
        public async Task <IOperationResult> HandleAsync(IUpdateAssignmentToPointOfSale command, ICorrelationContext context)
        {
            var assignment = await _repository.GetAsync(command.Id);

            if (assignment is null)
            {
                throw new BaristaException("assignment_not_found", $"Assignment with ID '{command.Id}' was not found");
            }
            if (!(assignment is Domain.AssignmentToPointOfSale assignmentToUser))
            {
                throw new BaristaException("invalid_assignment_update_command", "This assignment does not assign means to a point of sale and as such cannot be updated with this command.");
            }

            await Task.WhenAll(
                _meansVerifier.AssertExists(command.MeansId),
                _posVerifier.AssertExists(command.PointOfSaleId)
                );

            assignment.SetMeansId(command.MeansId);
            assignment.SetValidity(command.ValidSince, command.ValidUntil);
            assignmentToUser.SetPointOfSale(command.PointOfSaleId);

            await _repository.UpdateAsync(assignmentToUser);

            await _repository.SaveChanges();

            try
            {
                await _exclVerifier.VerifyAssignmentExclusivity(command.MeansId);
            }
            catch (BaristaException)
            {
                await _repository.DeleteAsync(assignment);

                await _repository.SaveChanges();

                throw;
            }

            await _busPublisher.Publish(new AssignmentToPointOfSaleUpdated(assignmentToUser.Id,
                                                                           assignmentToUser.MeansId, assignmentToUser.ValidSince, assignmentToUser.ValidUntil,
                                                                           assignmentToUser.PointOfSaleId));

            return(OperationResult.Ok());
        }
Example #7
0
        public async Task <IIdentifierResult> HandleAsync(ICreateStockItem command, ICorrelationContext context)
        {
            await _posVerifier.AssertExists(command.PointOfSaleId);

            var stockItem = new Domain.StockItem(command.Id, command.DisplayName, command.PointOfSaleId);
            await _repository.AddAsync(stockItem);

            try
            {
                await _repository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("stock_item_already_exists", $"A stock item with the ID '{command.Id}' already exists.");
            }


            await _busPublisher.Publish(new StockItemCreated(stockItem.Id, stockItem.DisplayName, stockItem.PointOfSaleId));

            return(new IdentifierResult(stockItem.Id));
        }
        public async Task <IOperationResult> HandleAsync(IUpdateStockItem command, ICorrelationContext context)
        {
            var stockItem = await _repository.GetAsync(command.Id);

            if (stockItem is null)
            {
                throw new BaristaException("stock_item_not_found", $"Could not find stock item with ID '{command.Id}'");
            }

            await _posVerifier.AssertExists(command.PointOfSaleId);

            stockItem.SetDisplayName(command.DisplayName);
            stockItem.SetPointOfSaleId(command.PointOfSaleId);
            await _repository.UpdateAsync(stockItem);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new StockItemUpdated(stockItem.Id, stockItem.DisplayName, stockItem.PointOfSaleId));

            return(OperationResult.Ok());
        }
Example #9
0
        public async Task <IOperationResult> HandleAsync(IUpdateSaleStateChange 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");
            }

            var saleStateChange = sale.StateChanges.FirstOrDefault(chg => chg.Id == command.Id);

            if (saleStateChange is null)
            {
                throw new BaristaException("sale_state_change_not_found", $"Sale state change with ID '{command.Id}' not found for sale with ID '{command.ParentSaleId}");
            }

            if (command.CausedByUserId != null)
            {
                await _userVerifier.AssertExists(command.CausedByUserId.Value);
            }

            if (command.CausedByPointOfSaleId != null)
            {
                await _posVerifier.AssertExists(command.CausedByPointOfSaleId.Value);
            }

            saleStateChange.SetReason(command.Reason);
            saleStateChange.SetCausedByPointOfSaleId(command.CausedByPointOfSaleId);
            saleStateChange.SetCausedByUserId(command.CausedByUserId);

            await _salesRepository.UpdateAsync(sale);

            await _salesRepository.SaveChanges();

            await _busPublisher.Publish(new SaleStateChangeUpdated(saleStateChange.Id, saleStateChange.Created,
                                                                   saleStateChange.Reason, saleStateChange.State.ToString(), saleStateChange.CausedByPointOfSaleId,
                                                                   saleStateChange.CausedByUserId));

            return(OperationResult.Ok());
        }
        public async Task <IIdentifierResult> HandleAsync(ICreateAssignmentToPointOfSale command, ICorrelationContext context)
        {
            await Task.WhenAll(
                _meansVerifier.AssertExists(command.MeansId),
                _posVerifier.AssertExists(command.PointOfSaleId)
                );

            var assignment = new Domain.AssignmentToPointOfSale(command.Id, command.MeansId, command.ValidSince, command.ValidUntil, command.PointOfSaleId);
            await _repository.AddAsync(assignment);

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

            try
            {
                await _exclVerifier.VerifyAssignmentExclusivity(command.MeansId);
            }
            catch (BaristaException)
            {
                await _repository.DeleteAsync(assignment);

                await _repository.SaveChanges();

                throw;
            }

            await _busPublisher.Publish(new AssignmentToPointOfSaleCreated(command.Id, command.MeansId, command.ValidSince, command.ValidUntil, command.PointOfSaleId));

            return(new IdentifierResult(assignment.Id));
        }