This application is used for whatsapp governance, which contains the API for the several services provided by GCC.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc.restapiservices.servicesPdf.controller;
  2. import com.gcc.restapiservices.servicesPdf.service.ServicePdfService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.core.io.Resource;
  5. import org.springframework.core.io.UrlResource;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import java.net.MalformedURLException;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. @RestController
  19. @RequestMapping("/gccrestservices/api/servicespdf")
  20. public class ServicePdfController {
  21. @Autowired
  22. private ServicePdfService servicePdfService;
  23. @GetMapping("/get-services")
  24. public ResponseEntity<List<Map<String, Object>>> listServices() {
  25. List<Map<String, Object>> services = servicePdfService.findAllServices();
  26. if (services.isEmpty()) {
  27. return ResponseEntity.noContent().build();
  28. }
  29. return ResponseEntity.ok(services);
  30. }
  31. @GetMapping("/get-types")
  32. public ResponseEntity<List<Map<String, Object>>> getTypesByService(
  33. @RequestParam("serviceId") int serviceId
  34. ) {
  35. List<Map<String,Object>> types = servicePdfService.findTypesByService(serviceId);
  36. if (types.isEmpty()) {
  37. return ResponseEntity.noContent().build();
  38. }
  39. return ResponseEntity.ok(types);
  40. }
  41. @GetMapping("/download")
  42. public ResponseEntity<?> downloadPdf(
  43. @RequestParam(value = "serviceId") String serviceIdStr,
  44. @RequestParam(value = "typeId") String typeIdStr
  45. ) {
  46. if (serviceIdStr == null || serviceIdStr.isBlank()) {
  47. return ResponseEntity
  48. .badRequest()
  49. .body(Map.of(
  50. "status", "error",
  51. "message", "Missing required parameter: serviceId"
  52. ));
  53. }
  54. if (typeIdStr == null || typeIdStr.isBlank()) {
  55. return ResponseEntity
  56. .badRequest()
  57. .body(Map.of(
  58. "status", "error",
  59. "message", "Missing required parameter: typeId"
  60. ));
  61. }
  62. if (!serviceIdStr.matches("\\d+")) {
  63. return ResponseEntity
  64. .badRequest()
  65. .body(Map.of(
  66. "status", "error",
  67. "message", "Invalid parameter: 'serviceId' must be an integer"
  68. ));
  69. }
  70. if (!typeIdStr.matches("\\d+")) {
  71. return ResponseEntity
  72. .badRequest()
  73. .body(Map.of(
  74. "status", "error",
  75. "message", "Invalid parameter: 'typeId' must be an integer"
  76. ));
  77. }
  78. int serviceId = Integer.parseInt(serviceIdStr);
  79. int typeId = Integer.parseInt(typeIdStr);
  80. try {
  81. String pdfFullPath = servicePdfService.getPdfPathByServiceAndType(serviceId, typeId);
  82. if (pdfFullPath == null) {
  83. return ResponseEntity.status(404).body(Map.of(
  84. "status", "error",
  85. "message", "No PDF found for serviceId=" + serviceId + ", typeId=" + typeId
  86. ));
  87. }
  88. Resource resource = new UrlResource(pdfFullPath);
  89. if (!resource.exists()) {
  90. return ResponseEntity.status(404).body(Map.of(
  91. "status", "error",
  92. "message", "PDF file not found on server for serviceId=" + serviceId +
  93. ", typeId=" + typeId
  94. ));
  95. }
  96. String fileName = pdfFullPath.substring(pdfFullPath.lastIndexOf("/") + 1);
  97. return ResponseEntity.ok(Map.of(
  98. "status", "success",
  99. "serviceId", serviceId,
  100. "typeId", typeId,
  101. "fileName", fileName,
  102. "url", pdfFullPath
  103. ));
  104. } catch (MalformedURLException e) {
  105. e.printStackTrace();
  106. return ResponseEntity.status(500).body(Map.of(
  107. "status", "error",
  108. "message", "Invalid URL format for PDF file."
  109. ));
  110. }
  111. }
  112. }