Example #1
0
        public async Task <IEnumerable <GoodsDTO> > GetGoods(string key, string category, string subCategory, int pageIndex, int pageSize, string storeId = "")
        {
            IEnumerable <GoodsDTO> goods = null;

            var result = _goodsRepository.GetFiltered(o => (o.StoreId == storeId || string.IsNullOrEmpty(storeId)));

            if (!string.IsNullOrEmpty(key))
            {
                result = result.Where(o => o.Title.Contains(key));
            }

            if (!string.IsNullOrEmpty(category))
            {
                result = result.Where(o => o.Category == category);
            }

            if (!string.IsNullOrEmpty(subCategory))
            {
                result = result.Where(o => o.SubCategory == subCategory);
            }

            goods = result.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(
                item => new GoodsDTO
            {
                Category    = item.Category,
                Description = item.Description,
                Detail      = item.Detail,
                Id          = item.Id,
                ItemNumber  = item.ItemNumber,
                MarketPrice = item.MarketPrice,
                OptionalPropertyJsonObject = item.OptionalPropertyJsonObject,
                Status            = item.Status,
                Stock             = item.Stock,
                StoreId           = item.StoreId,
                SubCategory       = item.SubCategory,
                Title             = item.Title,
                Unit              = item.Unit,
                UnitPrice         = item.UnitPrice,
                DistributionScope = item.DistributionScope,
                VideoPath         = item.VideoPath,
                GoodsCategoryName = item.GoodsCategoryName
            }).ToList();

            if (goods != null && goods.Count() > 0)
            {
                foreach (var item in goods)
                {
                    item.GoodsImages = _goodsImageRepository.GetFiltered(o => o.GoodsId == item.Id).Select(image => new GoodsImageDTO {
                        ImageId = image.ImageId, GoodsId = image.GoodsId, Id = image.Id
                    }).ToList();

                    var imageIds = item.GoodsImages.Select(o => o.ImageId).ToList();
                    var images   = await _imageServiceProxy.GetImagesByIds(imageIds);

                    foreach (var img in item.GoodsImages)
                    {
                        var pic = images.FirstOrDefault(o => o.Id == img.ImageId);

                        img.HttpPath    = pic?.HttpPath;
                        img.Title       = pic?.Title;
                        img.Description = pic?.Description;
                    }
                }
            }
            return(goods);
        }