aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/Api/SkillController.php
blob: cc73a2e3421b8756401a646d154c4a5235f99533 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Skill;
use App\Models\Student;
use App\Policies\ConnectionService;
use App\Services\LogService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class SkillController extends Controller
{
    /**
     * Display a listing of skills.
     */
    public function index(): JsonResponse
    {
        $skills = Cache::remember('skills.all', 60 * 60, function () {
            return Skill::all();
        });


        return response()->json([
            'data' => $skills
        ]);
    }

    /**
     * Display the specified skill.
     */
    public function show(string $id): JsonResponse
    {
        try {
            $skill = Skill::findOrFail($id);
            return response()->json(['data' => $skill]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Skill not found'], 404);
        }
    }

    /**
     * Attach skills to a student.
     */
    public function attachToStudent(Request $request, string $id, LogService $logService): JsonResponse
    {
        try {
            $student = Student::findOrFail($id);

            $validated = $request->validate([
                'skill_ids' => 'required|array',
                'skill_ids.*' => 'exists:skills,id'
            ]);

            $student->skills()->syncWithoutDetaching($validated['skill_ids']);

            $logService->setLog("Student", $student->id, "Skills added to student", "Student");

            return response()->json([
                'message' => 'Skills attached successfully',
                'data' => $student->load('skills')
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Student not found'], 404);
        }
    }

    /**
     * Detach a skill from a student.
     */
    public function detachFromStudent(string $id, string $skillId, LogService $logService): JsonResponse
    {
        try {
            $student = Student::findOrFail($id);
            $student->skills()->detach($skillId);

            $logService->setLog("Student", $student->id, "Skill removed from student", "Student");

            return response()->json([
                'message' => 'Skill detached successfully'
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Student or skill not found'], 404);
        }
    }

    /**
     * Get all skills for a student.
     */
    public function getStudentSkills(string $id): JsonResponse
    {
        try {
            $student = Student::with('skills')->findOrFail($id);
            return response()->json([
                'data' => $student->skills
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Student not found'], 404);
        }
    }

    /**
     * Attach skills to a company.
     */
    public function attachToCompany(Request $request, string $id, LogService $logService): JsonResponse
    {
        try {
            $company = Company::findOrFail($id);

            $validated = $request->validate([
                'skill_ids' => 'required|array',
                'skill_ids.*' => 'exists:skills,id'
            ]);

            $company->skills()->syncWithoutDetaching($validated['skill_ids']);

            $logService->setLog("Company", $company->id, "Skills added to company", "Company");

            return response()->json([
                'message' => 'Skills attached successfully',
                'data' => $company->load('skills')
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Company not found'], 404);
        }
    }

    /**
     * Detach a skill from a company.
     */
    public function detachFromCompany(string $id, string $skillId, LogService $logService): JsonResponse
    {
        try {
            $company = Company::findOrFail($id);
            $company->skills()->detach($skillId);

            $logService->setLog("Company", $company->id, "Skill removed from company", "Company");

            return response()->json([
                'message' => 'Skill detached successfully'
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Company or skill not found'], 404);
        }
    }

    /**
     * Get all skills for a company.
     */
    public function getCompanySkills(string $id): JsonResponse
    {
        try {
            $company = Company::with('skills')->findOrFail($id);
            return response()->json([
                'data' => $company->skills
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Company not found'], 404);
        }
    }

    /**
     * Calculate the skill match percentage between a student and company.
     */
    public function calculateMatch(string $studentId, string $companyId): JsonResponse
    {
        try {
            $matchPercentage = ConnectionService::calculateSkillMatchPercentage($studentId, $companyId);

            return response()->json([
                'data' => [
                    'student_id' => $studentId,
                    'company_id' => $companyId,
                    'match_percentage' => $matchPercentage
                ]
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['message' => 'Student or company not found'], 404);
        }
    }
}