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
|
4 |
|
public function __construct() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
// This applies the appropriate policy to each resource route |
17
|
4 |
|
$this->authorizeResource(JobPoster::class, 'job'); |
18
|
4 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Convert a job poster to the array expected by API requests, |
22
|
|
|
* with all criteria, |
23
|
|
|
* and with translation arrays in both languages. |
24
|
|
|
* |
25
|
|
|
* @param JobPoster $job |
|
|
|
|
26
|
|
|
* @return mixed[] |
27
|
|
|
*/ |
28
|
2 |
|
private function jobToArray(JobPoster $job) |
|
|
|
|
29
|
|
|
{ |
30
|
2 |
|
$criteria = Criteria::where('job_poster_id', $job->id)->get(); |
31
|
2 |
|
$criteriaTranslated = []; |
32
|
2 |
|
foreach ($criteria as $criterion) { |
33
|
|
|
// TODO: getTranslationsArray probably makes DB calls every loop. Find a way to profile & optimize. |
34
|
2 |
|
$criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray()); |
35
|
|
|
} |
36
|
2 |
|
$jobArray = array_merge($job->toApiArray(), ['criteria' => $criteriaTranslated]); |
37
|
2 |
|
return $jobArray; |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* Display a listing of the resource. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function index() |
45
|
|
|
{ |
46
|
|
|
//TODO: complete |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Store a newly created resource in storage. |
51
|
|
|
* |
52
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
53
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function store(Request $request) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
//TODO: complete |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Display the specified resource. |
62
|
|
|
* |
63
|
|
|
* @param JobPoster $job |
|
|
|
|
64
|
|
|
* @return \Illuminate\Http\Response |
65
|
|
|
*/ |
66
|
1 |
|
public function show(JobPoster $job) |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
return $this->jobToArray($job); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* Update the specified resource in storage. |
73
|
|
|
* |
74
|
|
|
* @param App\Http\Requests\UpdateJobPoster $request Validates input. |
|
|
|
|
75
|
|
|
* @param JobPoster $jobPoser |
|
|
|
|
76
|
|
|
* @return \Illuminate\Http\Response |
77
|
|
|
*/ |
78
|
1 |
|
public function update(UpdateJobPoster $request, JobPoster $job) |
|
|
|
|
79
|
|
|
{ |
80
|
1 |
|
$request->validated(); |
81
|
1 |
|
JobPosterValidator::validateUnpublished($job); |
82
|
|
|
// Only values in the JobPoster->fillable array will be set |
83
|
1 |
|
$job->fill($request->input()); |
84
|
1 |
|
$job->save(); |
85
|
1 |
|
return $this->jobToArray($job); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove the specified resource from storage. |
90
|
|
|
* |
91
|
|
|
* @param int $id |
|
|
|
|
92
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
public function destroy($id) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
// TODO: |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|