public StoreItemReadModel ToReadModel(IStoreItem model, IItemCategory itemCategory,
                                              IManufacturer manufacturer, Dictionary <StoreId, IStore> stores)
        {
            var availabilityReadModels = new List <StoreItemAvailabilityReadModel>();

            foreach (var av in model.Availabilities)
            {
                var store   = stores[av.StoreId];
                var section = store.Sections.First(s => s.Id == av.DefaultSectionId);

                availabilityReadModels.Add(av.ToReadModel(store, section));
            }

            return(new StoreItemReadModel(
                       model.Id,
                       model.Name,
                       model.IsDeleted,
                       model.Comment,
                       model.IsTemporary,
                       model.QuantityType.ToReadModel(),
                       model.QuantityInPacket,
                       model.QuantityTypeInPacket.ToReadModel(),
                       itemCategory?.ToReadModel(),
                       manufacturer?.ToReadModel(),
                       availabilityReadModels));
        }
        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());
            }));
        }