1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use App\Models\JobPoster; |
8
|
|
|
use App\Models\Criteria; |
9
|
|
|
use App\Services\Validation\JobPosterValidator; |
10
|
|
|
use App\Http\Requests\UpdateJobPoster; |
11
|
|
|
|
12
|
|
|
class JobApiController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Class constructor |
16
|
|
|
*/ |
17
|
5 |
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
// This applies the appropriate policy to each resource route. |
20
|
5 |
|
$this->authorizeResource(JobPoster::class, 'job'); |
21
|
5 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Convert a job poster to the array expected by API requests, |
25
|
|
|
* with all criteria, |
26
|
|
|
* and with translation arrays in both languages. |
27
|
|
|
* |
28
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster object. |
29
|
|
|
* @return mixed[] |
30
|
|
|
*/ |
31
|
3 |
|
private function jobToArray(JobPoster $job) |
32
|
|
|
{ |
33
|
3 |
|
$criteria = Criteria::where('job_poster_id', $job->id)->get(); |
34
|
3 |
|
$criteriaTranslated = []; |
35
|
3 |
|
foreach ($criteria as $criterion) { |
36
|
3 |
|
$criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray()); |
37
|
|
|
} |
38
|
3 |
|
$jobArray = array_merge($job->toApiArray(), ['criteria' => $criteriaTranslated]); |
39
|
3 |
|
return $jobArray; |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* Display a listing of the resource. |
43
|
|
|
* |
44
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function index() |
47
|
|
|
{ |
48
|
|
|
// TODO: complete. |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Store a newly created resource in storage. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Http\Request $request Incoming request. |
55
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
public function store(Request $request) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
// TODO: complete. |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Display the specified resource. |
64
|
|
|
* |
65
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster. |
66
|
|
|
* @return \Illuminate\Http\Response |
67
|
|
|
*/ |
68
|
1 |
|
public function show(JobPoster $job) |
69
|
|
|
{ |
70
|
1 |
|
return $this->jobToArray($job); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Update the specified resource in storage. |
75
|
|
|
* |
76
|
|
|
* @param \App\Http\Requests\UpdateJobPoster $request Validates input. |
77
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster. |
78
|
|
|
* @return \Illuminate\Http\Response |
79
|
|
|
*/ |
80
|
2 |
|
public function update(UpdateJobPoster $request, JobPoster $job) |
81
|
|
|
{ |
82
|
2 |
|
$data = $request->validated(); |
83
|
2 |
|
JobPosterValidator::validateUnpublished($job); |
84
|
|
|
// Only values both in the JobPoster->fillable array, |
85
|
|
|
// and returned by UpdateJobPoster->validatedData(), will be set. |
86
|
2 |
|
$job->fill($data); |
87
|
2 |
|
$job->save(); |
88
|
2 |
|
return $this->jobToArray($job); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Remove the specified resource from storage. |
93
|
|
|
* |
94
|
|
|
* @param integer $id Job Poster ID. |
95
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
public function destroy($id) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
// TODO: complete. |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|