Files
jobifygarden/app/Http/Controllers/Web/JobController.php
T

135 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
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
{
public function __construct(
private JobSearchService $searchService,
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
*/
public function index(Request $request)
{
$filters = $request->only([
'keywords',
'location',
'category_id',
'job_type',
'experience_level',
'remote',
'salary_min',
'salary_max',
'posted_within',
'company',
'sort',
]);
// Track search event if keywords are provided
if (!empty($filters['keywords'])) {
AnalyticsEvent::logEvent(AnalyticsEvent::EVENT_SEARCH, null, $filters);
}
$perPage = $request->input('per_page', 20);
$jobs = $this->searchService->search($filters, $perPage);
$categories = $this->getCategoriesWithCount();
$filterOptions = $this->searchService->getFilterOptions();
return view('jobs.index', compact('jobs', 'categories', 'filterOptions', 'filters'));
}
/**
* Display the specified job
*/
public function show(Job $job)
{
// Load relationships
$job->load(['source', 'category', 'jobLocation', 'tags']);
// Track job view event
AnalyticsEvent::logEvent(AnalyticsEvent::EVENT_JOB_VIEW, $job->id);
$similarJobs = $this->jobRepository->getSimilarJobs($job, 5);
return view('jobs.show', compact('job', 'similarJobs'));
}
/**
* Display jobs by category
*/
public function category(string $slug, Request $request)
{
$category = JobCategory::where('slug', $slug)->firstOrFail();
// Track category view event
AnalyticsEvent::logEvent(AnalyticsEvent::EVENT_CATEGORY_VIEW, null, ['category' => $category->name]);
$filters = $request->only([
'keywords',
'location',
'job_type',
'experience_level',
'remote',
'salary_min',
'salary_max',
'posted_within',
'sort',
]);
$filters['category_id'] = $category->id;
$perPage = $request->input('per_page', 20);
$jobs = $this->searchService->search($filters, $perPage);
$filterOptions = $this->searchService->getFilterOptions();
return view('jobs.category', compact('category', 'jobs', 'filterOptions', 'filters'));
}
/**
* Display remote jobs
*/
public function remote(Request $request)
{
$filters = $request->only([
'keywords',
'category_id',
'job_type',
'experience_level',
'salary_min',
'salary_max',
'posted_within',
'sort',
]);
$filters['remote'] = true;
$perPage = $request->input('per_page', 20);
$jobs = $this->searchService->search($filters, $perPage);
$categories = $this->getCategoriesWithCount();
$filterOptions = $this->searchService->getFilterOptions();
return view('jobs.remote', compact('jobs', 'categories', 'filterOptions', 'filters'));
}
}