Skip to content

Commit e10fa6a

Browse files
committed
To finish
1 parent 2fca7e4 commit e10fa6a

26 files changed

+536
-85
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Employer;
6+
use Illuminate\Http\Request;
7+
8+
class EmployerController extends Controller
9+
{
10+
public function __construct()
11+
{
12+
$this->authorizeResource(Employer::class);
13+
}
14+
15+
/**
16+
* Show the form for creating a new resource.
17+
*/
18+
public function create()
19+
{
20+
return view('employer.create');
21+
}
22+
23+
/**
24+
* Store a newly created resource in storage.
25+
*/
26+
public function store(Request $request)
27+
{
28+
auth()->user()->employer()->create(
29+
$request->validate([
30+
'company_name' => 'required|min:3|unique:employers,company_name'
31+
])
32+
);
33+
34+
return redirect()->route('my-jobs.index')
35+
->with('success', 'Your employer account was created. You can post your first job.');
36+
}
37+
38+
/**
39+
* Display the specified resource.
40+
*/
41+
public function show(string $id)
42+
{
43+
//
44+
}
45+
46+
/**
47+
* Show the form for editing the specified resource.
48+
*/
49+
public function edit(string $id)
50+
{
51+
//
52+
}
53+
54+
/**
55+
* Update the specified resource in storage.
56+
*/
57+
public function update(Request $request, string $id)
58+
{
59+
//
60+
}
61+
62+
/**
63+
* Remove the specified resource from storage.
64+
*/
65+
public function destroy(string $id)
66+
{
67+
//
68+
}
69+
}

job-board/app/Http/Controllers/JobApplicationController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ public function create(Job $job)
1616
public function store(Job $job, Request $request)
1717
{
1818
$this->authorize('apply', $job);
19+
20+
$validatedData = $request->validate([
21+
'expected_salary' => 'required|min:1|max:1000000',
22+
'cv' => 'required|file|mimes:pdf|max:2048',
23+
]);
24+
25+
$file = $request->file('cv');
26+
$path = $file->store('cvs', 'private');
27+
1928
$job->jobApplications()->create([
2029
'user_id' => $request->user()->id,
21-
...$request->validate([
22-
'expected_salary' => 'required|min:1|max:1000000'
23-
])
30+
'cv_path' => $path,
31+
'expected_salary' => $validatedData['expected_salary'],
2432
]);
2533

2634
return redirect()->route('jobs.show', $job)

job-board/app/Http/Controllers/JobController.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
class JobController extends Controller
99
{
10-
/**
11-
* Display a listing of the resource.
12-
*/
1310
public function index()
1411
{
1512
$filters = request()->only(
@@ -22,58 +19,15 @@ public function index()
2219

2320
return view(
2421
'job.index',
25-
['jobs' => Job::with('employer')->filter($filters)->get()]
22+
['jobs' => Job::with('employer')->filter($filters)->latest()->get()]
2623
);
2724
}
2825

29-
/**
30-
* Show the form for creating a new resource.
31-
*/
32-
public function create()
33-
{
34-
//
35-
}
36-
37-
/**
38-
* Store a newly created resource in storage.
39-
*/
40-
public function store(Request $request)
41-
{
42-
//
43-
}
44-
45-
/**
46-
* Display the specified resource.
47-
*/
4826
public function show(Job $job)
4927
{
5028
return view(
5129
'job.show',
5230
['job' => $job->load('employer.jobs')]
5331
);
5432
}
55-
56-
/**
57-
* Show the form for editing the specified resource.
58-
*/
59-
public function edit(string $id)
60-
{
61-
//
62-
}
63-
64-
/**
65-
* Update the specified resource in storage.
66-
*/
67-
public function update(Request $request, string $id)
68-
{
69-
//
70-
}
71-
72-
/**
73-
* Remove the specified resource from storage.
74-
*/
75-
public function destroy(string $id)
76-
{
77-
//
78-
}
7933
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Job;
6+
use Illuminate\Http\Request;
7+
8+
class MyJobController extends Controller
9+
{
10+
public function __construct()
11+
{
12+
$this->authorizeResource(Job::class, 'my_job');
13+
}
14+
15+
public function index()
16+
{
17+
return view(
18+
'my_job.index',
19+
[
20+
'jobs' => auth()->user()->employer->jobs()->with('employer')->get()
21+
]
22+
);
23+
}
24+
25+
public function create()
26+
{
27+
return view('my_job.create');
28+
}
29+
30+
public function store(Request $request)
31+
{
32+
$validatedData = $request->validate([
33+
'title' => 'required|string|max:255',
34+
'location' => 'required|string|max:255',
35+
'salary' => 'required|numeric|min:5000',
36+
'description' => 'required|string',
37+
'experience' => 'required|in:' . implode(',', Job::$experience),
38+
'category' => 'required|in:' . implode(',', Job::$category),
39+
]);
40+
41+
$job = $request->user()->employer->jobs()->create($validatedData);
42+
43+
return redirect()->route('my-jobs.index')
44+
->with('success', 'Job created successfully.');
45+
}
46+
47+
public function edit(Job $myJob)
48+
{
49+
return view('my_job.edit', ['job' => $myJob]);
50+
}
51+
52+
public function update(Request $request, Job $myJob)
53+
{
54+
$validatedData = $request->validate([
55+
'title' => 'required|string|max:255',
56+
'location' => 'required|string|max:255',
57+
'salary' => 'required|numeric|min:5000',
58+
'description' => 'required|string',
59+
'experience' => 'required|in:' . implode(',', Job::$experience),
60+
'category' => 'required|in:' . implode(',', Job::$category),
61+
]);
62+
63+
$myJob->update($validatedData);
64+
65+
return redirect()->route('my-jobs.index')
66+
->with('success', 'Job updated successfully.');
67+
}
68+
69+
public function destroy(Job $myJob)
70+
{
71+
$myJob->delete();
72+
73+
return redirect()->route('my-jobs.index')
74+
->with('success', 'Job deleted successfully.');
75+
}
76+
}

job-board/app/Http/Kernel.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Kernel extends HttpKernel
1414
* @var array<int, class-string|string>
1515
*/
1616
protected $middleware = [
17-
// \App\Http\Middleware\TrustHosts::class,
17+
// \App\Http\Middleware\TrustHosts::class,
1818
\App\Http\Middleware\TrustProxies::class,
1919
\Illuminate\Http\Middleware\HandleCors::class,
2020
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
@@ -39,8 +39,8 @@ class Kernel extends HttpKernel
3939
],
4040

4141
'api' => [
42-
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
43-
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
42+
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
43+
\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
4444
\Illuminate\Routing\Middleware\SubstituteBindings::class,
4545
],
4646
];
@@ -63,5 +63,6 @@ class Kernel extends HttpKernel
6363
'signed' => \App\Http\Middleware\ValidateSignature::class,
6464
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
6565
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
66+
'employer' => \App\Http\Middleware\Employer::class
6667
];
67-
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Auth\Access\AuthorizationException;
7+
use Illuminate\Http\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class Employer
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
16+
*/
17+
public function handle(Request $request, Closure $next): Response
18+
{
19+
if (null === $request->user() || null === $request->user()->employer) {
20+
return redirect()->route('employer.create')
21+
->with('error', 'You need to register as an employer first!');
22+
// throw new AuthorizationException('You are not a registered employer.');
23+
}
24+
25+
return $next($request);
26+
}
27+
}

