UzyTavAnexoSPRController.java 3.88 KB
Newer Older
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
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();
        }
    }

53 54 55 56 57 58
    @GetMapping("/ListarRegistrosPorProyecID/{proyecID}")
    public ResponseEntity<List<DtoUzyTavAnexoSPR>> ListarRegistrosPorProyecID(@PathVariable("proyecID") Long proyecID) {
        List<DtoUzyTavAnexoSPR> dtos = serviceUzyTavAnexoSPR.ListarRegistrosPorProyecID(proyecID);
        return ResponseEntity.ok(dtos);
    }

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

    @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());
        }
    }

}