Beispiel #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(ICreateAccountingGroup command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            await _saleStrategyVerifier.AssertExists(command.SaleStrategyId);

            var accountingGroup = new Domain.AccountingGroup(command.Id, command.DisplayName, command.SaleStrategyId);
            await _accGroupRepository.AddAsync(accountingGroup);

            try
            {
                await _accGroupRepository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("accounting_group_already_exists", $"An accounting group with the ID '{command.Id}' already exists.");
            }

            await _busPublisher.Publish(new AccountingGroupCreated(command.Id, command.DisplayName, command.SaleStrategyId));

            return(new IdentifierResult(accountingGroup.Id));
        }
Beispiel #3
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());
        }
Beispiel #4
0
        public async Task <IOperationResult> HandleAsync(IUpdatePointOfSale command, ICorrelationContext context)
        {
            var pos = await _repository.GetAsync(command.Id);

            if (pos is null)
            {
                throw new BaristaException("point_of_sale_not_found", $"Could not find point of sale with ID '{command.Id}'");
            }

            await Task.WhenAll(
                _agVerifier.AssertExists(command.ParentAccountingGroupId),
                command.SaleStrategyId != null?_ssVerifier.AssertExists(command.SaleStrategyId.Value) : Task.CompletedTask
                );

            pos.SetDisplayName(command.DisplayName);
            pos.SetSaleStrategyId(command.SaleStrategyId);
            pos.SetParentAccountingGroupId(command.ParentAccountingGroupId);
            pos.SetFeatures(command.Features ?? new string[0]);
            await _repository.UpdateAsync(pos);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new PointOfSaleUpdated(pos.Id, pos.DisplayName, pos.ParentAccountingGroupId, pos.SaleStrategyId));

            return(OperationResult.Ok());
        }
Beispiel #5
0
        public async Task <IIdentifierResult> HandleAsync(ICreateManualStockOperation command, ICorrelationContext context)
        {
            await Task.WhenAll(
                _stockItemVerifier.AssertExists(command.StockItemId),
                _userVerifier.AssertExists(command.CreatedByUserId)
                );

            var manualStockOp = new Domain.ManualStockOperation(command.Id, command.StockItemId, command.Quantity, command.CreatedByUserId, command.Comment);
            await _repository.AddAsync(manualStockOp);


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


            await _busPublisher.Publish(new ManualStockOperationCreated(manualStockOp.Id, manualStockOp.StockItemId, manualStockOp.Quantity, manualStockOp.CreatedByUserId, manualStockOp.Comment));

            return(new IdentifierResult(manualStockOp.Id));
        }
Beispiel #6
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(IUpdateUser command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            var user = await _repository.GetAsync(command.Id);

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

            user.SetFullName(command.FullName);
            user.SetEmailAddress(command.EmailAddress);
            user.SetIsAdministrator(command.IsAdministrator);
            user.SetIsActive(command.IsActive);

            await _repository.UpdateAsync(user);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new UserUpdated(user.Id, user.FullName, user.EmailAddress, user.IsAdministrator, user.IsActive));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(IUnsetOfferStockItemReference 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}'");
            }

            if (offer.StockItemId == command.StockItemIdToUnset)
            {
                offer.SetStockItemId(null);
                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());
        }
        public async Task <IParentChildIdentifierResult> HandleAsync(ICreateAccountingGroupUserAuthorization command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (!Enum.TryParse <UserAuthorizationLevel>(command.Level, out var userAuthLevel))
            {
                throw new BaristaException("invalid_user_auth_level", $"Could not parse user authorization level '{command.Level}'");
            }

            await Task.WhenAll(
                _userVerifier.AssertExists(command.UserId),
                _agVerifier.AssertExists(command.AccountingGroupId)
                );

            var authorizedUser = new Domain.UserAuthorization(command.AccountingGroupId, command.UserId, userAuthLevel);
            await _repository.AddAsync(authorizedUser);

            try
            {
                await _repository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("accounting_group_user_authorization_already_exists", $"User with ID '{command.AccountingGroupId}' is already an authorized user to accounting group with ID '{command.UserId}'.");
            }

            await _busPublisher.Publish(new UserAuthorizationCreated(command.AccountingGroupId, command.UserId, command.Level));

            return(new ParentChildIdentifierResult(command.AccountingGroupId, authorizedUser.UserId));
        }
        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());
        }
