public async Task<ApiJsonResult> Search(SearchJobParams jobParams)
 {
     try
     {
         Tuple<List<Job>, int, int> result = await new JobManager().Search(jobParams);
         return new ApiJsonPagingResult { Success = true, Data = result.Item1, TotalPages = result.Item2, TotalItems = result.Item3 };
     }
     catch (Exception ex)
     {
         return ProcessException(ex);
     }
 }
Beispiel #2
0
        public async Task<Tuple<List<Job>, int, int>> Search(SearchJobParams param)
        {
            using (AppDbContext context = new AppDbContext())
            {
                var query = context.Jobs.Include(x => x.Employer).Include(x => x.Educations).Include(x => x.Location).AsQueryable();

                if (!string.IsNullOrWhiteSpace(param.JobTitle))
                {
                    query = query.Where(x => x.JobName.Contains(param.JobTitle) || x.Employer.CompanyName.Contains(param.JobTitle));
                }

                if (param.Location != null)
                {
                    query = query.Where(x => x.LocationId == param.Location.Id);
                }

                if (param.Category != null)
                {
                    query = query.Where(x => x.Educations.Any(p => p.FieldOfStudyId == param.Category.Id));
                }

                if (param.JobType != 0)
                {
                    query = query.Where(x => x.JobType == param.JobType);
                }

                if (param.MinSalary > 0)
                {
                    query = query.Where(x => x.MinSalary >= param.MinSalary);
                }

                if (param.MaxSalary > 0)
                {
                    query = query.Where(x => x.MaxSalary <= param.MaxSalary);
                }

                int totalItems = await query.CountAsync();
                int totalPages = totalItems / param.PageSize;
                if (totalItems % param.PageSize > 0)
                {
                    totalPages++;
                }

                List<Job> result = await query.OrderByDescending(p => p.JobName).Skip(param.PageIndex * param.PageSize).Take(param.PageSize).ToListAsync();

                return new Tuple<List<Job>, int, int>(result, totalPages, totalItems);
            }
        }