public async Task <bool> HandleAsync(AddItemToShoppingListCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var list = await shoppingListRepository.FindByAsync(command.ShoppingListId, cancellationToken);

            if (list == null)
            {
                throw new DomainException(new ShoppingListNotFoundReason(command.ShoppingListId));
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (command.ItemId.IsActualId)
            {
                var actualId = new ItemId(command.ItemId.ActualId.Value);
                await addItemToShoppingListService.AddItemToShoppingList(list, actualId, command.SectionId, command.Quantity,
                                                                         cancellationToken);
            }
            else
            {
                var temporaryId = new TemporaryItemId(command.ItemId.OfflineId.Value);
                await addItemToShoppingListService.AddItemToShoppingList(list, temporaryId, command.SectionId,
                                                                         command.Quantity, cancellationToken);
            }

            cancellationToken.ThrowIfCancellationRequested();

            await shoppingListRepository.StoreAsync(list, cancellationToken);

            return(true);
        }
        public async Task HandleAsync_WithValidOfflineId_ShouldPutItemInBasket()
        {
            // Arrange
            var local   = new LocalFixture();
            var handler = local.CreateCommandHandler();
            var command = local.CreateCommandWithOfflineId();

            IStoreItem storeItem = local.StoreItemFixture.CreateValid();

            var temporaryItemId       = new TemporaryItemId(command.OfflineTolerantItemId.OfflineId.Value);
            ShoppingListMock listMock = local.ShoppingListMockFixture.Create();

            local.ShoppingListRepositoryMock.SetupFindByAsync(command.ShoppingListId, listMock.Object);
            local.ItemRepositoryMock.SetupFindByAsync(temporaryItemId, storeItem);

            // Act
            bool result = await handler.HandleAsync(command, default);

            // Assert
            using (new AssertionScope())
            {
                result.Should().BeTrue();
                local.ItemRepositoryMock.VerifyFindByAsync(temporaryItemId);
                listMock.VerifyPutItemInBasketOnce(storeItem.Id);
                local.ShoppingListRepositoryMock.VerifyStoreAsyncOnce(listMock.Object);
            }
        }
コード例 #3
0
        public async Task <bool> HandleAsync(RemoveItemFromShoppingListCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var list = await shoppingListRepository.FindByAsync(command.ShoppingListId, cancellationToken);

            if (list == null)
            {
                throw new DomainException(new ShoppingListNotFoundReason(command.ShoppingListId));
            }

            cancellationToken.ThrowIfCancellationRequested();

            IStoreItem item;

            if (command.OfflineTolerantItemId.IsActualId)
            {
                ItemId itemId = new ItemId(command.OfflineTolerantItemId.ActualId.Value);

                item = await itemRepository.FindByAsync(itemId, cancellationToken);

                if (item == null)
                {
                    throw new DomainException(new ItemNotFoundReason(itemId));
                }
            }
            else
            {
                TemporaryItemId itemId = new TemporaryItemId(command.OfflineTolerantItemId.OfflineId.Value);

                item = await itemRepository.FindByAsync(itemId, cancellationToken);

                if (item == null)
                {
                    throw new DomainException(new ItemNotFoundReason(itemId));
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

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

            list.RemoveItem(item.Id);
            if (item.IsTemporary)
            {
                item.Delete();
                await itemRepository.StoreAsync(item, cancellationToken);
            }

            cancellationToken.ThrowIfCancellationRequested();

            await shoppingListRepository.StoreAsync(list, cancellationToken);

            await transaction.CommitAsync(cancellationToken);

            return(true);
        }
コード例 #4
0
 public void VerifyFindByAsync(TemporaryItemId temporaryItemId)
 {
     mock.Verify(
         i => i.FindByAsync(
             It.Is <TemporaryItemId>(id => id == temporaryItemId),
             It.IsAny <CancellationToken>()),
         Times.Once);
 }
コード例 #5
0
 public void SetupFindByAsync(TemporaryItemId temporaryItemId, IStoreItem returnValue)
 {
     mock
     .Setup(i => i.FindByAsync(
                It.Is <TemporaryItemId>(id => id == temporaryItemId),
                It.IsAny <CancellationToken>()))
     .ReturnsAsync(returnValue);
 }
 public void VerifyAddItemToShoppingListOnce(IShoppingList shoppingList, TemporaryItemId temporaryItemId,
                                             SectionId sectionId, float quantity)
 {
     mock.Verify(i => i.AddItemToShoppingList(
                     shoppingList,
                     temporaryItemId,
                     sectionId,
                     quantity,
                     It.IsAny <CancellationToken>()));
 }
        private async Task <IStoreItem> LoadItem(TemporaryItemId temporaryItemId, CancellationToken cancellationToken)
        {
            IStoreItem item = await itemRepository.FindByAsync(temporaryItemId, cancellationToken);

            if (item == null)
            {
                throw new DomainException(new ItemNotFoundReason(temporaryItemId));
            }

            return(item);
        }
        public async Task AddItemToShoppingList(IShoppingList shoppingList, TemporaryItemId temporaryItemId,
                                                SectionId sectionId, float quantity, CancellationToken cancellationToken)
        {
            if (shoppingList is null)
            {
                throw new ArgumentNullException(nameof(shoppingList));
            }
            if (temporaryItemId is null)
            {
                throw new ArgumentNullException(nameof(temporaryItemId));
            }

            IStoreItem storeItem = await LoadItem(temporaryItemId, cancellationToken);

            await AddItemToShoppingList(shoppingList, storeItem, sectionId, quantity, cancellationToken);
        }
        public async Task <bool> HandleAsync(ChangeItemQuantityOnShoppingListCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var list = await shoppingListRepository.FindByAsync(command.ShoppingListId, cancellationToken);

            if (list == null)
            {
                throw new DomainException(new ShoppingListNotFoundReason(command.ShoppingListId));
            }

            ItemId itemId;

            if (command.OfflineTolerantItemId.IsActualId)
            {
                itemId = new ItemId(command.OfflineTolerantItemId.ActualId.Value);
            }
            else
            {
                var temporaryId = new TemporaryItemId(command.OfflineTolerantItemId.OfflineId.Value);
                var item        = await itemRepository.FindByAsync(temporaryId, cancellationToken);

                if (item == null)
                {
                    throw new DomainException(new ItemNotFoundReason(temporaryId));
                }

                itemId = item.Id;
            }

            list.ChangeItemQuantity(itemId, command.Quantity);

            cancellationToken.ThrowIfCancellationRequested();

            await shoppingListRepository.StoreAsync(list, cancellationToken);

            return(true);
        }
コード例 #10
0
        public async Task <bool> HandleAsync(RemoveItemFromBasketCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var list = await shoppingListRepository.FindByAsync(command.ShoppingListId, cancellationToken);

            if (list == null)
            {
                throw new DomainException(new ShoppingListNotFoundReason(command.ShoppingListId));
            }

            ItemId itemId;

            if (command.OfflineTolerantItemId.IsActualId)
            {
                itemId = new ItemId(command.OfflineTolerantItemId.ActualId.Value);
            }
            else
            {
                var        temporaryId = new TemporaryItemId(command.OfflineTolerantItemId.OfflineId.Value);
                IStoreItem item        = await itemRepository.FindByAsync(temporaryId, cancellationToken);

                if (item == null)
                {
                    throw new DomainException(new ItemNotFoundReason(temporaryId));
                }

                itemId = item.Id;
            }

            list.RemoveFromBasket(itemId);

            await shoppingListRepository.StoreAsync(list, cancellationToken);

            return(true);
        }
        public async Task HandleAsync_WithInvalidOfflineId_ShouldThrowDomainException()
        {
            // Arrange
            var local   = new LocalFixture();
            var handler = local.CreateCommandHandler();
            var command = local.CreateCommandWithOfflineId();

            var temporaryItemId       = new TemporaryItemId(command.OfflineTolerantItemId.OfflineId.Value);
            ShoppingListMock listMock = local.ShoppingListMockFixture.Create();

            local.ShoppingListRepositoryMock.SetupFindByAsync(command.ShoppingListId, listMock.Object);
            local.ItemRepositoryMock.SetupFindByAsync(temporaryItemId, null);

            // Act
            Func <Task> function = async() => await handler.HandleAsync(command, default);

            // Assert
            using (new AssertionScope())
            {
                (await function.Should().ThrowAsync <DomainException>())
                .Where(e => e.Reason.ErrorCode == ErrorReasonCode.ItemNotFound);
            }
        }
コード例 #12
0
        public async Task <IStoreItem> FindByAsync(TemporaryItemId temporaryItemId, CancellationToken cancellationToken)
        {
            if (temporaryItemId is null)
            {
                throw new ArgumentNullException(nameof(temporaryItemId));
            }

            cancellationToken.ThrowIfCancellationRequested();

            var itemEntity = await GetItemQuery()
                             .FirstOrDefaultAsync(item => item.CreatedFrom.HasValue && item.CreatedFrom == temporaryItemId.Value);

            cancellationToken.ThrowIfCancellationRequested();

            if (itemEntity == null)
            {
                return(null);
            }

            itemEntity.Predecessor = await LoadPredecessorsAsync(itemEntity);

            return(toModelConverter.ToDomain(itemEntity));
        }
コード例 #13
0
        public IStoreItem Create(ItemId id, string name, bool isDeleted, string comment, bool isTemporary,
                                 QuantityType quantityType, float quantityInPacket, QuantityTypeInPacket quantityTypeInPacket,
                                 ItemCategoryId itemCategoryId, ManufacturerId manufacturerId, IStoreItem predecessor,
                                 IEnumerable <IStoreItemAvailability> availabilities, TemporaryItemId temporaryId)
        {
            var item = new StoreItem(
                id,
                name,
                isDeleted,
                comment,
                isTemporary,
                quantityType,
                quantityInPacket,
                quantityTypeInPacket,
                itemCategoryId,
                manufacturerId,
                availabilities,
                temporaryId);

            item.SetPredecessor(predecessor);
            return(item);
        }
コード例 #14
0
 public ItemNotFoundReason(TemporaryItemId id)
 {
     Message = $"Item {id} not found.";
 }