public ApiListResult <ProductAttributeValueDTO> Get([FromUri] AntPageOption option = null)
        {
            var query = ProductAttributeValueService.GetAll()
                        .Where(x => !x.Deleted && x.ProductAttributeMappingId == option.Id)
                        .ProjectTo <ProductAttributeValueDTO>();

            if (option != null)
            {
                if (!string.IsNullOrEmpty(option.SortField))
                {
                    //for example
                    if (option.SortField == "id")
                    {
                        if (option.SortOrder == PageSortTyoe.DESC)
                        {
                            query = query.OrderByDescending(x => x.Id);
                        }
                        else
                        {
                            query = query.OrderBy(x => x.Id);
                        }
                    }
                }

                if (option.Page > 0 && option.Results > 0)
                {
                    if (string.IsNullOrEmpty(option.SortField))
                    {
                        query = query.OrderBy(x => x.Id);
                    }
                }
            }
            else
            {
                query = query.OrderBy(x => x.Id);
            }
            var count  = query.Count();
            var result = query.Paging <ProductAttributeValueDTO>(option.Page - 1, option.Results, count);

            return(new ApiListResult <ProductAttributeValueDTO>(result, result.PageIndex, result.PageSize, count));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> Detail(int id)
        {
            var result  = new ApiResult <ProductDetailModel>();
            var product = await ProductService.GetAll()
                          .Where(x => x.Id == id)
                          .FirstOrDefaultAsync();

            if (product == null || product.Deleted)
            {
                return(NotFound());
            }

            var model = new ProductDetailModel
            {
                Id            = product.Id,
                OwnerId       = product.CreateUserId,
                Name          = product.Name,
                CategoryId    = product.CategoryId,
                Description   = product.Description,
                DetailUrl     = product.DetailUrl,
                ImageUrl      = product.ImageUrl,
                isAgreeActive = product.isAgreeActive,
                Price         = product.Price,
                SKU           = product.SKU,
                Status        = product.Status,
                VipPrice      = product.VipPrice,
                UrgencyPrice  = product.UrgencyPrice
            };

            #region attribute
            //performance optimization
            //We cache a value indicating whether a product has attributes
            IList <ProductAttributeMapping> productAttributeMapping = null;

            //no value in the cache yet
            //let's load attributes and cache the result (true/false)
            productAttributeMapping = ProductAttributeMappingService.GetProductAttributeMappingsByProductId(product.Id);

            if (productAttributeMapping == null)
            {
                productAttributeMapping = new List <ProductAttributeMapping>();
            }
            foreach (var attribute in productAttributeMapping)
            {
                var attributeModel = new ProductAttributeModel
                {
                    Id                 = attribute.Id,
                    ProductId          = product.Id,
                    ProductAttributeId = attribute.ProductAttributeId,
                    Name               = attribute.ProductAttribute.Name,
                    Description        = attribute.ProductAttribute.Description
                };

                //values
                var attributeValues = ProductAttributeValueService.GetAll().Where(x => x.ProductAttributeMappingId == attribute.Id && !x.Deleted);
                foreach (var attributeValue in attributeValues)
                {
                    var valueModel = new ProductAttributeValueModel
                    {
                        Id              = attributeValue.Id,
                        Name            = attributeValue.Name,
                        PriceAdjustment = attributeValue.PriceAdjustment,
                        ImageUrl        = attributeValue.ImageUrl
                    };
                    attributeModel.Values.Add(valueModel);
                }
                model.ProductAttributes.Add(attributeModel);
            }

            #endregion

            #region specification
            var specs = from psa in ProductSpecificationAttributeService.GetAll()
                        join sao in SpecificationAttributeOptionService.GetAll()
                        on psa.SpecificationAttributeOptionId equals sao.Id
                        join sa in SpecificationAttributeService.GetAll()
                        on sao.SpecificationAttributeId equals sa.Id
                        select new
            {
                sa,
                psa,
                sao
            };

            model.ProductSpecifications = specs.Where(x => x.psa.ProductId == id).ToList().Select(x => new ProductSpecificationModel()
            {
                SpecificationAttributeId   = x.sa.Id,
                SpecificationAttributeName = x.sa.Name,
                Value = x.sao.Name
            }).ToList();

            #endregion


            #region Storage Quatity

            var storageQuatities = from psq in ProductStorageQuantityService.GetAll()
                                   join ss in StorageService.GetAll()
                                   on psq.StorageId equals ss.Id
                                   select new
            {
                psq,
                ss,
            };

            model.ProductStorages = storageQuatities.Where(x => x.psq.ProductId == id).ToList().Select(x => new ProductStorageModel()
            {
                Name      = x.ss.Name,
                ProductId = x.psq.ProductId,
                StorageId = x.ss.Id,
                Quantity  = x.psq.Quantity,
                Id        = x.psq.Id
            }).ToList();

            #endregion
            result.Data = model;

            return(Ok(result));
        }