Beispiel #11
0
        public async Task <IIdentifierResult> HandleAsync(ICreateUser command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var user = new Domain.User(command.Id, command.FullName, command.EmailAddress, command.IsAdministrator, command.IsActive);
            await _repository.AddAsync(user);


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


            await _busPublisher.Publish(new UserCreated(user.Id, user.FullName, user.EmailAddress, user.IsAdministrator, user.IsActive));

            return(new IdentifierResult(user.Id));
        }
Beispiel #12
0
        public async Task <IOperationResult> HandleAsync(IUpdateSaleBasedStockOperation command, ICorrelationContext context)
        {
            var stockOp = await _repository.GetAsync(command.Id);

            if (stockOp is null)
            {
                throw new BaristaException("stock_operation_not_found", $"Could not find stock operation with ID '{command.Id}'");
            }
            if (!(stockOp is Domain.SaleBasedStockOperation saleBasedStockOp))
            {
                throw new BaristaException("invalid_stock_operation_update_command", $"The stock operation with ID '{command.Id}' is not a sale-based stock operation and therefore cannot be updated with this command.");
            }

            await Task.WhenAll(
                _stockItemVerifier.AssertExists(command.StockItemId),
                _saleVerifier.AssertExists(command.SaleId)
                );

            saleBasedStockOp.SetQuantity(command.Quantity);
            saleBasedStockOp.SetStockItem(command.StockItemId);
            saleBasedStockOp.SetSaleId(command.SaleId);

            await _repository.UpdateAsync(saleBasedStockOp);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new SaleBasedStockOperationUpdated(saleBasedStockOp.Id, saleBasedStockOp.StockItemId, saleBasedStockOp.Quantity, saleBasedStockOp.SaleId));

            return(OperationResult.Ok());
        }
Beispiel #13
0
        public async Task <IOperationResult> HandleAsync(IDeleteAssignmentToPointOfSale command, ICorrelationContext context)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var assignment = await _assignmentsRepository.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))
            {
                throw new BaristaException("invalid_command", $"The assignment with ID '{command.Id}' is not an assignment to point of sale and as such cannot be deleted with this command.");
            }

            await _assignmentsRepository.DeleteAsync(assignment);

            await _assignmentsRepository.SaveChanges();

            await _busPublisher.Publish(new AssignmentToPointOfSaleDeleted(command.Id));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(IUpdateAuthenticationMeans command, ICorrelationContext context)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

            if (means is null)
            {
                throw new BaristaException("means_not_found", $"Authentication means with ID '{command.Id}' was not found");
            }

            means.SetMethod(command.Type);
            means.SetLabel(command.Label);
            means.SetValidity(command.ValidSince, command.ValidUntil);

            await _repository.UpdateAsync(means);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new AuthenticationMeansUpdated(means.Id, means.Label, means.Method, means.ValidSince, means.ValidUntil));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(IUpdateAccountingGroup command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

            if (accountingGroup is null)
            {
                throw new BaristaException("accounting_group_not_found", $"Could not find accounting group with ID '{command.Id}'");
            }

            await _saleStrategyVerifier.AssertExists(command.SaleStrategyId);

            accountingGroup.SetDisplayName(command.DisplayName);
            accountingGroup.SetSaleStrategyId(command.SaleStrategyId);
            await _repository.UpdateAsync(accountingGroup);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new AccountingGroupUpdated(accountingGroup.Id, accountingGroup.DisplayName, accountingGroup.SaleStrategyId));

            return(OperationResult.Ok());
        }
Beispiel #16
0
        public async Task <IOperationResult> HandleAsync(IDeleteSaleBasedStockOperation command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

            if (stockOp is null)
            {
                throw new BaristaException("stock_operation_not_found", $"Could not find stock operation with ID '{command.Id}'");
            }
            if (!(stockOp is Domain.SaleBasedStockOperation))
            {
                throw new BaristaException("invalid_stock_operation_delete_command", $"The stock operation with ID '{command.Id}' is not a sale-based stock operation and therefore cannot be deleted with this command.");
            }

            await _repository.DeleteAsync(stockOp);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new SaleBasedStockOperationDeleted(stockOp.Id));

            return(OperationResult.Ok());
        }
        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));
        }
Beispiel #18
0
        public async Task <IOperationResult> HandleAsync(IDeletePayment command, ICorrelationContext context)
        {
            var payment = await _paymentsRepository.GetAsync(command.Id);

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

            await _paymentsRepository.DeleteAsync(payment);

            await _paymentsRepository.SaveChanges();

            await _busPublisher.Publish(new PaymentDeleted(command.Id));

            return(OperationResult.Ok());
        }
