|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Mail; |
|
6
|
|
|
use Illuminate\Support\Facades\Auth; |
|
7
|
|
|
use App\Mail\JobPosterReviewRequested; |
|
8
|
|
|
use App\Http\Controllers\Controller; |
|
9
|
|
|
use App\Models\JobPoster; |
|
10
|
|
|
use App\Models\Criteria; |
|
11
|
|
|
use Jenssegers\Date\Date; |
|
12
|
|
|
use App\Http\Requests\UpdateJobPoster; |
|
13
|
|
|
use App\Http\Requests\StoreJobPoster; |
|
14
|
|
|
|
|
15
|
|
|
class JobApiController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
8 |
|
* Class constructor |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() |
|
21
|
8 |
|
{ |
|
22
|
8 |
|
// This applies the appropriate policy to each resource route. |
|
23
|
|
|
$this->authorizeResource(JobPoster::class, 'job'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Convert a job poster to the array expected by API requests, |
|
28
|
|
|
* with all criteria, |
|
29
|
|
|
* and with translation arrays in both languages. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster object. |
|
32
|
4 |
|
* @return mixed[] |
|
33
|
|
|
*/ |
|
34
|
4 |
|
private function jobToArray(JobPoster $job) |
|
35
|
|
|
{ |
|
36
|
|
|
$criteria = Criteria::where('job_poster_id', $job->id)->get(); |
|
37
|
3 |
|
|
|
38
|
4 |
|
$toApiArray = function ($model) { |
|
39
|
4 |
|
return array_merge($model->toArray(), $model->getTranslationsArray()); |
|
40
|
|
|
}; |
|
41
|
4 |
|
$criteriaTranslated = $criteria->map($toApiArray); |
|
42
|
4 |
|
|
|
43
|
|
|
$jobArray = array_merge($job->toApiArray(), ['criteria' => $criteriaTranslated]); |
|
44
|
|
|
return $jobArray; |
|
45
|
|
|
} |
|
46
|
|
|
/** |
|
47
|
|
|
* Display a listing of the resource. |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
public function index() |
|
52
|
|
|
{ |
|
53
|
|
|
// TODO: complete. |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Store a newly created resource in storage. |
|
58
|
|
|
* |
|
59
|
|
|
* @param \App\Http\Requests\StoreJobPoster $request Incoming request. |
|
60
|
1 |
|
* @return \Illuminate\Http\Response |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function store(StoreJobPoster $request) |
|
63
|
1 |
|
{ |
|
64
|
1 |
|
$data = $request->validated(); |
|
65
|
1 |
|
$job = new JobPoster(); |
|
66
|
1 |
|
$job->manager_id = $request->user()->manager->id; |
|
67
|
1 |
|
$job->fill($data); |
|
68
|
|
|
$job->save(); |
|
69
|
|
|
return response()->json($this->jobToArray($job)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Display the specified resource. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster. |
|
76
|
1 |
|
* @return \Illuminate\Http\Response |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function show(JobPoster $job) |
|
79
|
|
|
{ |
|
80
|
|
|
return response()->json($this->jobToArray($job)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Update the specified resource in storage. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \App\Http\Requests\UpdateJobPoster $request Validates input. |
|
87
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster. |
|
88
|
2 |
|
* @return \Illuminate\Http\Response |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function update(UpdateJobPoster $request, JobPoster $job) |
|
91
|
|
|
{ |
|
92
|
|
|
$data = $request->validated(); |
|
93
|
2 |
|
// Only values both in the JobPoster->fillable array, |
|
94
|
2 |
|
// and returned by UpdateJobPoster->validatedData(), will be set. |
|
95
|
2 |
|
$job->fill($data); |
|
96
|
|
|
$job->save(); |
|
97
|
|
|
return response()->json($this->jobToArray($job->fresh())); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Remove the specified resource from storage. |
|
102
|
|
|
* |
|
103
|
|
|
* @param integer $id Job Poster ID. |
|
104
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
public function destroy($id) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
// TODO: complete. |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Submit the Job Poster for review. |
|
113
|
|
|
* |
|
114
|
|
|
* @param \App\Models\JobPoster $job Job Poster object. |
|
|
|
|
|
|
115
|
|
|
* @return \Illuminate\Http\Response |
|
116
|
|
|
*/ |
|
117
|
|
|
public function submitForReview(JobPoster $job) |
|
118
|
|
|
{ |
|
119
|
|
|
// Update review request timestamp. |
|
120
|
|
|
$job->review_requested_at = new Date(); |
|
121
|
|
|
$job->save(); |
|
122
|
|
|
|
|
123
|
|
|
// Send email. |
|
124
|
|
|
$reviewer_email = config('mail.reviewer_email'); |
|
125
|
|
|
if (isset($reviewer_email)) { |
|
126
|
|
|
Mail::to($reviewer_email)->send(new JobPosterReviewRequested($job, Auth::user())); |
|
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
|
|
Log::error('The reviewer email environment variable is not set.'); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return response()->json($this->jobToArray($job)); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|