Passed
Push — master ( b25be8...d8e2f1 )
by Tristan
07:17 queued 10s
created

ApplicantSkillsController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 2
1
<?php
2
namespace App\Http\Controllers\Api;
3
4
use App\Http\Controllers\Controller;
5
use App\Http\Requests\UpdateApplicantSkills;
6
use App\Models\Applicant;
7
use App\Models\ExperienceSkill;
8
use Illuminate\Http\Resources\Json\JsonResource;
9
use Illuminate\Support\Facades\DB;
10
11
class ApplicantSkillsController extends Controller
12
{
13
    public function index(Applicant $applicant)
14
    {
15
        return [
16
            'skill_ids' => $applicant->skills->pluck('id')->all()
17
        ];
18
    }
19
20
    public function update(UpdateApplicantSkills $request, Applicant $applicant)
21
    {
22
        $skillIds = $request->validated()['skill_ids'];
23
        $applicant->skills()->sync($skillIds);
24
25
        // $deletedExperienceSkills = $applicant->experienceSkillsQuery()->whereNotIn('skill_id', $skillIds)->get();
26
        // I'm leaving the above line commented out for now, but if we want to do something with the
27
        // deleted ExperienceSkill objects, we can retrieve them before deleting and include
28
        // them in the api response.
29
        $applicant->experienceSkillsQuery()->whereNotIn('skill_id', $skillIds)->delete();
30
        return [
31
            'skill_ids' => $skillIds,
32
            // 'deleted_experience_skills' => JsonResource::collection($deletedExperienceSkills),
33
        ];
34
    }
35
}
36