public async Task <PageResult <ProductViewModel> > GetAllByCategoryId(GetPublicProductPagingRequest request)
        {
            //1. Select join
            var query = from p in _context.Products
                        join pt in _context.ProductTranslations on p.Id equals pt.ProductId
                        join pic in _context.ProductInCategories on p.Id equals pic.ProductId
                        join c in _context.Categories on pic.CategoryId equals c.Id
                        select new { p, pt, pic };

            //2. Filter
            if (request.CategoryId.HasValue && request.CategoryId.Value > 0)
            {
                query = query.Where(p => p.pic.CategoryId == request.CategoryId);
            }

            //3. Paging
            int totalRow = await query.CountAsync();

            var data = await query.Skip((request.PageIndex - 1) *request.PageSize)
                       .Take(request.PageSize)
                       .Select(x => new ProductViewModel()
            {
                Id             = x.p.Id,
                Name           = x.pt.Name,
                DateCreated    = x.p.DateCreated,
                Description    = x.pt.Description,
                Details        = x.pt.Details,
                LanguageId     = x.pt.LanguageId,
                OriginalPrice  = x.p.OriginalPrice,
                Price          = x.p.Price,
                SeoAlias       = x.pt.SeoAlias,
                SeoDescription = x.pt.SeoDescription,
                SeoTitle       = x.pt.SeoTitle,
                Stock          = x.p.Stock,
                ViewCount      = x.p.ViewCount
            }).ToListAsync();

            //4. Select and project
            var pageResult = new PageResult <ProductViewModel>()
            {
                Items       = data,
                TotalRecord = totalRow
            };

            return(pageResult);
        }
Exemple #2
0
 public Task <PagedResult <ProductViewModel> > GetAllPaging(GetPublicProductPagingRequest request)
 {
     throw new NotImplementedException();
 }
 PageResult <ProductViewModel> IPublicProductService.GetAllByCategoryId(GetPublicProductPagingRequest request)
 {
     throw new NotImplementedException();
 }
Exemple #4
0
        public async Task <PagedResult <ProductViewModel> > GetAllByCategoryId(string languageId, GetPublicProductPagingRequest request)
        {
            //1. Query
            var query = from p in _context.Products
                        join pt in _context.ProductTranslations on p.Id equals pt.ProductId
                        join pic in _context.ProductInCategories on p.Id equals pic.ProductId
                        join c in _context.Catergories on pic.CategoryId equals c.Id
                        where pt.LanguageId == languageId
                        select new { p, pt, pic };

            //2. Filter
            if (request.CategoryId.HasValue && request.CategoryId.Value > 0)
            {
                query = query.Where(x => x.pic.CategoryId == request.CategoryId);
            }
            //3. Paging
            int totalRow = await query.CountAsync();

            //VD muon lay trang 2 pageIndex = 2 thì Skip (2-1)*10 = 10 records và Take 10 record tiếp theo
            var data = await query.Skip((request.PageIndex - 1) *request.PageSize)   //ToListAsync thi phai sua lai await
                       .Take(request.PageSize)
                       .Select(x => new ProductViewModel()
            {
                Id             = x.p.Id,
                Name           = x.pt.Name,
                DateCreated    = x.p.DateCreated,
                Description    = x.pt.Description,
                Details        = x.pt.Details,
                LanguageId     = x.pt.LanguageId,
                OriginalPrice  = x.p.OriginalPrice,
                Price          = x.p.Price,
                SeoAlias       = x.pt.SeoAlias,
                SeoDescription = x.pt.SeoDescription,
                SeoTitle       = x.pt.SeoTitle,
                Stock          = x.p.Stock,
                ViewCount      = x.p.ViewCount
            }).ToListAsync();

            var pageResult = new PagedResult <ProductViewModel>()
            {
                TotalRecords = totalRow,
                PageSize     = request.PageSize,
                PageIndex    = request.PageIndex,
                Items        = data
            };

            return(pageResult);
        }
Exemple #5
0
        public async Task <PagedResult <ProductVm> > GetAllByCategoryId(string languageId, GetPublicProductPagingRequest request) // lấy của public nhe
        {
            // bước 1 :select join
            var query = from p in _context.Products
                        join pt in _context.ProductTranslations
                        on p.Id equals pt.ProductId
                        join pic in _context.ProductInCategories on p.Id equals pic.ProductId

                        join c in _context.Categories on pic.CategoryId equals c.Id
                        where pt.LanguageId == languageId
                        select new { p, pt, pic };

            // bước 1: filter theo điều kiện

            if (request.CategoryId.HasValue && request.CategoryId.Value > 0)// HasValue mặc đinh là true
            {
                //using System.linq
                query = query.Where(p => p.pic.CategoryId == request.CategoryId);//một trong só nhữ thằng này thì mới được
            }
            // bước 3: paging
            int totalRow = await query.CountAsync(); // lấy ra tông số số dòng để phân trang   phải include using Microsoft.EntityFrameworkCore;

            var data = await query.Skip((request.PageIndex - 1) *request.PageSize)
                       .Take(request.PageSize)// nếu PageIndex=2 và PageSize=20 thì bỏ qua 10 chỉ lấy 10 bẩn ghi hiện lên ko lấy tất để phù hợp với PageSize
                       .Select(x => new ProductVm()
            {
                Id             = x.p.Id,
                Name           = x.pt.Name,
                DateCreated    = x.p.DateCreated,
                Description    = x.pt.Description,
                Details        = x.pt.Details,
                LanguageId     = x.pt.LanguageId,
                OriginalPrice  = x.p.OriginalPrice,
                Price          = x.p.Price,
                SeoAlias       = x.pt.SeoAlias,
                SeoDescription = x.pt.SeoDescription,
                SeoTitle       = x.pt.SeoTitle,
                Stock          = x.p.Stock,
                ViewCount      = x.p.ViewCount,
            }).ToListAsync();     // vì ta Async ở đây nên trên kia ta chỉ cần await để đẩy vào data là song  nhớ là ToListAsync nha vì bên PageRsult Item ta để là list

            // bước 4: selecet and Project
            var pagedResult = new PagedResult <ProductVm>()
            {
                TotalRecords = totalRow,
                PageSize     = request.PageSize,
                PageIndex    = request.PageIndex,
                Items        = data
            };

            return(pagedResult);
        }