package ec.edu.espe.movilidad.MovilidadWS.Controller; import ec.edu.espe.movilidad.MovilidadWS.Dto.DtoUzyTavAnexoSPR; import ec.edu.espe.movilidad.MovilidadWS.Service.UzyTavAnexoSPR.IServiceUzyTavAnexoSPR; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import static ec.edu.espe.movilidad.MovilidadWS.Constant.GlobalConstants.V1_API_VERSION; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; @RestController @CrossOrigin(origins = "*") @RequestMapping(V1_API_VERSION+"/anexos") public class UzyTavAnexoSPRController { private final IServiceUzyTavAnexoSPR serviceUzyTavAnexoSPR; @Autowired public UzyTavAnexoSPRController(IServiceUzyTavAnexoSPR serviceUzyTavAnexoSPR) { this.serviceUzyTavAnexoSPR = serviceUzyTavAnexoSPR; } @PostMapping("/guardarnuevo") public ResponseEntity<DtoUzyTavAnexoSPR> saveUzyTavConvoca( @RequestParam("file") MultipartFile file, @ModelAttribute DtoUzyTavAnexoSPR dtoUzyTavAnexoSPR) throws IOException { dtoUzyTavAnexoSPR.setUzytavanexospr_digital(file); DtoUzyTavAnexoSPR savedUzyTavConvoca = serviceUzyTavAnexoSPR.save(dtoUzyTavAnexoSPR); if (savedUzyTavConvoca != null) { return ResponseEntity.ok(savedUzyTavConvoca); } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } @GetMapping("/exampleFindId/{id}") public ResponseEntity<DtoUzyTavAnexoSPR> listarPorID(@PathVariable("id") Long id) { DtoUzyTavAnexoSPR dto = serviceUzyTavAnexoSPR.ListarPorID(id); if (dto != null) { return ResponseEntity.ok(dto); } else { return ResponseEntity.notFound().build(); } } @GetMapping("/ListarRegistrosPorProyecID/{proyecID}") public ResponseEntity<List<DtoUzyTavAnexoSPR>> ListarRegistrosPorProyecID(@PathVariable("proyecID") Long proyecID) { List<DtoUzyTavAnexoSPR> dtos = serviceUzyTavAnexoSPR.ListarRegistrosPorProyecID(proyecID); return ResponseEntity.ok(dtos); } @GetMapping("/getAll") public ResponseEntity<List<DtoUzyTavAnexoSPR>> listarRegistros() { List<DtoUzyTavAnexoSPR> dtos = serviceUzyTavAnexoSPR.ListarRegistros(); return ResponseEntity.ok(dtos); } @GetMapping("/verImagen/{uzytavanexospr_id}") public ResponseEntity<byte[]> obtenerContenidoPDF(@PathVariable("uzytavanexospr_id") Long uzytavanexospr_id) { String fileName = serviceUzyTavAnexoSPR.obtenerNombrePDF(uzytavanexospr_id); byte[] pdfContent = serviceUzyTavAnexoSPR.obtenerContenidoPDF(uzytavanexospr_id); if (pdfContent != null && fileName != null) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_JPEG); headers.setContentType(MediaType.IMAGE_PNG); headers.setContentDisposition(ContentDisposition.builder("inline").filename(fileName).build()); return new ResponseEntity<>(pdfContent, headers, HttpStatus.OK); } else { return ResponseEntity.notFound().build(); } } @DeleteMapping("/eliminar/{uzytavanexospr_id}") public ResponseEntity<String> eliminarConPDF(@PathVariable("uzytavanexospr_id") Long uzytavanexospr_id) { try { serviceUzyTavAnexoSPR.eliminarConPDF(uzytavanexospr_id); return ResponseEntity.ok("Registro eliminado exitosamente."); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("Error al eliminar el registro: " + e.getMessage()); } } }