예제 #1
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));
        }
예제 #2
0
        public async Task <ApiResult <PromotionDto> > GetPromotionDetail(string promotionId)
        {
            var checkPromotion = await _context.Promotions.Where(x => x.Id == promotionId).SingleOrDefaultAsync();

            if (checkPromotion == null)
            {
                return(new ApiResult <PromotionDto>(HttpStatusCode.NotFound, $"Không tìm thấy chương trình khuyến mãi có mã: {promotionId}"));
            }
            var promotion = await(from p in _context.Promotions
                                  join category in _context.Categories on p.CategoryId equals category.Id
                                  into CategoryGroup
                                  from c in CategoryGroup.DefaultIfEmpty()
                                  where p.Id == promotionId
                                  select new PromotionDto()
            {
                Id            = p.Id,
                Description   = p.Description,
                Name          = p.Name,
                CategoryId    = p.CategoryId,
                CategoryName  = c.Name,
                DiscountType  = p.DiscountType,
                DiscountValue = p.DiscountValue,
                FromDate      = p.FromDate,
                ToDate        = p.ToDate,
                Products      = (from pd in _context.PromotionDetails
                                 join pr in _context.Products on pd.ProductId equals pr.Id
                                 where pd.PromotionId == p.Id
                                 select new PromotionDetailDto()
                {
                    Id = pd.Id,
                    DiscountType = pd.DiscountType,
                    DiscountValue = pd.DiscountValue,
                    ProductId = pr.Id,
                    ProductName = pr.Name,
                    PromotionId = p.Id,
                    UnitPrice = pd.DiscountType == "%"
                                ? pr.RetailPrice - (pr.RetailPrice * pd.DiscountValue / 100)
                                : pr.RetailPrice - pd.DiscountValue
                }).ToList()
            }).SingleOrDefaultAsync();

            return(new ApiResult <PromotionDto>(HttpStatusCode.OK, promotion));
        }
예제 #3
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));
        }
예제 #4
0
        public async Task <ApiResult <PromotionDto> > GetClosetPromotion()
        {
            var promotion = await(from p in _context.Promotions
                                  join category in _context.Categories on p.CategoryId equals category.Id
                                  into CategoryGroup
                                  from c in CategoryGroup.DefaultIfEmpty()
                                  orderby p.FromDate ascending
                                  where (p.FromDate <= DateTime.Now && p.ToDate >= DateTime.Now) || p.FromDate >= DateTime.Now
                                  select new PromotionDto()
            {
                Description   = p.Description,
                Id            = p.Id,
                Name          = p.Name,
                CategoryId    = p.CategoryId,
                CategoryName  = c.Name,
                DiscountType  = p.DiscountType,
                DiscountValue = p.DiscountValue,
                FromDate      = p.FromDate,
                ToDate        = p.ToDate,
                Products      = (from pd in _context.PromotionDetails
                                 join pr in _context.Products on pd.ProductId equals pr.Id
                                 where pd.PromotionId == p.Id
                                 select new PromotionDetailDto()
                {
                    Id = pd.Id,
                    DiscountType = pd.DiscountType,
                    DiscountValue = pd.DiscountValue,
                    ProductId = pd.ProductId,
                    ProductName = pr.Name,
                    PromotionId = p.Id,
                    UnitPrice = pd.DiscountType == "%"?pr.RetailPrice - (pr.RetailPrice * pd.DiscountValue / 100):pr.RetailPrice - pd.DiscountValue
                })
            }).SingleOrDefaultAsync();

            return(new ApiResult <PromotionDto>(HttpStatusCode.OK, promotion ?? new PromotionDto()));
        }