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)); }
public async Task <IEnumerable <ManufacturerReadModel> > HandleAsync(AllActiveManufacturersQuery query, CancellationToken cancellationToken) { var manufacturers = await manufacturerRepository.FindByAsync(false, cancellationToken); return(manufacturers.Select(m => m.ToReadModel())); }
public async Task <IEnumerable <ManufacturerReadModel> > HandleAsync(ManufacturerSearchQuery query, CancellationToken cancellationToken) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var manufacturerModels = await manufacturerRepository.FindByAsync(query.SearchInput, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); return(manufacturerModels.Select(model => model.ToReadModel())); }
public async Task ValidateAsync(ManufacturerId manufacturerId, CancellationToken cancellationToken) { if (manufacturerId is null) { throw new ArgumentNullException(nameof(manufacturerId)); } IManufacturer manufacturer = await manufacturerRepository .FindByAsync(manufacturerId, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); if (manufacturer == null) { throw new DomainException(new ManufacturerNotFoundReason(manufacturerId)); } }
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()); })); }
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)); }