public async Task <IActionResult> ListPgms([FromQuery] PgmsQuery request) { try { var result = await _service.GetPgms(request); return(Ok(result)); } catch (BusinessLogicException ex) { return(BadRequest(new Response { Status = false, Message = ex.Message })); } catch (Exception e) { return(BadRequest(new Response { Status = false, Message = ErrorMessages.UnkownError })); } }
public async Task <Response <PgmsDto> > GetPgms(PgmsQuery request) { var result = _context.PGMs.Include(x => x.Department).AsQueryable(); if (!string.IsNullOrEmpty(request.Name)) { result = result.Where(x => x.Name.Contains(request.Name)); } if (!string.IsNullOrEmpty(request.DepartmentName)) { result = result.Where(x => x.Department.Name.Contains(request.DepartmentName)); } ///pagenating int take = request.PageSize; int skip = (request.PageId - 1) * take; int totalPages = (int)Math.Ceiling(result.Count() / (double)take); var finalResult = result.OrderBy(x => x.Name).Skip(skip).Take(take).AsQueryable(); //---------------- //var resultDepartment=await var resultData = new PgmsDto { Dtos = await finalResult.Select(d => new PgmDto() { Id = d.Id, Name = d.Name, Department = new DepartmentDto { Id = d.Department.Id, Name = d.Department.Name } }).ToListAsync(), PageId = request.PageId, PageSize = request.PageSize, Total = await result.CountAsync() }; return(new Response <PgmsDto> { Data = resultData, Status = true, Message = "success" }); }