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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Collection;
class ConnectionController extends Controller
{
//
public function makeConnection(Request $request)
{
$data = $request->validate([
'student_id' => 'required|integer',
'company_id' => 'required|integer',
'status'=> 'required|boolean'
]);
// Get authentication token from session
$token = session('api_token');
// Make API request with authentication if token exists
$httpRequest = $token
? Http::withHeaders(['Authorization' => 'Bearer ' . $token])
: Http::asJson();
$response = $httpRequest->post($this->connectionsApiUrl, $data);
if(!$response->successful()) {
// Check if it's an AJAX request
if ($request->expectsJson()) {
return response()->json([
'success' => false,
'message' => 'Er is een fout opgetreden bij het maken van de match.'
], 500);
}
return redirect()->back()->with('error', 'Er is een fout opgetreden bij het maken van de match.');
}
// Store in session as backup since GET connections API is failing
$sessionKey = "liked_companies_student_{$data['student_id']}";
$likedCompanies = session($sessionKey, []);
if ($data['status'] === true) {
// Add to liked companies if not already there
if (!in_array($data['company_id'], $likedCompanies)) {
$likedCompanies[] = $data['company_id'];
session([$sessionKey => $likedCompanies]);
}
} else {
// Remove from liked companies if disliked
$likedCompanies = array_diff($likedCompanies, [$data['company_id']]);
session([$sessionKey => array_values($likedCompanies)]);
}
// Check if it's an AJAX request
if ($request->expectsJson()) {
return response()->json([
'success' => true,
'message' => 'Connection saved successfully'
]);
}
return redirect()->back()->with('succes', 'Gematched met bedrijf ${$this->translateCompany($request["company_id"}!');
}
public function removeConnection($id, Request $request)
{
try {
$data = $request->validate([
'student_id' => 'required|integer',
'company_id' => 'required|integer'
]);
} catch (\Exception $e) {
return redirect()->back()->with('error', 'Ongeldige gegevens: ' . $e->getMessage());
}
$data['status'] = false; // Set status to false to indicate deactivation
$response = Http::patch("{$this->connectionsApiUrl}/{$id}", $data);
if (!$response->successful()) {
return redirect()->back()->with('error', '404 not found (match niet gevonden, contacteer de beheerder).');
}
return redirect()->back()->with('success', 'Match succesvol gedeactiveerd!');
}
/**
* Get connections for a specific student or company
*
* @param int $id The ID of the student or company
* @param string $type Either 'student' or 'company'
* @return array Array of connections
*/
public function getConnections($id, $type)
{
// Get authentication token from session
$token = session('api_token');
// Make API request with authentication if token exists
$httpRequest = $token
? Http::withHeaders(['Authorization' => 'Bearer ' . $token])
: Http::asJson();
try {
// Use the specific endpoint for the type
$endpoint = "{$this->connectionsApiUrl}/{$type}/{$id}";
$response = $httpRequest->get($endpoint);
// If the API is returning errors, return empty array for now
if (!$response->successful()) {
return []; // Return empty array instead of crashing
}
$responseData = $response->json('data');
if (!$responseData) {
return [];
}
// Return the connections directly since the API already filters by type and ID
return is_array($responseData) ? $responseData : [$responseData];
} catch (\Exception $e) {
return []; // Return empty array on exception
}
}
/**
* Get connections for a specific student and return liked companies
*
* @param int $id The student ID
* @param Request $request
* @return JsonResponse
*/
public function getConnectionsForStudent($id, Request $request): JsonResponse
{
try {
// Get authentication token from session
$token = session('api_token');
if (!$token) {
return response()->json([
'success' => false,
'message' => 'Authentication required'
], 401);
}
// Use the StudentController's getLikedCompanies method logic
$connections = $this->getConnections($id, 'student');
// If no connections from API, try session backup
$likedCompanyIds = [];
if (empty($connections)) {
// Fallback to session storage
$sessionKey = "liked_companies_student_{$id}";
$likedCompanyIds = session($sessionKey, []);
} else {
// Filter to only liked connections (status = true)
$likedConnections = collect($connections)->where('status', true)->all();
if (!empty($likedConnections)) {
// Get company IDs that were liked
$likedCompanyIds = collect($likedConnections)->pluck('company_id')->toArray();
}
}
$likedCompanies = [];
if (!empty($likedCompanyIds)) {
// Fetch all companies
$httpRequest = Http::withHeaders(['Authorization' => 'Bearer ' . $token]);
$response = $httpRequest->get($this->companiesApiUrl);
if ($response->successful()) {
$allCompanies = $response->json('data');
// Filter companies to only include liked ones
$likedCompanies = collect($allCompanies)
->whereIn('id', $likedCompanyIds)
->values()
->toArray();
}
}
return response()->json([
'success' => true,
'likedCompanies' => $likedCompanies
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Error fetching connections: ' . $e->getMessage()
], 500);
}
}
}
|