Ejemplo n.º 1
0
        public async Task <IActionResult> GetPagging(string LanguageId, [FromQuery] GetCategoryPaggingReqest request)
        {
            var result = await _CategoryService.GetAll(request, LanguageId);

            if (result.IsSuccessed == false)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
Ejemplo n.º 2
0
        public async Task <ApiResult <List <CategoryViewModel> > > GetAll(GetCategoryPaggingReqest request, string languageId)
        {
            var query = from p in _context.Categories
                        join pt in _context.CategoryTranslations on p.Id equals pt.CategoryId
                        where pt.LanguageId == languageId
                        join l in _context.Languages on languageId equals l.Id
                        select new { p, pt, l };

            //filter
            if (!String.IsNullOrEmpty(request.Keywork))
            {
                query = query.Where(x => x.pt.Name.Contains(request.Keywork));
            }
            int totalRow = await query.CountAsync();

            //Pagging
            if (request.PageIndex == 0 || request.PageSize == 0)
            {
                var data = await query
                           .Select(x => new CategoryViewModel()
                {
                    Id           = x.p.Id,
                    Name         = x.pt.Name,
                    LanguageId   = x.pt.LanguageId,
                    CategoryUrl  = x.pt.CategoryUrl,
                    ImagePath    = x.p.ImagePath,
                    IsShowOnHome = x.p.IsShowOnHome,
                    Status       = x.p.Status,
                    Language     = x.l.Name
                }).ToListAsync();

                return(new ApiResultSuccess <List <CategoryViewModel> >(data));
            }
            else
            {
                var data = await query.Skip((request.PageIndex - 1) *request.PageSize).Take(request.PageSize)
                           .Select(x => new CategoryViewModel()
                {
                    Id           = x.p.Id,
                    Name         = x.pt.Name,
                    LanguageId   = x.pt.LanguageId,
                    CategoryUrl  = x.pt.CategoryUrl,
                    ImagePath    = x.p.ImagePath,
                    IsShowOnHome = x.p.IsShowOnHome,
                    Status       = x.p.Status,
                    Language     = x.l.Name
                }).ToListAsync();

                return(new ApiResultSuccess <List <CategoryViewModel> >(data));
            }
        }