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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\MessageController;
class CompanyController extends Controller
{
//companiesApiUrls in parent class
//Index function to shown on the company page, takes the ID and shows the company
public function index(string $id): View
{
$token = session('api_token');
$response = Http::withToken($token)->get("{$this->companiesApiUrl}/{$id}");
if (!$response->successful()) {
return view('notfound', ['message' => 'Dit bedrijf lijkt niet te bestaan of je hebt geen toegang (error code ' . $response->status() . '). Contacteer de beheerder van de site voor meer informatie']);
}
$company = $response->json('data');
//get all appointments where the company is involved
$response = Http::get("{$this->appointmentApiUrl}");
$appointments = $response->json('data');
$appointments = collect($appointments)->where('company_id', $id)->all();
//Gives names to the ID's
foreach ($appointments as &$appointment) {
//translate student and company id to names
$appointment['student_name'] = $this->translateStudent($appointment['student_id']);
$appointment['company_name'] = $this->translateCompany($appointment['company_id']);
}
$connectionController = new \App\Http\Controllers\ConnectionController();
$connections = $connectionController->getConnections($id, 'company');
// Fetch students data for messages
$studentsResponse = Http::get($this->studentsApiUrl);
$students = [];
if ($studentsResponse->successful()) {
$students = $studentsResponse->json('data');
}
// Get all messages for this company
$messageController = new MessageController();
$allMessages = $messageController->getAllMessagesForCompany($id);
return view('company.company', [
'company' => $company,
'appointments' => $appointments,
'connections' => $allMessages, // Use messages instead of connections
'students' => $students
]);
}
//Test Function to show a company, this will be replaced by the index function.
public function indexTest(): View
{
$id = 3; //This is a test company, will be replaced by the index function
$response = Http::get("{$this->companiesApiUrl}/{$id}");
if (!$response->successful()) {
return view('notfound', ['message' => 'Dit bedrijf lijkt niet te bestaan (error code 404). Contacteer de beheerder van de site voor meer informatie']);
}
$company = $response->json('data');
//get all appointments where the company is involved
$response = Http::get("{$this->appointmentApiUrl}");
$appointments = $response->json('data');
$appointments = collect($appointments)->where('company_id', $id)->all();
//Gives names to the ID's
foreach ($appointments as &$appointment) {
//translate student and company id to names
$appointment['student_name'] = $this->translateStudent($appointment['student_id']);
$appointment['company_name'] = $this->translateCompany($appointment['company_id']);
}
$connectionController = new \App\Http\Controllers\ConnectionController();
$connections = $connectionController->getConnections($id, 'company');
// Fetch students data for messages
$studentsResponse = Http::get($this->studentsApiUrl);
$students = [];
if ($studentsResponse->successful()) {
$students = $studentsResponse->json('data');
}
// Get all messages for this company
$messageController = new MessageController();
$allMessages = $messageController->getAllMessagesForCompany($id);
return view('company.company', [
'company' => $company,
'appointments' => $appointments,
'connections' => $allMessages, // Use messages instead of connections
'students' => $students
]);
}
//
public function store(Request $request)
{
try {
$token = "Bearer " . $request['token'];
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
// unique email must be fixed
'plan_type' => 'required|string',
'booth_location' => 'required|string|max:255',
'password1' => 'required|string|min:8',
'password2' => 'required|same:password1',
// Optional fields
'company_description' => 'nullable|string|max:1000',
'job_types' => 'nullable|string|max:255',
'job_domain' => 'nullable|string|max:255',
'job_title' => 'nullable|string|max:255',
'job_description' => 'nullable|string|max:255',
'job_requirements' => 'nullable|string|max:255',
'photo' => 'nullable|image',
'speeddate_duration' => 'nullable|integer',
]);
} catch (\Exception $e) {
return redirect()->back()->with('error', 'Validatie mislukt: ' . $e->getMessage());
}
if (isset($validated['company_description'])) {
$validated['company_description'] = 'Vul dit in';
}
if (isset($validated['job_types'])) {
$validated['job_types'] = 'Vul dit in';
}
if (isset($validated['job_domain'])) {
$validated['job_domain'] = 'Vul dit in';
}
if (isset($validated['photo'])) {
$validated['photo'] = 'Aan te passen';
}
if (isset($validated['speeddate_duration'])) {
$validated['speeddate_duration'] = '1'; // Default value
}
//Take password 1 for value
unset($validated['password2']);
$validated['password'] = $validated['password1'];
unset($validated['password1']);
try {
$response = Http::withHeaders( [
"Authorization" => $token
])->post($this->companiesApiUrl, $validated);
return redirect()->back()->with('success', 'Bedrijf succesvol toegevoegd!');
} catch (\Exception $e) {
return redirect()->back()->with('error', 'Er is een fout opgetreden: ' . $e->getMessage());
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
try {
$validated = $request->validate([
'name' => 'sometimes|string|max:255',
'email' => 'sometimes|email|max:255',
// unique email must be fixed
'plan_type' => 'sometimes|string',
'booth_location' => 'sometimes|string|max:255',
'password1' => 'sometimes|string|min:8',
'password2' => 'sometimes|same:password1',
// Optional fields
'company_description' => 'nullable|string|max:1000',
'job_types' => 'nullable|string|max:255',
'job_domain' => 'nullable|string|max:255',
'job_title' => 'nullable|string|max:255',
'job_description' => 'nullable|string|max:255',
'job_requirements' => 'nullable|string|max:255',
'photo' => 'nullable|image',
'speeddate_duration' => 'nullable|integer',
]);
} catch (\Exception $e) {
return redirect()->back()->with('error', 'Validatie mislukt: ' . $e->getMessage());
}
//Current data is taken and merged with the validated data
$current = Http::get("{$this->companiesApiUrl}/{$id}");
$current = $current->json('data');
$data = array_merge($current, $validated);
unset($data['password1']); // Remove password field if it is not set in the request
unset($data['password2']); // Remove password field if it is not set in the request
//Password hashing issue, please fix
$response = Http::patch("{$this->companiesApiUrl}/{$id}", $data);
if (!$response->successful()) {
return redirect()->back()->with('error', 'Er is een fout opgetreden bij het bijwerken van het account.');
}
return redirect()->back()->with('success', 'Account succesvol aangepast');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id, Request $request)
{
$token = "Bearer " . $request['token'];
$response = Http::withHeaders( [
"Authorization" => $token
])->delete("{$this->companiesApiUrl}{$id}");
if (!$response->successful()) {
return redirect()->back()->with('error', 'Er is een fout opgetreden bij het verwijderen van het bedrijf.');
}
return redirect()->back()->with('success', 'Bedrijf succesvol verwijderd!');
}
/**
* Send a message from company to student
*/
public function sendMessage(Request $request, string $id)
{
// Validate the request
$validated = $request->validate([
'receiver_id' => 'required|integer',
'content' => 'required|string|max:1000'
]);
// Prepare data for MessageController
$messageData = [
'sender_id' => (int)$id,
'sender_type' => 'App\\Models\\Company',
'receiver_id' => $validated['receiver_id'],
'receiver_type' => 'App\\Models\\Student',
'content' => $validated['content']
];
// Use MessageController to send the message
$messageController = new MessageController();
$messageRequest = new Request($messageData);
$messageRequest->headers->set('Accept', 'application/json');
return $messageController->sendMessage($messageRequest);
}
}
|