public async Task <PagedResultOutput <LeaveRequestDo> > GetLeaveRequests(GetLeaveRequestsInput input) { try { //if the request can view all leave request: get employeeId if employeenumber is set long employeeId = 0; if (IsGranted(AppPermissions.Pages_Leave_LeaveRequest_ViewAll)) { if (!string.IsNullOrEmpty(input.EmployeeNumber)) { var employee = _employeeRepository.GetEmployeeByNumber(input.EmployeeNumber); if (employee == null) { return(null); } employeeId = employee.Id; } } else { employeeId = GetCurrentUserId(); } IEnumerable <LeaveRequestDo> queryable; if (employeeId == 0) { if (input.StartDate == null) { queryable = input.Status == null ? _leaveRequestRepository.GetRequestDos() : _leaveRequestRepository.GetRequestDos(l => l.Status == input.Status.Value); } else { queryable = input.Status == null ? _leaveRequestRepository.GetRequestDos( l => l.StartDate >= input.StartDate.Value && l.EndDate <= input.EndDate.Value) : _leaveRequestRepository.GetRequestDos(l => l.Status == input.Status.Value && l.StartDate >= input.StartDate.Value && l.EndDate <= input.EndDate.Value); } } else { if (input.StartDate == null) { queryable = input.Status == null ? _leaveRequestRepository.GetRequestDos(l => l.EmployeeId == employeeId) : _leaveRequestRepository.GetRequestDos(l => l.EmployeeId == employeeId && l.Status == input.Status.Value); } else { queryable = input.Status == null ? _leaveRequestRepository.GetRequestDos( l => l.EmployeeId == employeeId && l.StartDate >= input.StartDate.Value && l.EndDate <= input.EndDate.Value) : _leaveRequestRepository.GetRequestDos(l => l.EmployeeId == employeeId && l.Status == input.Status.Value && l.StartDate >= input.StartDate.Value && l.EndDate <= input.EndDate.Value); } } var query = queryable.AsQueryable(); var requestCount = await query.CountAsync(); var requestList = await query .OrderBy(input.Sorting) .PageBy(input) .ToListAsync(); return(new PagedResultOutput <LeaveRequestDo>( requestCount, requestList )); } catch (Exception exception) { return(new PagedResultOutput <LeaveRequestDo>()); } }