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

ApplicantSkillsController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 8
c 1
b 0
f 0
dl 0
loc 21
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 12 1
A index() 0 4 1
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