Skip to content

Commit 1f99650

Browse files
committed
0150-has-applied-yet-logic
1 parent c035ab6 commit 1f99650

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

job-board/app/Models/Job.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use Illuminate\Auth\Authenticatable;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Builder;
@@ -31,6 +32,15 @@ public function jobApplications(): HasMany
3132
return $this->hasMany(JobApplication::class);
3233
}
3334

35+
public function hasUserApplied(Authenticatable|User|int $user): bool
36+
{
37+
return $this->where('id', $this->id)
38+
->whereHas(
39+
'jobApplications',
40+
fn($query) => $query->where('user_id', '=', $user->id ?? $user)
41+
)->exists();
42+
}
43+
3444
public function scopeFilter(Builder|QueryBuilder $query, array $filters): Builder|QueryBuilder
3545
{
3646
return $query->when($filters['search'] ?? null, function ($query, $search) {

job-board/app/Policies/JobPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public function forceDelete(User $user, Job $job): bool
6666

6767
public function apply(User $user, Job $job): bool
6868
{
69-
return false;
69+
return !$job->hasUserApplied($user);
7070
}
7171
}

job-board/resources/views/job/show.blade.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
{!! nl2br(e($job->description)) !!}
77
</p>
88

9-
<x-link-button :href="route('job.application.create', $job)">
10-
Apply
11-
</x-link-button>
9+
@can('apply', $job)
10+
<x-link-button :href="route('job.application.create', $job)">
11+
Apply
12+
</x-link-button>
13+
@else
14+
<div class="text-center text-sm font-medium text-slate-500">
15+
You already applied to this job
16+
</div>
17+
@endcan
1218
</x-job-card>
1319

1420
<x-card class="mb-4">

0 commit comments

Comments
 (0)