job-board/app/Models/Employer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Employer extends Model
1111
{
1212
use HasFactory;
1313

14+
protected $fillable = ['company_name'];
15+
1416
public function jobs(): HasMany
1517
{
1618
return $this->hasMany(Job::class);

job-board/app/Models/Job.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Job extends Model
1414
{
1515
use HasFactory;
1616

17+
protected $fillable = ['title', 'experience', 'category', 'salary', 'description', 'location'];
18+
1719
public static array $experience = ['entry', 'intermediate', 'senior'];
1820
public static array $category = [
1921
'IT',

job-board/app/Models/JobApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class JobApplication extends Model
1010
{
1111
use HasFactory;
1212

13-
protected $fillable = ['expected_salary', 'user_id', 'job_id'];
13+
protected $fillable = ['expected_salary', 'user_id', 'job_id', 'cv_path'];
1414

1515
public function job(): BelongsTo
1616
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Employer;
6+
use App\Models\User;
7+
use Illuminate\Auth\Access\Response;
8+
9+
class EmployerPolicy
10+
{
11+
/**
12+
* Determine whether the user can view any models.
13+
*/
14+
public function viewAny(User $user): bool
15+
{
16+
return false;
17+
}
18+
19+
/**
20+
* Determine whether the user can view the model.
21+
*/
22+
public function view(?User $user, Employer $employer): bool
23+
{
24+
return true;
25+
}
26+
27+
/**
28+
* Determine whether the user can create models.
29+
*/
30+
public function create(User $user): bool
31+
{
32+
return true;
33+
}
34+
35+
/**
36+
* Determine whether the user can update the model.
37+
*/
38+
public function update(User $user, Employer $employer): bool
39+
{
40+
return $employer->user_id === $user->id;
41+
}
42+
43+
/**
44+
* Determine whether the user can delete the model.
45+
*/
46+
public function delete(User $user, Employer $employer): bool
47+
{
48+
return false;
49+
}
50+
51+
/**
52+
* Determine whether the user can restore the model.
53+
*/
54+
public function restore(User $user, Employer $employer): bool
55+
{
56+
return false;
57+
}
58+
59+
/**
60+
* Determine whether the user can permanently delete the model.
61+
*/
62+
public function forceDelete(User $user, Employer $employer): bool
63+
{
64+
return false;
65+
}
66+
}

0 commit comments

Comments
 (0)