Esempio n. 1
0
        public async Task <bool> HandleAsync(ModifyItemCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var storeItem = await itemRepository.FindByAsync(command.ItemModify.Id, cancellationToken);

            if (storeItem == null)
            {
                throw new DomainException(new ItemNotFoundReason(command.ItemModify.Id));
            }
            if (storeItem.IsTemporary)
            {
                throw new DomainException(new TemporaryItemNotModifyableReason(command.ItemModify.Id));
            }

            var itemCategoryId = command.ItemModify.ItemCategoryId;
            var manufacturerId = command.ItemModify.ManufacturerId;

            await itemCategoryValidationService.ValidateAsync(itemCategoryId, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            if (manufacturerId != null)
            {
                await manufacturerValidationService.ValidateAsync(manufacturerId, cancellationToken);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var availabilities = command.ItemModify.Availabilities;
            await availabilityValidationService.ValidateAsync(availabilities, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            storeItem.Modify(command.ItemModify, availabilities);

            var availableAtStoreIds   = storeItem.Availabilities.Select(av => av.StoreId);
            var shoppingListsWithItem = (await shoppingListRepository.FindByAsync(storeItem.Id, cancellationToken))
                                        .Where(list => !availableAtStoreIds.Any(storeId => storeId == list.StoreId))
                                        .ToList();

            using var transaction = await transactionGenerator.GenerateAsync(cancellationToken);

            await itemRepository.StoreAsync(storeItem, cancellationToken);

            foreach (var list in shoppingListsWithItem)
            {
                cancellationToken.ThrowIfCancellationRequested();
                // remove items from all shopping lists where item is not available anymore
                list.RemoveItem(storeItem.Id);
                await shoppingListRepository.StoreAsync(list, cancellationToken);
            }

            await transaction.CommitAsync(cancellationToken);

            return(true);
        }
Esempio n. 2
0
        public async Task <bool> HandleAsync(CreateItemCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var itemCategoryId = command.ItemCreation.ItemCategoryId;
            var manufacturerId = command.ItemCreation.ManufacturerId;

            await itemCategoryValidationService.ValidateAsync(itemCategoryId, cancellationToken);

            if (manufacturerId != null)
            {
                await manufacturerValidationService.ValidateAsync(manufacturerId, cancellationToken);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var availabilities = command.ItemCreation.Availabilities;
            await availabilityValidationService.ValidateAsync(availabilities, cancellationToken);

            var storeItem = storeItemFactory.Create(command.ItemCreation);

            await itemRepository.StoreAsync(storeItem, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            return(true);
        }
        public async Task <bool> HandleAsync(UpdateItemCommand command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new System.ArgumentNullException(nameof(command));
            }

            IStoreItem oldItem = await itemRepository.FindByAsync(command.ItemUpdate.OldId, cancellationToken);

            if (oldItem == null)
            {
                throw new DomainException(new ItemNotFoundReason(command.ItemUpdate.OldId));
            }
            if (oldItem.IsTemporary)
            {
                throw new DomainException(new TemporaryItemNotUpdateableReason(command.ItemUpdate.OldId));
            }

            oldItem.Delete();

            var itemCategoryId = command.ItemUpdate.ItemCategoryId;
            var manufacturerId = command.ItemUpdate.ManufacturerId;

            await itemCategoryValidationService.ValidateAsync(itemCategoryId, cancellationToken);

            if (manufacturerId != null)
            {
                await manufacturerValidationService.ValidateAsync(manufacturerId, cancellationToken);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var availabilities = command.ItemUpdate.Availabilities;
            await availabilityValidationService.ValidateAsync(availabilities, cancellationToken);

            using ITransaction transaction = await transactionGenerator.GenerateAsync(cancellationToken);

            await itemRepository.StoreAsync(oldItem, cancellationToken);

            // create new Item
            IStoreItem updatedItem = storeItemFactory.Create(command.ItemUpdate, oldItem);

            updatedItem = await itemRepository.StoreAsync(updatedItem, cancellationToken);

            // change existing item references on shopping lists
            await shoppingListUpdateService.ExchangeItemAsync(oldItem.Id, updatedItem, cancellationToken);

            await transaction.CommitAsync(cancellationToken);

            return(true);
        }
Esempio n. 4
0
        public async Task <bool> HandleAsync(MakeTemporaryItemPermanentCommand command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            IStoreItem storeItem = await itemRepository.FindByAsync(command.PermanentItem.Id, cancellationToken);

            if (storeItem == null)
            {
                throw new DomainException(new ItemNotFoundReason(command.PermanentItem.Id));
            }
            if (!storeItem.IsTemporary)
            {
                throw new DomainException(new ItemNotTemporaryReason(command.PermanentItem.Id));
            }

            var itemCategoryId = command.PermanentItem.ItemCategoryId;
            var manufacturerId = command.PermanentItem.ManufacturerId;

            await itemCategoryValidationService.ValidateAsync(itemCategoryId, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            if (command.PermanentItem.ManufacturerId != null)
            {
                await manufacturerValidationService.ValidateAsync(manufacturerId, cancellationToken);
            }

            var availabilities = command.PermanentItem.Availabilities;
            await availabilityValidationService.ValidateAsync(availabilities, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            storeItem.MakePermanent(command.PermanentItem, availabilities);
            await itemRepository.StoreAsync(storeItem, cancellationToken);

            return(true);
        }