public ActionResult <PagedResponse <JournalistDto> > Get([FromQuery] JournalistSearch journalistSearch)
 {
     try
     {
         var journalists = this.getJournalistsCommand.Execute(journalistSearch);
         return(Ok(journalists));
     }
     catch (Exception)
     {
         return(StatusCode(500));
     }
 }
        public PagedResponse <JournalistDto> Execute(JournalistSearch request)
        {
            var query = this.Context.Journalists
                        .Where(j => !j.IsDeleted)
                        .AsQueryable();

            if (request.FirstName != null)
            {
                query = query.Where(
                    j => j.FirstName.ToLower()
                    .Contains(request.FirstName.ToLower()));
            }

            if (request.IsActive != null)
            {
                query = query.Where(
                    j => j.IsActive == request.IsActive);
            }

            int totalCount = query.Count();

            // number of pages/buttons
            int numberOfPages = (int)Math.Ceiling((double)totalCount / request.PerPage);

            query = query.Skip(request.PageNumber * request.PerPage - request.PerPage).Take(request.PerPage);
            // second way .Skip((request.PageNumber - 1)* request.PerPage).Take(request.PerPage);

            var result = new PagedResponse <JournalistDto>
            {
                TotalNumber = totalCount,
                PagesNumber = numberOfPages,
                CurrentPage = request.PageNumber,
                Data        = query.Select(j => new JournalistDto
                {
                    Id        = j.Id,
                    FirstName = j.FirstName,
                    LastName  = j.LastName,
                    IsActive  = j.IsActive
                })
            };

            return(result);
        }
Example #3
0
        // GET: Journalists
        public IActionResult Index(JournalistSearch search)
        {
            var journalists = this.getJournalistsCommand.Execute(search);

            return(View(journalists));
        }