public async Task<ApiJsonResult> GetAllJobs(GetAllJobsParams getAllJobsParams) { try { Tuple<List<Job>, int, int> result = await new JobManager(GetCurrentUserId()).GetAllJobs(getAllJobsParams); return new ApiJsonPagingResult { Success = true, Data = result.Item1, TotalPages = result.Item2, TotalItems = result.Item3 }; } catch (Exception ex) { return ProcessException(ex); } }
public async Task<Tuple<List<Job>, int, int>> GetAllJobs(GetAllJobsParams getAllJobsParams) { using (AppDbContext context = new AppDbContext()) { User user = await GetCurrentUser(context); if (user.UserType == UserType.Employer) { var query = context.Jobs.AsQueryable(); query = query.Where(x => x.EmployerId == user.Id); int totalItems = await query.CountAsync(); int totalPages = totalItems / getAllJobsParams.PageSize; if (totalItems % getAllJobsParams.PageSize > 0) { totalPages++; } List<Job> jobs = await query.OrderByDescending(p => p.JobName).Skip(getAllJobsParams.PageIndex * getAllJobsParams.PageSize).Take(getAllJobsParams.PageSize).ToListAsync(); return new Tuple<List<Job>, int, int>(jobs, totalPages, totalItems); } else { throw new UserException(ErrorCode.NO_PERMISSION.ToString()); } } }