public async Task <StoreItemReadModel> ConvertAsync(IStoreItem item, CancellationToken cancellationToken)
        {
            if (item is null)
            {
                throw new System.ArgumentNullException(nameof(item));
            }

            IItemCategory itemCategory = null;
            IManufacturer manufacturer = null;

            if (item.ItemCategoryId != null)
            {
                itemCategory = await itemCategoryRepository.FindByAsync(item.ItemCategoryId, cancellationToken);

                if (itemCategory == null)
                {
                    throw new DomainException(new ItemCategoryNotFoundReason(item.ItemCategoryId));
                }
            }
            if (item.ManufacturerId != null)
            {
                manufacturer = await manufacturerRepository.FindByAsync(item.ManufacturerId, cancellationToken);

                if (manufacturer == null)
                {
                    throw new DomainException(new ManufacturerNotFoundReason(item.ManufacturerId));
                }
            }

            var storeIds  = item.Availabilities.Select(av => av.StoreId);
            var storeDict = (await storeRepository.FindByAsync(storeIds, cancellationToken))
                            .ToDictionary(store => store.Id);

            return(ToReadModel(item, itemCategory, manufacturer, storeDict));
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <ItemCategoryReadModel> > HandleAsync(ItemCategorySearchQuery query,
                                                                             CancellationToken cancellationToken)
        {
            var itemCategoryModels = await itemCategoryRepository.FindByAsync(query.SearchInput,
                                                                              cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            return(itemCategoryModels.Select(model => model.ToReadModel()));
        }
Ejemplo n.º 3
0
        public async Task ValidateAsync(ItemCategoryId itemCategoryId, CancellationToken cancellationToken)
        {
            if (itemCategoryId is null)
            {
                throw new ArgumentNullException(nameof(itemCategoryId));
            }

            IItemCategory itemCategory = await itemCategoryRepository
                                         .FindByAsync(itemCategoryId, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            if (itemCategory == null)
            {
                throw new DomainException(new ItemCategoryNotFoundReason(itemCategoryId));
            }
        }
        public async Task <bool> HandleAsync(DeleteItemCategoryCommand command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new System.ArgumentNullException(nameof(command));
            }

            var category = await itemCategoryRepository.FindByAsync(command.ItemCategoryId, cancellationToken);

            if (category == null)
            {
                throw new DomainException(new ItemCategoryNotFoundReason(command.ItemCategoryId));
            }

            category.Delete();

            var storeItems = (await itemRepository.FindActiveByAsync(command.ItemCategoryId, cancellationToken))
                             .ToList();

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

            foreach (var item in storeItems)
            {
                var lists = await shoppingListRepository.FindActiveByAsync(item.Id, cancellationToken);

                foreach (var list in lists)
                {
                    list.RemoveItem(item.Id);
                    await shoppingListRepository.StoreAsync(list, cancellationToken);
                }
                item.Delete();
                await itemRepository.StoreAsync(item, cancellationToken);
            }
            await itemCategoryRepository.StoreAsync(category, cancellationToken);

            await transaction.CommitAsync(cancellationToken);

            return(true);
        }
        public async Task <IEnumerable <ItemSearchReadModel> > ConvertAsync(IEnumerable <IStoreItem> items,
                                                                            IStore store, CancellationToken cancellationToken)
        {
            var itemCategoryIds = items
                                  .Where(i => i.ItemCategoryId != null)
                                  .Select(i => i.ItemCategoryId)
                                  .Distinct();
            var itemCategoryDict = (await itemCategoryRepository.FindByAsync(itemCategoryIds, cancellationToken))
                                   .ToDictionary(i => i.Id);

            var manufacturerIds = items
                                  .Where(i => i.ManufacturerId != null)
                                  .Select(i => i.ManufacturerId)
                                  .Distinct();
            var manufaturerDict = (await manufacturerRepository.FindByAsync(manufacturerIds, cancellationToken))
                                  .ToDictionary(m => m.Id);

            return(items
                   .Select(item =>
            {
                IManufacturer manufacturer = item.ManufacturerId == null ? null : manufaturerDict[item.ManufacturerId];
                IItemCategory itemCategory = item.ItemCategoryId == null ? null : itemCategoryDict[item.ItemCategoryId];

                IStoreItemAvailability storeAvailability = item.Availabilities
                                                           .Single(av => av.StoreId == store.Id);

                var section = store.Sections.Single(s => s.Id == storeAvailability.DefaultSectionId);

                return new ItemSearchReadModel(
                    item.Id,
                    item.Name,
                    item.QuantityType.GetAttribute <DefaultQuantityAttribute>().DefaultQuantity,
                    storeAvailability.Price,
                    manufacturer?.ToReadModel(),
                    itemCategory?.ToReadModel(),
                    section.ToReadModel());
            }));
        }
Ejemplo n.º 6
0
        public async Task <ShoppingListReadModel> ConvertAsync(IShoppingList shoppingList, CancellationToken cancellationToken)
        {
            if (shoppingList is null)
            {
                throw new System.ArgumentNullException(nameof(shoppingList));
            }

            var ItemIds   = shoppingList.Items.Select(i => i.Id);
            var itemsDict = (await itemRepository.FindByAsync(ItemIds, cancellationToken))
                            .ToDictionary(i => i.Id);

            var itemCategoryIds    = itemsDict.Values.Where(i => i.ItemCategoryId != null).Select(i => i.ItemCategoryId);
            var itemCategoriesDict = (await itemCategoryRepository.FindByAsync(itemCategoryIds, cancellationToken))
                                     .ToDictionary(cat => cat.Id);

            var manufacturerIds   = itemsDict.Values.Where(i => i.ManufacturerId != null).Select(i => i.ManufacturerId);
            var manufacturersDict = (await manufacturerRepository.FindByAsync(manufacturerIds, cancellationToken))
                                    .ToDictionary(man => man.Id);

            IStore store = await storeRepository.FindByAsync(shoppingList.StoreId, cancellationToken);

            return(ToReadModel(shoppingList, store, itemsDict, itemCategoriesDict, manufacturersDict));
        }