improvements for the jobs page
This commit is contained in:
@@ -7,8 +7,10 @@ use App\Models\AnalyticsEvent;
|
||||
use App\Models\Job;
|
||||
use App\Models\JobCategory;
|
||||
use App\Repositories\JobRepository;
|
||||
use App\Services\CacheService;
|
||||
use App\Services\JobSearchService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class JobController extends Controller
|
||||
{
|
||||
@@ -17,6 +19,13 @@ class JobController extends Controller
|
||||
private JobRepository $jobRepository
|
||||
) {}
|
||||
|
||||
private function getCategoriesWithCount()
|
||||
{
|
||||
return Cache::remember('categories:with_job_count', CacheService::CACHE_CATEGORIES, function () {
|
||||
return JobCategory::withCount('jobs')->orderBy('name')->get();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of jobs with search and filters
|
||||
*/
|
||||
@@ -44,7 +53,7 @@ class JobController extends Controller
|
||||
$perPage = $request->input('per_page', 20);
|
||||
$jobs = $this->searchService->search($filters, $perPage);
|
||||
|
||||
$categories = JobCategory::withCount('jobs')->orderBy('name')->get();
|
||||
$categories = $this->getCategoriesWithCount();
|
||||
$filterOptions = $this->searchService->getFilterOptions();
|
||||
|
||||
return view('jobs.index', compact('jobs', 'categories', 'filterOptions', 'filters'));
|
||||
@@ -117,7 +126,7 @@ class JobController extends Controller
|
||||
$perPage = $request->input('per_page', 20);
|
||||
$jobs = $this->searchService->search($filters, $perPage);
|
||||
|
||||
$categories = JobCategory::withCount('jobs')->orderBy('name')->get();
|
||||
$categories = $this->getCategoriesWithCount();
|
||||
$filterOptions = $this->searchService->getFilterOptions();
|
||||
|
||||
return view('jobs.remote', compact('jobs', 'categories', 'filterOptions', 'filters'));
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Services;
|
||||
use App\Models\Job;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
|
||||
class JobSearchService
|
||||
{
|
||||
@@ -16,17 +17,21 @@ class JobSearchService
|
||||
*/
|
||||
public function search(array $filters = [], int $perPage = 15): LengthAwarePaginator
|
||||
{
|
||||
$page = Request::input('page', 1);
|
||||
$cacheKey = $this->cacheService->searchResultsKey($filters, (int) $page);
|
||||
|
||||
$cached = $this->cacheService->remember($cacheKey, CacheService::CACHE_SEARCH_RESULTS, function () use ($filters, $perPage) {
|
||||
$query = Job::query()
|
||||
->with(['source', 'category', 'jobLocation'])
|
||||
->active();
|
||||
|
||||
// Apply filters
|
||||
$this->applyFilters($query, $filters);
|
||||
|
||||
// Apply sorting
|
||||
$this->applySorting($query, $filters['sort'] ?? 'date');
|
||||
|
||||
return $query->paginate($perPage);
|
||||
});
|
||||
|
||||
return $cached;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user