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
|
<?php
namespace App\Http\Controllers\Api;
use App\Enums\LogLevel;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Connection;
use App\Policies\ConnectionService;
use App\Services\LogService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ConnectionController extends Controller
{
/**
* Display a listing of connections.
*/
public function index(): JsonResponse
{
$connections = Connection::all();
$this->authorize("viewAny", Admin::class);
return response()->json(['data' => $connections]);
}
/*
* Create a new connection
*/
public function store(Request $request, LogService $logService): JsonResponse
{
$validated = $request->validate([
'student_id' => 'required|integer|exists:students,id',
'company_id' => 'required|integer|exists:companies,id',
'status' => 'required|boolean',
'skill_match_percentage' => 'nullable|numeric',
]);
$exists = Connection::where('student_id', $validated['student_id'])
->where('company_id', $validated['company_id'])
->exists();
if ($exists) {
return response()->json(['message' => 'Connection already exists'], 409);
}
$this->authorize("create", Connection::class);
// Calculate skill match percentage
$skillMatchPercentage = ConnectionService::calculateSkillMatchPercentage(
$validated['student_id'],
$validated['company_id']
);
// Add match percentage to validated data
$validated['skill_match_percentage'] = $skillMatchPercentage;
$connection = Connection::create($validated);
$logService->setLog("Student", $connection->student_id, "Connection created", "Connection");
return response()->json([
'data' => $connection,
'message' => 'connection succesvol aangemaakt'
], 201);
}
/*
* Display a specific connection by ID
*/
public function show(string $id): JsonResponse
{
try {
$connection = Connection::findOrFail($id);
$this->authorize("view", $connection);
return response()->json(['data' => $connection]);
} catch (ModelNotFoundException $e) {
return response()->json(['message' => 'connection niet gevonden'], 404);
}
}
public function showConnectionStudent(string $id): JsonResponse
{
$this->authorize('viewAny', [Connection::class, $id]);
$connections = Connection::where('student_id', $id)->get();
if ($connections->isEmpty()) {
return response()->json(['message' => 'No connection found for this student'], 404);
}
return response()->json(['data' => $connections]);
}
public function showConnectionCompany(string $id): JsonResponse
{
$this->authorize('viewAny', [Connection::class, $id]);
$connections = Connection::where('company_id', $id)->get();
if ($connections->isEmpty()) {
return response()->json(['message' => 'No connection found for this company'], 404);
}
return response()->json(['data' => $connections]);
}
/*
* Update a connection by ID
*/
public function update(Request $request, string $id, LogService $logService): JsonResponse
{
try {
$connection = Connection::findOrFail($id);
$validated = $request->validate([
'student_id' => 'required|integer|exists:students,id',
'company_id' => 'required|integer|exists:companies,id',
'status' => 'required|boolean',
]);
// Bij het updaten van de connection, controleer of student_id of company_id is gewijzigd
if ($connection->student_id != $validated['student_id'] ||
$connection->company_id != $validated['company_id']) {
$validated['skill_match_percentage'] = ConnectionService::calculateSkillMatchPercentage(
$validated['student_id'],
$validated['company_id']
);
}
$this->authorize("update", $connection);
$connection->update($validated);
$logService->setLog("Student", $connection->student_id, "Connection updated", "Connection");
return response()->json([
'data' => $connection,
'message' => 'connection succesvol bijgewerkt'
]);
} catch (ModelNotFoundException $e) {
return response()->json(['message' => 'connection niet gevonden'], 404);
}
}
/*
* Delete a connection by ID
*/
public function destroy(string $id, LogService $logService): JsonResponse
{
try {
$connection = Connection::findOrFail($id);
$this->authorize("delete", $connection);
$connection->delete();
$logService->setLog("Student", $connection->student_id, "Connection deleted ", "Connection", LogLevel::CRITICAL);
return response()->json([
'message' => 'Connection deleted'
]);
} catch (ModelNotFoundException $e) {
return response()->json(['message' => 'connection niet gevonden'], 404);
}
}
}
|