示例#1
0
        public async Task <IActionResult> GetAll([FromQuery] ProductParamsDto productParamsDto)
        {
            var result = await _productService.GetProductsByIdBrandAndTypesAsync(productParamsDto);

            if (result.Success)
            {
                return(Ok(result.Data));
            }

            return(BadRequest(result.Message));
        }
示例#2
0
        public async Task <IList <ProductsDto> > GetProductsByIdBrandAndTypesAsync(ProductParamsDto productParamsDto, string filters)
        {
            using (var context = new StoreContext())
            {
                //string query = "WHERE 1 = 1 ";

                //Type type = productParamsDto.GetType();

                //foreach (var item in type.GetProperties())
                //{
                //    var value = item.GetValue(productParamsDto, null);

                //    if (value != null && item.Name != "Sort")
                //    {
                //        query += $"AND {item.Name}={item.GetValue(productParamsDto)}";
                //    }
                //    if(item.Name == "Sort")
                //    {
                //        var ordersType = item.GetValue(productParamsDto).ToString();
                //        switch (ordersType)
                //        {
                //            case "priceAsc":
                //                query += $"ORDER BY Products.Price ASC";
                //                break;
                //            case "priceDesc":
                //                query += $"ORDER BY Products.Price DESC";
                //                break;
                //            default:
                //                query += $"ORDER BY Products.Name ASC";
                //                break;
                //        }

                //    }

                //}

                var result = context.ProductsDtos.FromSqlRaw <ProductsDto>(
                    $"SELECT Products.Id,Products.Name,Description,Price,PictureUrl,ProductBrand = ProductBrands.Name,ProductType = ProductTypes.Name FROM dbo.Products WITH(NOLOCK) JOIN dbo.ProductBrands WITH(NOLOCK) ON ProductBrands.Id = Products.ProductBrandId JOIN dbo.ProductTypes WITH(NOLOCK) ON ProductTypes.Id = Products.ProductTypeId {filters}");
                return(await result.ToListAsync());
            }
        }
        public async Task <IDataResult <ProductPaginationDto> > GetProductsByIdBrandAndTypesAsync(ProductParamsDto productParamsDto)
        {
            string filters = "WHERE 1 = 1 ";

            Type type = productParamsDto.GetType();

            foreach (var item in type.GetProperties())
            {
                var value = item.GetValue(productParamsDto, null);

                if (value != null && (item.Name == "ProductBrandId" || item.Name == "ProductTypeId"))
                {
                    filters += $"AND {item.Name}={item.GetValue(productParamsDto)}";
                }
            }

            if (productParamsDto.Search != null)
            {
                filters += $"AND Products.Name LIKE '{productParamsDto.Search}%' ";
            }

            if (productParamsDto.Sort != null)
            {
                switch (productParamsDto.Sort)
                {
                case "priceAsc":
                    filters += $"ORDER BY Products.Price ASC";
                    break;

                case "priceDesc":
                    filters += $"ORDER BY Products.Price DESC";
                    break;

                default:
                    filters += $"ORDER BY Products.Name ASC";
                    break;
                }
            }


            var result = await _productDal.GetProductsByIdBrandAndTypesAsync(productParamsDto, filters);

            var data = result.Skip(productParamsDto.PageSize * (productParamsDto.PageIndex - 1)).Take(productParamsDto.PageSize).ToList();

            foreach (var item in data)
            {
                item.PictureUrl = item.PictureUrl.Insert(0, _apiUrl);
            }
            var productPaginationDto = new ProductPaginationDto
            {
                Count     = result.Count,
                Data      = data.ToList(),
                PageIndex = productParamsDto.PageIndex,
                PageSize  = productParamsDto.PageSize
            };

            return(new SuccessDataResult <ProductPaginationDto>(productPaginationDto));
        }