-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppointmentController.java
More file actions
356 lines (336 loc) · 15.9 KB
/
AppointmentController.java
File metadata and controls
356 lines (336 loc) · 15.9 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package contactapp.api;
import contactapp.api.dto.AppointmentRequest;
import contactapp.api.dto.AppointmentResponse;
import contactapp.api.dto.ErrorResponse;
import contactapp.api.exception.ResourceNotFoundException;
import contactapp.domain.Appointment;
import contactapp.service.AppointmentService;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import static contactapp.domain.Validation.MAX_ID_LENGTH;
/**
* REST controller for Appointment CRUD operations.
*
* <p>Provides endpoints at {@code /api/v1/appointments} per ADR-0016.
*
* <h2>Endpoints</h2>
* <ul>
* <li>POST /api/v1/appointments - Create a new appointment (201 Created)</li>
* <li>GET /api/v1/appointments - List all appointments (200 OK)</li>
* <li>GET /api/v1/appointments/{id} - Get appointment by ID (200 OK / 404 Not Found)</li>
* <li>PUT /api/v1/appointments/{id} - Update appointment (200 OK / 404 Not Found)</li>
* <li>DELETE /api/v1/appointments/{id} - Delete appointment (204 No Content / 404 Not Found)</li>
* <li>PATCH /api/v1/appointments/{id}/archive - Archive appointment (200 OK / 404 Not Found)</li>
* <li>PATCH /api/v1/appointments/{id}/unarchive - Unarchive appointment (200 OK / 404 Not Found)</li>
* </ul>
*
* <h2>Date Handling</h2>
* <p>Dates are accepted and returned in ISO 8601 format with milliseconds and offset:
* {@code yyyy-MM-dd'T'HH:mm:ss.SSSXXX} (UTC timezone).
*
* <h2>Validation</h2>
* <p>Uses two layers of validation:
* <ol>
* <li>Bean Validation on request DTOs ({@code @Valid})</li>
* <li>Domain validation via {@link contactapp.domain.Validation} in Appointment constructor</li>
* </ol>
*
* @see AppointmentRequest
* @see AppointmentResponse
* @see AppointmentService
*/
@RestController
@RequestMapping(value = "/api/v1/appointments", produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(name = "Appointments", description = "Appointment CRUD operations")
@SecurityRequirement(name = "bearerAuth")
@Validated
@SuppressFBWarnings(
value = "EI_EXPOSE_REP2",
justification = "Spring-managed singleton service is intentionally stored without copy"
)
@PreAuthorize("hasAnyRole('USER', 'ADMIN')")
public class AppointmentController {
private final AppointmentService appointmentService;
/**
* Creates a new AppointmentController with the given service.
*
* @param appointmentService the service for appointment operations
*/
public AppointmentController(final AppointmentService appointmentService) {
this.appointmentService = appointmentService;
}
/**
* Creates a new appointment.
*
* @param request the appointment data
* @return the created appointment
* @throws contactapp.api.exception.DuplicateResourceException if an appointment with the given ID already exists
*/
@Operation(summary = "Create a new appointment")
@ApiResponses({
@ApiResponse(responseCode = "201", description = "Appointment created",
content = @Content(schema = @Schema(implementation = AppointmentResponse.class))),
@ApiResponse(responseCode = "400", description = "Validation error (e.g., past date)",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "409", description = "Appointment with this ID already exists",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public AppointmentResponse create(@Valid @RequestBody final AppointmentRequest request) {
final Appointment appointment = new Appointment(
request.id(),
request.appointmentDate(),
request.description()
);
appointment.setProjectId(request.projectId());
appointment.setTaskId(request.taskId());
appointmentService.addAppointment(appointment);
return AppointmentResponse.from(appointment);
}
/**
* Returns all appointments for the authenticated user.
*
* <p>For regular users, returns only their appointments.
* For ADMIN users with {@code ?all=true}, returns all appointments across all users.
*
* <p>Supports filtering by:
* <ul>
* <li>{@code ?projectId=proj-1} - filter by associated project</li>
* <li>{@code ?taskId=task-1} - filter by associated task</li>
* </ul>
*
* @param all if true and user is ADMIN, returns all appointments across all users
* @param projectId filter by associated project ID
* @param taskId filter by associated task ID
* @return list of appointments
*/
@Operation(summary = "Get all appointments",
description = "Returns appointments for the authenticated user. "
+ "ADMIN users can pass ?all=true to see all appointments across all users. "
+ "Supports filtering by projectId and taskId.")
@ApiResponse(responseCode = "200", description = "List of appointments")
@GetMapping
public List<AppointmentResponse> getAll(
@Parameter(description = "If true and user is ADMIN, returns all appointments")
@RequestParam(required = false, defaultValue = "false") final boolean all,
@Parameter(description = "Filter by associated project ID")
@RequestParam(required = false) @Size(max = MAX_ID_LENGTH) final String projectId,
@Parameter(description = "Filter by associated task ID")
@RequestParam(required = false) @Size(max = MAX_ID_LENGTH) final String taskId) {
// Handle filtering
if (projectId != null) {
return appointmentService.getAppointmentsByProjectId(projectId).stream()
.map(AppointmentResponse::from)
.toList();
}
if (taskId != null) {
return appointmentService.getAppointmentsByTaskId(taskId).stream()
.map(AppointmentResponse::from)
.toList();
}
// No filters - return all appointments
if (all) {
// Verify caller has ADMIN role before returning all users' data
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
final boolean isAdmin = auth.getAuthorities().stream()
.anyMatch(a -> "ROLE_ADMIN".equals(a.getAuthority()));
if (!isAdmin) {
throw new AccessDeniedException(
"Only administrators can access all users' appointments");
}
return appointmentService.getAllAppointmentsAllUsers().stream()
.map(AppointmentResponse::from)
.toList();
}
return appointmentService.getAllAppointments().stream()
.map(AppointmentResponse::from)
.toList();
}
/**
* Returns an appointment by ID.
*
* @param id the appointment ID
* @return the appointment
* @throws ResourceNotFoundException if no appointment with the given ID exists
*/
@Operation(summary = "Get appointment by ID")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Appointment found",
content = @Content(schema = @Schema(implementation = AppointmentResponse.class))),
@ApiResponse(responseCode = "400", description = "Invalid ID format",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Appointment not found",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/{id}")
public AppointmentResponse getById(
@Parameter(
description = "Appointment ID",
schema = @Schema(
minLength = 1,
maxLength = MAX_ID_LENGTH,
pattern = "\\S+"))
@NotBlank @Size(min = 1, max = MAX_ID_LENGTH) @PathVariable final String id) {
return appointmentService.getAppointmentById(id)
.map(AppointmentResponse::from)
.orElseThrow(() -> new ResourceNotFoundException(
"Appointment not found: " + id));
}
/**
* Updates an existing appointment.
*
* @param id the appointment ID (from path)
* @param request the updated appointment data
* @return the updated appointment
* @throws ResourceNotFoundException if no appointment with the given ID exists
*/
@Operation(summary = "Update an existing appointment")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Appointment updated",
content = @Content(schema = @Schema(implementation = AppointmentResponse.class))),
@ApiResponse(responseCode = "400", description = "Validation error (e.g., past date)",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Appointment not found",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
public AppointmentResponse update(
@Parameter(
description = "Appointment ID",
schema = @Schema(
minLength = 1,
maxLength = MAX_ID_LENGTH,
pattern = "\\S+"))
@NotBlank @Size(min = 1, max = MAX_ID_LENGTH) @PathVariable final String id,
@Valid @RequestBody final AppointmentRequest request) {
if (!appointmentService.updateAppointment(
id,
request.appointmentDate(),
request.description(),
request.projectId(),
request.taskId())) {
throw new ResourceNotFoundException("Appointment not found: " + id);
}
return getById(id);
}
/**
* Deletes an appointment by ID.
*
* @param id the appointment ID
* @throws ResourceNotFoundException if no appointment with the given ID exists
*/
@Operation(summary = "Delete an appointment")
@ApiResponses({
@ApiResponse(responseCode = "204", description = "Appointment deleted"),
@ApiResponse(responseCode = "400", description = "Invalid ID format",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Appointment not found",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(
@Parameter(
description = "Appointment ID",
schema = @Schema(
minLength = 1,
maxLength = MAX_ID_LENGTH,
pattern = "\\S+"))
@NotBlank @Size(min = 1, max = MAX_ID_LENGTH) @PathVariable final String id) {
if (!appointmentService.deleteAppointment(id)) {
throw new ResourceNotFoundException("Appointment not found: " + id);
}
}
/**
* Archives an appointment by ID.
*
* @param id the appointment ID
* @return the updated appointment
* @throws ResourceNotFoundException if no appointment with the given ID exists
*/
@Operation(summary = "Archive an appointment")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Appointment archived",
content = @Content(schema = @Schema(implementation = AppointmentResponse.class))),
@ApiResponse(responseCode = "400", description = "Invalid ID format",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Appointment not found",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/{id}/archive")
@PreAuthorize("hasAnyRole('USER', 'ADMIN')")
public AppointmentResponse archive(
@Parameter(
description = "Appointment ID",
schema = @Schema(
minLength = 1,
maxLength = MAX_ID_LENGTH,
pattern = "\\S+"))
@NotBlank @Size(min = 1, max = MAX_ID_LENGTH) @PathVariable final String id) {
if (!appointmentService.archiveAppointment(id)) {
throw new ResourceNotFoundException("Appointment not found: " + id);
}
return getById(id);
}
/**
* Unarchives an appointment by ID.
*
* @param id the appointment ID
* @return the updated appointment
* @throws ResourceNotFoundException if no appointment with the given ID exists
*/
@Operation(summary = "Unarchive an appointment")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Appointment unarchived",
content = @Content(schema = @Schema(implementation = AppointmentResponse.class))),
@ApiResponse(responseCode = "400", description = "Invalid ID format",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Appointment not found",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/{id}/unarchive")
@PreAuthorize("hasAnyRole('USER', 'ADMIN')")
public AppointmentResponse unarchive(
@Parameter(
description = "Appointment ID",
schema = @Schema(
minLength = 1,
maxLength = MAX_ID_LENGTH,
pattern = "\\S+"))
@NotBlank @Size(min = 1, max = MAX_ID_LENGTH) @PathVariable final String id) {
if (!appointmentService.unarchiveAppointment(id)) {
throw new ResourceNotFoundException("Appointment not found: " + id);
}
return getById(id);
}
}