package com.gcc.restapiservices.servicesPdf.controller; import com.gcc.restapiservices.servicesPdf.service.ServicePdfService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.net.MalformedURLException; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/gccrestservices/api/servicespdf") public class ServicePdfController { @Autowired private ServicePdfService servicePdfService; @GetMapping("/get-services") public ResponseEntity>> listServices() { List> services = servicePdfService.findAllServices(); if (services.isEmpty()) { return ResponseEntity.noContent().build(); } return ResponseEntity.ok(services); } @GetMapping("/get-types") public ResponseEntity>> getTypesByService( @RequestParam("serviceId") int serviceId ) { List> types = servicePdfService.findTypesByService(serviceId); if (types.isEmpty()) { return ResponseEntity.noContent().build(); } return ResponseEntity.ok(types); } @GetMapping("/download") public ResponseEntity downloadPdf( @RequestParam(value = "serviceId") String serviceIdStr, @RequestParam(value = "typeId") String typeIdStr ) { if (serviceIdStr == null || serviceIdStr.isBlank()) { return ResponseEntity .badRequest() .body(Map.of( "status", "error", "message", "Missing required parameter: serviceId" )); } if (typeIdStr == null || typeIdStr.isBlank()) { return ResponseEntity .badRequest() .body(Map.of( "status", "error", "message", "Missing required parameter: typeId" )); } if (!serviceIdStr.matches("\\d+")) { return ResponseEntity .badRequest() .body(Map.of( "status", "error", "message", "Invalid parameter: 'serviceId' must be an integer" )); } if (!typeIdStr.matches("\\d+")) { return ResponseEntity .badRequest() .body(Map.of( "status", "error", "message", "Invalid parameter: 'typeId' must be an integer" )); } int serviceId = Integer.parseInt(serviceIdStr); int typeId = Integer.parseInt(typeIdStr); try { String pdfFullPath = servicePdfService.getPdfPathByServiceAndType(serviceId, typeId); if (pdfFullPath == null) { return ResponseEntity.status(404).body(Map.of( "status", "error", "message", "No PDF found for serviceId=" + serviceId + ", typeId=" + typeId )); } Resource resource = new UrlResource(pdfFullPath); if (!resource.exists()) { return ResponseEntity.status(404).body(Map.of( "status", "error", "message", "PDF file not found on server for serviceId=" + serviceId + ", typeId=" + typeId )); } String fileName = pdfFullPath.substring(pdfFullPath.lastIndexOf("/") + 1); return ResponseEntity.ok(Map.of( "status", "success", "serviceId", serviceId, "typeId", typeId, "fileName", fileName, "url", pdfFullPath )); } catch (MalformedURLException e) { e.printStackTrace(); return ResponseEntity.status(500).body(Map.of( "status", "error", "message", "Invalid URL format for PDF file." )); } } }