示例#1
0
        public async Task <ApiResult <PagedResult <ProductCardDto> > > GetProductsByCategoryPagingAsync(string categoryId, int pageIndex, int pageSize)
        {
            var products = await(from p in _context.Products
                                 join c in _context.Categories on p.CategoryId equals c.Id
                                 join brand in _context.Brands on p.BrandId equals brand.Id
                                 into BrandGroup
                                 from b in BrandGroup.DefaultIfEmpty()
                                 join productImage in _context.ProductImages on p.Id equals productImage.ProductId
                                 into ProductImageGroup
                                 from pi in ProductImageGroup.DefaultIfEmpty()
                                 orderby p.Id
                                 where c.Id.Contains(categoryId == "all"?"":categoryId) && pi.IsThumbnail == true
                                 select new ProductCardDto()
            {
                Id           = p.Id,
                Name         = p.Name,
                Stars        = p.Stars,
                Views        = p.Views,
                BrandName    = b.Name,
                CategoryName = c.Name,
                ImagePath    = pi.Path,
                RetailPrice  = p.RetailPrice
            }).GetPagedAsync(pageIndex, pageSize);

            foreach (var item in products.Results)
            {
                var result = await GetProductPrice(item.Id);

                item.PromotionRetailPrice = result.Item1;
                item.DiscountValue        = result.Item2;
            }
            return(new ApiResult <PagedResult <ProductCardDto> >(HttpStatusCode.OK, products));
        }
示例#2
0
        public async Task <ApiResult <ProductDto> > GetProductAsync(string productId)
        {
            var product = await(from p in _context.Products
                                join b in _context.Brands on p.BrandId equals b.Id
                                join c in _context.Categories on p.CategoryId equals c.Id
                                join productimages in _context.ProductImages on p.Id equals productimages.ProductId
                                into ProductImageGroup
                                from pi in ProductImageGroup.DefaultIfEmpty()
                                where p.Id == productId && p.IsDelete == false && pi.IsThumbnail == true
                                select new ProductDto()
            {
                Id              = p.Id,
                Description     = p.Description,
                OverView        = p.OverView,
                Name            = p.Name,
                Views           = p.Views,
                BrandName       = b.Name,
                CategoryName    = c.Name,
                RetailPrice     = p.RetailPrice,
                Stars           = p.Stars,
                WarrantyPeriod  = p.WarrantyPeriod,
                WholesalePrices = p.WholesalePrices,
                Path            = pi.Path,
                AbleToSale      = (from s in _context.Stocks
                                   where s.ProductId == p.Id && s.WarehouseId == GlobalProperties.MainWarehouseId
                                   select s.AbleToSale).SingleOrDefault(),
                ListImage = (from pi in _context.ProductImages where pi.ProductId == p.Id
                             select new ProductImagesDto()
                {
                    Id = pi.Id,
                    ImagePath = pi.Path,
                    IsThumbnail = pi.IsThumbnail
                })
            }).SingleOrDefaultAsync();

            if (product != null)
            {
                var promotionDetail = await(from p in _context.Promotions
                                            join pd in _context.PromotionDetails on p.Id equals pd.PromotionId
                                            where p.FromDate <= DateTime.Now && p.ToDate >= DateTime.Now && pd.ProductId == product.Id
                                            select pd).SingleOrDefaultAsync();
                if (promotionDetail != null)
                {
                    product.PromotionPrice = promotionDetail.DiscountType == "%"
                        ? product.RetailPrice - (product.RetailPrice * promotionDetail.DiscountValue / 100)
                        : product.RetailPrice - promotionDetail.DiscountValue;
                }
                else
                {
                    product.PromotionPrice = product.RetailPrice;
                }
            }
            if (product == null)
            {
                return(new ApiResult <ProductDto>(HttpStatusCode.NotFound, $"Không tìm thấy sản phẩm có mã: {productId}"));
            }
            return(new ApiResult <ProductDto>(HttpStatusCode.OK, product));
        }
