1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Models\JobPoster; |
7
|
|
|
use App\Models\Criteria; |
8
|
|
|
use App\Services\Validation\JobPosterValidator; |
9
|
|
|
use App\Http\Requests\UpdateJobPoster; |
10
|
|
|
use App\Http\Requests\StoreJobPoster; |
11
|
|
|
|
12
|
|
|
class JobApiController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Class constructor |
16
|
|
|
*/ |
17
|
8 |
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
// This applies the appropriate policy to each resource route. |
20
|
8 |
|
$this->authorizeResource(JobPoster::class, 'job'); |
21
|
8 |
|
} |
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
|
4 |
|
private function jobToArray(JobPoster $job) |
32
|
|
|
{ |
33
|
4 |
|
$criteria = Criteria::where('job_poster_id', $job->id)->get(); |
34
|
4 |
|
$criteriaTranslated = []; |
35
|
4 |
|
foreach ($criteria as $criterion) { |
36
|
3 |
|
$criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray()); |
37
|
|
|
} |
38
|
4 |
|
$jobArray = array_merge($job->toApiArray(), ['criteria' => $criteriaTranslated]); |
39
|
4 |
|
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 \App\Http\Requests\StoreJobPoster $request Incoming request. |
55
|
|
|
* @return \Illuminate\Http\Response |
56
|
|
|
*/ |
57
|
1 |
|
public function store(StoreJobPoster $request) |
58
|
|
|
{ |
59
|
1 |
|
$data = $request->validated(); |
60
|
1 |
|
$job = new JobPoster(); |
61
|
1 |
|
$job->manager_id = $request->user()->manager->id; |
62
|
1 |
|
$job->fill($data); |
63
|
1 |
|
$job->save(); |
64
|
1 |
|
return response()->json($this->jobToArray($job)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Display the specified resource. |
69
|
|
|
* |
70
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster. |
71
|
|
|
* @return \Illuminate\Http\Response |
72
|
|
|
*/ |
73
|
1 |
|
public function show(JobPoster $job) |
74
|
|
|
{ |
75
|
1 |
|
return response()->json($this->jobToArray($job)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Update the specified resource in storage. |
80
|
|
|
* |
81
|
|
|
* @param \App\Http\Requests\UpdateJobPoster $request Validates input. |
82
|
|
|
* @param \App\Models\JobPoster $job Incoming Job Poster. |
83
|
|
|
* @return \Illuminate\Http\Response |
84
|
|
|
*/ |
85
|
2 |
|
public function update(UpdateJobPoster $request, JobPoster $job) |
86
|
|
|
{ |
87
|
2 |
|
$data = $request->validated(); |
88
|
|
|
// Only values both in the JobPoster->fillable array, |
89
|
|
|
// and returned by UpdateJobPoster->validatedData(), will be set. |
90
|
2 |
|
$job->fill($data); |
91
|
2 |
|
$job->save(); |
92
|
2 |
|
return response()->json($this->jobToArray($job->fresh())); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Remove the specified resource from storage. |
97
|
|
|
* |
98
|
|
|
* @param integer $id Job Poster ID. |
99
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
public function destroy($id) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// TODO: complete. |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|