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