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
|
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Appointment;
use App\Services\AppointmentService;
use App\Services\LogService;
use App\Services\MailService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class AppointmentController extends Controller
{
/**
* Display a listing of the resource.
*/
use AuthorizesRequests;
public function index(): JsonResponse
{
$this->authorize('viewAny', Appointment::class);
$appointments = Cache::rememberForever('appointments.all', function () {
return Appointment::all();
});
return response()->json(['data' => $appointments]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, MailService $mailService, LogService $logService): JsonResponse
{
Cache::forget('appointments.all');
$validate = $request->validate([
'student_id' => 'required|integer|exists:students,id',
'company_id' => 'required|integer|exists:companies,id',
'time_start' => 'required|date_format:H:i',
'time_end' => 'required|date_format:H:i'
]);
if (AppointmentService::appointmentTimeOverlap($validate)) {
return response()->json([
'message' => 'That time slot is already being used'
], 400);
}
$appointment = Appointment::create($validate);
$logService->setLog("student", $appointment->student_id, "appointment creation", " appointment");
$mailService->sendAppointmentConfirmation($appointment, $logService);
return response()->json([
'data' => $appointment,
'message' => 'Appointment created successfully'
], 201);
}
/**
* Display the specified resource.
*/
public function show(string $id): JsonResponse
{
Cache::forget('appointments.all');
try {
$appointment = Appointment::findOrFail($id);
$this->authorize('view', $appointment);
return response()->json(['data' => $appointment]);
} catch (ModelNotFoundException $e) {
return response()->json(['message' => 'Appointment not found'], 404);
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id): JsonResponse
{
Cache::forget('appointments.all');
$appointment = Appointment::findOrFail($id);
$validated = $request->validate([
'time_start' => 'required|date_format:H:i',
'time_end' => 'required|date_format:H:i'
]);
$validated['company_id'] = $appointment->company_id;
// we controleren niet op een company id dus we halen hem even uit de appointment zodat de check kan werken
if (AppointmentService::appointmentTimeOverlap($validated, $id)) {
return response()->json([
'message' => 'That time slot is already being used'
], 400);
}
$this->authorize('update', $appointment);
$appointment->update($validated);
return response()->json([
'data' => $appointment,
'message' => 'Appointment updated successfully'
]);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id, LogService $logger): JsonResponse
{
Cache::forget('appointments.all');
try {
$appointment = Appointment::findOrFail($id);
$this->authorize("delete", $appointment);
$appointment->delete();
$logger->setLog("Student", $appointment->student_id, "Appointment deleted", "Appointment");
return response()->json([
'message' => 'Appointment deleted successfully'
]);
} catch (ModelNotFoundException $e) {
return response()->json([
'message' => 'Appointment not found']
, 404);
}
}
}
|