示例#3
0
        public async Task <ApiResult <PagedResult <ProductCardDto> > > FilterProductsByCategoryAsync(string categoryId, string brandId, bool order, decimal minimumPrice,
                                                                                                     decimal maximumPrice, int pageNumber, int pageSize)
        {
            if (minimumPrice > maximumPrice)
            {
                return(new ApiResult <PagedResult <ProductCardDto> >
                           (HttpStatusCode.BadRequest, "Giá tối thiểu không được lớn hơn giá tối đa."));
            }
            var products = await(from p in _context.Products
                                 join brand in _context.Brands on p.BrandId equals brand.Id
                                 into BrandGroup
                                 from b in BrandGroup.DefaultIfEmpty()
                                 join category in _context.Categories on p.CategoryId equals category.Id
                                 into CategoryGroup
                                 from c in CategoryGroup.DefaultIfEmpty()
                                 join productImage in _context.ProductImages on p.Id equals productImage.ProductId
                                 into ProductImageGroup
                                 from pi in ProductImageGroup.DefaultIfEmpty()
                                 where p.CategoryId.ToLower().Contains(!string.IsNullOrEmpty(categoryId) ? categoryId : "") &&
                                 p.RetailPrice >= minimumPrice && p.RetailPrice <= maximumPrice && b.Id.ToLower()
                                 .Contains(!string.IsNullOrEmpty(brandId) ? brandId : "") &&
                                 pi.IsThumbnail == true
                                 select new ProductCardDto()
            {
                Id           = p.Id,
                Name         = p.Name,
                Stars        = p.Stars,
                Views        = p.Views,
                BrandName    = b.Name,
                CategoryName = c.Name,
                ImagePath    = pi.Path,
                RetailPrice  = p.RetailPrice,
                AbleToSale   = _context.Stocks.Where(x => x.ProductId == p.Id &&
                                                     x.WarehouseId == GlobalProperties.MainWarehouseId).SingleOrDefault().AbleToSale
            }
                                 ).GetPagedAsync(pageNumber, pageSize);

            products.Results = order ? products.Results.OrderBy(x => x.RetailPrice).ToList() :
                               products.Results.OrderByDescending(x => x.RetailPrice).ToList();
            foreach (var item in products.Results)
            {
                var result = await GetProductPrice(item.Id);

                item.PromotionRetailPrice = result.Item1;
                item.DiscountValue        = result.Item2;
            }
            return(new ApiResult <PagedResult <ProductCardDto> >(HttpStatusCode.OK, products));
        }
示例#4
0
        public async Task <ApiResult <PagedResult <ProductCardDto> > > SearchProductsByCategory(string categoryId, string searchValue,
                                                                                                int pageNumber, int pageSize)
        {
            var products = await(from p in _context.Products
                                 join category in _context.Categories on p.CategoryId equals category.Id
                                 into CategoryGroup
                                 from c in CategoryGroup.DefaultIfEmpty()
                                 join brand in _context.Brands on p.BrandId equals brand.Id
                                 into BrandGroup
                                 from b in BrandGroup.DefaultIfEmpty()
                                 join productImage in _context.ProductImages on p.Id equals productImage.ProductId
                                 into ProductImageGroup
                                 from pi in ProductImageGroup.DefaultIfEmpty()
                                 where p.CategoryId.Contains(!string.IsNullOrEmpty(categoryId)? categoryId:"") &&
                                 p.IsDelete == false && pi.IsThumbnail == true && p.Name.ToLower().Contains(
                                     !string.IsNullOrEmpty(searchValue)? searchValue.ToLower():"")
                                 select new ProductCardDto()
            {
                Id           = p.Id,
                Name         = p.Name,
                Stars        = p.Stars,
                Views        = p.Views,
                BrandName    = b.Name,
                CategoryName = c.Name,
                ImagePath    = pi.Path,
                RetailPrice  = p.RetailPrice,
                AbleToSale   = _context.Stocks.Where(x => x.ProductId == p.Id &&
                                                     x.WarehouseId == GlobalProperties.MainWarehouseId).SingleOrDefault().AbleToSale
            }).GetPagedAsync(pageNumber, pageSize);

            foreach (var item in products.Results)
            {
                var result = await GetProductPrice(item.Id);

                item.PromotionRetailPrice = result.Item1;
                item.DiscountValue        = result.Item2;
            }
            return(new ApiResult <PagedResult <ProductCardDto> >(HttpStatusCode.OK, products));
        }