getAttendanceReport(
+ @Parameter(name = "startDate", description = "Start date in yyyy-MM-dd format", required = true)
+ @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
+
+ @Parameter(name = "endDate", description = "End date in yyyy-MM-dd format", required = true)
+ @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
+
+ @Parameter(name = "format", description = "Report format: PDF, XLSX, CSV, JSON", required = true, in = ParameterIn.QUERY)
+ @RequestParam ReportFormat format
+ ) {
+ byte[] report = reportService.getAttendanceStatistics(startDate, endDate, format);
+ return buildResponse(report, format, "attendance_report");
+ }
+
+ /**
+ * Builds an HTTP response with appropriate headers for file download,
+ * based on the specified report format.
+ *
+ * This method sets the correct Content-Type and
+ * Content-Disposition headers to allow clients to download
+ * the report in the requested format (PDF, XLSX, CSV, JSON).
+ *
+ * @param content the byte array representing the report content
+ * @param format the format of the report (PDF, XLSX, CSV, JSON)
+ * @param filenameBase the base name for the file (without extension)
+ * @return a ResponseEntity with the file content and appropriate headers
+ */
+ private ResponseEntity buildResponse(byte[] content, ReportFormat format, String filenameBase) {
+ String contentType;
+ String extension;
+
+ switch (format) {
+ case PDF -> {
+ contentType = "application/pdf";
+ extension = ".pdf";
+ }
+ case XLSX -> {
+ contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
+ extension = ".xlsx";
+ }
+ case CSV -> {
+ contentType = "text/csv";
+ extension = ".csv";
+ }
+ case JSON -> {
+ contentType = "application/json";
+ extension = ".json";
+ }
+ default -> {
+ contentType = "application/octet-stream";
+ extension = "";
+ }
+ }
+
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.parseMediaType(contentType));
+ headers.setContentDisposition(ContentDisposition.attachment()
+ .filename(filenameBase + extension)
+ .build());
+
+ return new ResponseEntity<>(content, headers, HttpStatus.OK);
+ }
+
// // ------------------------------------------------------
// // Equipment reservations endpoints
// // -----------------------------------------------------
@@ -974,77 +1101,6 @@ public ResponseEntity