Ejemplo n.º 1
0
        public async Task <PagedResultDto <ProductListDto> > GetProducts(GetProductsInput input)
        {
            var query = _productManager
                        .Products
                        .Include(p => p.Pictures)
                        .WhereIf(!input.Name.IsNullOrWhiteSpace(), r => r.Name.Contains(input.Name));

            var productCount = await query.CountAsync();

            var products = await query
                           .OrderBy(input.Sorting)
                           .PageBy(input)
                           .ToListAsync();

            var productListDtos = products.Select(p =>
            {
                var productDto = ObjectMapper.Map <ProductListDto>(p);

                if (p.Pictures != null && p.Pictures.Any())
                {
                    productDto.PictureUrl = _pictureManager.GetPictureUrl(p.Pictures.FirstOrDefault().PictureId);
                }

                return(productDto);
            }).ToList();

            return(new PagedResultDto <ProductListDto>(
                       productCount,
                       productListDtos));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 初始化子订单商品属性
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private async Task <List <ProductAttributeDto> > PrepareOrderItemAttribute(OrderItem item)
        {
            var attributes = new List <ProductAttributeDto>();

            if (item.AttributesJson.IsNullOrWhiteSpace())
            {
                return(attributes);
            }

            var jsonAttributeList = JsonConvert.DeserializeObject <List <JsonProductAttribute> >(item.AttributesJson);

            foreach (var jsonAttribute in jsonAttributeList)
            {
                var atributeDto = new ProductAttributeDto();

                var attribute = await _productAttributeManager.GetByIdAsync(jsonAttribute.AttributeId);

                atributeDto.Id     = jsonAttribute.AttributeId;
                atributeDto.Name   = attribute.Name;
                atributeDto.Values = jsonAttribute.AttributeValues.Select(value =>
                {
                    var valueDto = new ProductAttributeValueDto();

                    var attributeValue = _productAttributeManager.FindValueById(value.AttributeValueId);
                    if (attributeValue == null)
                    {
                        valueDto.Name = _productAttributeManager.GetPredefinedValueById(value.AttributeValueId).Name;
                    }
                    else
                    {
                        valueDto.Name = attributeValue.Name;
                    }

                    valueDto.Id         = attributeValue.Id;
                    valueDto.PictureUrl = _pictureManager.GetPictureUrl(attributeValue.PictureId);
                    return(valueDto);
                }).ToList();
                attributes.Add(atributeDto);
            }

            return(attributes);
        }