This commit is contained in:
Doar Mihai
2026-04-03 20:55:51 +03:00
parent a8a48f3f6f
commit 31e666ef33
7 changed files with 586 additions and 68 deletions
+48 -36
View File
@@ -3,11 +3,15 @@
namespace App\Repositories;
use App\Models\Job;
use App\Services\CacheService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
class JobRepository
{
public function __construct(
private CacheService $cacheService
) {}
/**
* Get all active jobs with pagination
*/
@@ -124,24 +128,28 @@ class JobRepository
*/
public function getFeaturedJobs(int $limit = 10): Collection
{
// First, build a subquery with window function
$subQuery = \DB::table('jobs')
->selectRaw('jobs.*, ROW_NUMBER() OVER (PARTITION BY job_source_id ORDER BY posted_at DESC) as rn')
->whereNull('deleted_at')
->where(function ($query) {
$query->whereNull('expires_at')
->orWhere('expires_at', '>', now());
});
$cacheKey = $this->cacheService->featuredJobsKey($limit);
// Then query from that subquery filtering by rn
// Use withoutGlobalScopes to prevent SoftDeletes from adding WHERE clause
return Job::withoutGlobalScopes()
->with(['source', 'category', 'jobLocation'])
->fromSub($subQuery, 'ranked_jobs')
->whereRaw('ranked_jobs.rn <= 2')
->orderBy('posted_at', 'desc')
->limit($limit)
->get();
return $this->cacheService->rememberWithTags(['jobs'], $cacheKey, CacheService::CACHE_FEATURED_JOBS, function () use ($limit) {
// First, build a subquery with window function
$subQuery = \DB::table('jobs')
->selectRaw('jobs.*, ROW_NUMBER() OVER (PARTITION BY job_source_id ORDER BY posted_at DESC) as rn')
->whereNull('deleted_at')
->where(function ($query) {
$query->whereNull('expires_at')
->orWhere('expires_at', '>', now());
});
// Then query from that subquery filtering by rn
// Use withoutGlobalScopes to prevent SoftDeletes from adding WHERE clause
return Job::withoutGlobalScopes()
->with(['source', 'category', 'jobLocation'])
->fromSub($subQuery, 'ranked_jobs')
->whereRaw('ranked_jobs.rn <= 2')
->orderBy('posted_at', 'desc')
->limit($limit)
->get();
});
}
/**
@@ -149,27 +157,31 @@ class JobRepository
*/
public function getStatistics(): array
{
$total = Job::count();
$active = Job::active()->count();
$remote = Job::active()->remote()->count();
$recentlyAdded = Job::where('created_at', '>=', now()->subDays(7))->count();
$cacheKey = $this->cacheService->statisticsKey();
$topCategories = Job::active()
->select('job_category_id', \DB::raw('COUNT(*) as count'))
->whereNotNull('job_category_id')
->groupBy('job_category_id')
->orderBy('count', 'desc')
->limit(5)
->with('category')
->get();
return $this->cacheService->remember($cacheKey, CacheService::CACHE_STATISTICS, function () {
$total = Job::count();
$active = Job::active()->count();
$remote = Job::active()->remote()->count();
$recentlyAdded = Job::where('created_at', '>=', now()->subDays(7))->count();
return [
'total_jobs' => $total,
'active_jobs' => $active,
'remote_jobs' => $remote,
'recently_added' => $recentlyAdded,
'top_categories' => $topCategories,
];
$topCategories = Job::active()
->select('job_category_id', \DB::raw('COUNT(*) as count'))
->whereNotNull('job_category_id')
->groupBy('job_category_id')
->orderBy('count', 'desc')
->limit(5)
->with('category')
->get();
return [
'total_jobs' => $total,
'active_jobs' => $active,
'remote_jobs' => $remote,
'recently_added' => $recentlyAdded,
'top_categories' => $topCategories,
];
});
}
/**