示例#1
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);
        }
        public async Task <bool> HandleAsync(CreateTemporaryItemCommand command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var availability = command.TemporaryItemCreation.Availability;
            await availabilityValidationService.ValidateAsync(availability.ToMonoList(), cancellationToken);

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

            await itemRepository.StoreAsync(storeItem, cancellationToken);

            return(true);
        }
示例#4
0
        public IStoreItem ToDomain(Item source)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            IStoreItem predecessor = null;

            if (source.PredecessorId != null)
            {
                var converter = new StoreItemConverter(storeItemFactory, storeItemAvailabilityConverter);
                predecessor = converter.ToDomain(source.Predecessor);
            }

            List <IStoreItemAvailability> availabilities = storeItemAvailabilityConverter.ToDomain(source.AvailableAt)
                                                           .ToList();
            var itemCategoryId = source.ItemCategoryId.HasValue ? new ItemCategoryId(source.ItemCategoryId.Value) : null;
            var manufacturerId = source.ManufacturerId.HasValue ? new ManufacturerId(source.ManufacturerId.Value) : null;
            var temporaryId    = source.CreatedFrom.HasValue ? new TemporaryItemId(source.CreatedFrom.Value) : null;

            return(storeItemFactory.Create(
                       new ItemId(source.Id),
                       source.Name,
                       source.Deleted,
                       source.Comment,
                       source.IsTemporary,
                       source.QuantityType.ToEnum <QuantityType>(),
                       source.QuantityInPacket,
                       source.QuantityTypeInPacket.ToEnum <QuantityTypeInPacket>(),
                       itemCategoryId,
                       manufacturerId,
                       predecessor,
                       availabilities,
                       temporaryId));
        }