Beispiel #19
0
        public async Task <IOperationResult> HandleAsync(IDeleteSale command, ICorrelationContext context)
        {
            var sale = await _salesRepository.GetAsync(command.Id);

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

            await _salesRepository.DeleteAsync(sale);

            await _salesRepository.SaveChanges();

            await _busPublisher.Publish(new SaleDeleted(command.Id));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(IUpdateAssignmentToUser 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.AssignmentToUser assignmentToUser))
            {
                throw new BaristaException("invalid_assignment_update_command", "This assignment does not assign means to an user and as such cannot be updated with this command.");
            }

            await Task.WhenAll(
                _meansVerifier.AssertExists(command.MeansId),
                _userVerifier.AssertExists(command.UserId)
                );

            assignment.SetMeansId(command.MeansId);
            assignment.SetValidity(command.ValidSince, command.ValidUntil);
            assignmentToUser.SetIsShared(command.IsShared);
            assignmentToUser.SetUserId(command.UserId);

            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 AssignmentToUserUpdated(assignmentToUser.Id, assignmentToUser.MeansId,
                                                                    assignmentToUser.ValidSince, assignmentToUser.ValidUntil, assignmentToUser.UserId,
                                                                    assignmentToUser.IsShared));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(ISetPointOfSaleKeyValue command, ICorrelationContext correlationContext)
        {
            var pos = await _repository.GetAsync(command.PointOfSaleId);

            if (pos is null)
            {
                throw new BaristaException("point_of_sale_not_found", $"Could not find point of sale with ID '{command.PointOfSaleId}'");
            }

            pos.SetKeyValue(command.Key, command.Value);
            await _repository.UpdateAsync(pos);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new PointOfSaleKeyValueUpdated(pos.Id, command.Key, command.Value));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(IDeleteAuthenticationMeans command, ICorrelationContext context)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (!(await _repository.GetAsync(command.Id) is Domain.AuthenticationMeans means))
            {
                throw new BaristaException("means_not_found", $"Authentication means with ID '{command.Id}' was not found");
            }

            await _repository.DeleteAsync(means);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new AuthenticationMeansDeleted(command.Id));

            return(OperationResult.Ok());
        }
Beispiel #23
0
        public async Task <IIdentifierResult> HandleAsync(ICreatePayment command, ICorrelationContext context)
        {
            await _userVerifier.AssertExists(command.UserId);

            var payment = new Domain.Payment(command.Id, command.Amount, command.UserId, command.Source, command.ExternalId);
            await _paymentsRepository.AddAsync(payment);

            try
            {
                await _paymentsRepository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("payment_already_exists", $"A payment with the ID '{command.Id}' already exists.");
            }

            await _busPublisher.Publish(new PaymentCreated(payment));

            return(new IdentifierResult(payment.Id));
        }
        public async Task <IOperationResult> HandleAsync(IDeleteUser command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            var user = await _repository.GetAsync(command.Id);

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

            await _repository.DeleteAsync(user);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new UserDeleted(user.Id));

            return(OperationResult.Ok());
        }
        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());
        }
Beispiel #26
0
        public async Task <IOperationResult> HandleAsync(IDeleteSpendingLimit command, ICorrelationContext context)
        {
            var assignmentToUser = await GetAssignmentToUser(_repository, command.ParentUserAssignmentId);

            var spendingLimit = assignmentToUser.SpendingLimits.FirstOrDefault(sl => sl.Id == command.Id);

            if (spendingLimit is null)
            {
                throw new BaristaException("spending_limit_not_found", $"Spending limit with ID '{command.Id}' was not found in assignment '{command.ParentUserAssignmentId}");
            }

            assignmentToUser.SpendingLimits.Remove(spendingLimit);

            await _repository.UpdateAsync(assignmentToUser);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new SpendingLimitDeleted(spendingLimit.Id, assignmentToUser.Id));

            return(OperationResult.Ok());
        }
Beispiel #27
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));
        }
Beispiel #28
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 <IOperationResult> HandleAsync(IDeleteProduct command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

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

            await _repository.DeleteAsync(product);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new ProductDeleted(product.Id));

            return(OperationResult.Ok());
        }
        public async Task <IOperationResult> HandleAsync(IDeletePointOfSale command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var pos = await _pointOfSaleRepository.GetAsync(command.Id);

            if (pos is null)
            {
                throw new BaristaException("point_of_sale_not_found", $"Could not find point of sale with ID {command.Id}");
            }

            await _pointOfSaleRepository.DeleteAsync(pos);

            await _pointOfSaleRepository.SaveChanges();

            await _busPublisher.Publish(new PointOfSaleDeleted(pos.Id));

            return(OperationResult.Ok());
        }