public async Task <List <BasketItemViewModel> > GetBasketItems(IReadOnlyCollection <BasketItem> basketItems) { var catalogItemsSpecification = new CatalogItemsSpecification(basketItems.Select(b => b.CatalogItemId).ToArray()); var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); var items = basketItems.Select(basketItem => { var catalogItem = catalogItems.FirstOrDefault(c => c.id == basketItem.CatalogItemId); //if basketItem.CatalogItemId == catalogItem.id make a basketViewModel out of both and return it var basketItemViewModel = new BasketItemViewModel { Id = basketItem.id, CatalogItemId = basketItem.CatalogItemId, ProductName = catalogItem.Name, Quantity = basketItem.Quantity, UnitPrice = basketItem.UnitPrice, PictureUri = _uriComposer.ComposePictureUri(catalogItem.PictureUri) }; return(basketItemViewModel); }).ToList(); return(items); }
public async Task <IEnumerable <CatalogItemViewModel> > Handle(GetCatalogItems request, CancellationToken cancellationToken) { var specification = new CatalogItemsSpecification(request.ItemsPage * request.PageIndex, request.ItemsPage); var catalogItems = await _catalogItemRepository.ListAsync(specification); return(catalogItems.Select(model => new CatalogItemViewModel { Id = model.Id, Name = model.Name, Price = model.Price, PictureUri = model.PictureUri })); }
public async Task CreateOrderAsync(int basketId, Address shippingAddress) { var basketSpec = new BasketWithItemsSpecification(basketId); var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec); Guard.Against.NullBasket(basketId, basket); var catalogItemsSpecification = new CatalogItemsSpecification(basket.Items.Select(item => item.CatalogItemId).ToArray()); var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); var items = basket.Items.Select(basketItem => { var catalogItem = catalogItems.First(c => c.Id == basketItem.CatalogItemId); var itemOrdered = new CatalogItemOrdered(catalogItem.Id, catalogItem.Name, _uriComposer.ComposePicUri(catalogItem.PictureUri)); var orderItem = new OrderItem(itemOrdered, basketItem.UnitPrice, basketItem.Quantity); return(orderItem); }).ToList(); var order = new Order(basket.BuyerId, shippingAddress, items); await _orderRepository.AddAsync(order); }
private async Task<List<BasketItemViewModel>> GetBasketItems(IReadOnlyCollection<BasketItem> basketItems) { var catalogItemsSpecification = new CatalogItemsSpecification(basketItems.Select(b => b.CatalogItemId).ToArray()); var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); var items = basketItems.Select(basketItem => { var catalogItem = catalogItems.First(c => c.Id == basketItem.CatalogItemId); var basketItemViewModel = new BasketItemViewModel { Id = basketItem.Id, UnitPrice = basketItem.UnitPrice, Quantity = basketItem.Quantity, CatalogItemId = basketItem.CatalogItemId, PictureUrl = _uriComposer.ComposePicUri(catalogItem.PictureUri), ProductName = catalogItem.Name }; return basketItemViewModel; }).ToList(); return items; }
public async Task CreateOrderAsync(int basketId, Address shippingAddress) { var basketSpec = new BasketWithItemsSpecification(basketId); var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec); Guard.Against.NullBasket(basket.id, basket); // make an array of catalogItem ids in all basketItem objects and send to CatalogItemsSpecification() var catalogItemsSpecification = new CatalogItemsSpecification(basket.Items.Select(item => item.CatalogItemId).ToArray()); var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); // list of catalogItems with ids in basket items var items = basket.Items.Select(basketItem => { // if basketItem's catalog item id matches repo catalogItem id set var var catalogItem = catalogItems.First(c => c.id == basketItem.CatalogItemId); var itemOrdered = new CatalogItemOrdered(catalogItem.id, catalogItem.Name, catalogItem.PictureUri); // make new orderItem by using selected basketItem and catalogItem var orderItem = new OrderItem(itemOrdered, catalogItem.Price, basketItem.Quantity); return(orderItem); // return the new orderItem and append to items list }).ToList(); var order = new Order(basket.BuyerId, shippingAddress, items); await _orderRepository.AddAsync(order); // add the new Order obj to orderRepository }