TramiteController.java
package com.licensis.notaire.api;
import com.licensis.notaire.negocio.Tramite;
import com.licensis.notaire.repository.TramiteRepository;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.transaction.annotation.Transactional;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/v1/tramites")
@Tag(name = "Trámites", description = "API para gestionar trámites")
public class TramiteController {
private static final Logger log = LoggerFactory.getLogger(TramiteController.class);
private final TramiteRepository repository;
public TramiteController(TramiteRepository repository) {
this.repository = repository;
}
@GetMapping
@Operation(summary = "Obtener todos los trámites")
@Transactional(readOnly = true)
public ResponseEntity<Page<Tramite>> getAll(
@PageableDefault(size = 20) Pageable pageable) {
return ResponseEntity.ok(repository.findAll(pageable));
}
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "No encontrado")
})
@GetMapping("/{id}")
@Operation(summary = "Obtener trámite por ID")
@Transactional(readOnly = true)
public ResponseEntity<Tramite> getById(@PathVariable Integer id) {
return repository.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@ApiResponses({
@ApiResponse(responseCode = "201", description = "Creado"),
@ApiResponse(responseCode = "400", description = "Solicitud inválida"),
@ApiResponse(responseCode = "409", description = "Conflicto")
})
@PostMapping
@Operation(summary = "Crear nuevo trámite")
public ResponseEntity<Object> create(@RequestBody Tramite entity) {
try {
entity = repository.save(entity);
return ResponseEntity.status(HttpStatus.CREATED).body(entity);
} catch (Exception e) {
log.error("Failed to create tramite", e);
return ResponseEntity.internalServerError().build();
}
}
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "No encontrado")
})
@PutMapping("/{id}")
@Operation(summary = "Actualizar trámite")
public ResponseEntity<Void> update(@PathVariable Integer id, @RequestBody Tramite entity) {
if (!repository.existsById(id)) {
return ResponseEntity.notFound().build();
}
try {
entity.setIdTramite(id);
repository.save(entity);
return ResponseEntity.ok().build();
} catch (Exception e) {
log.error("Failed to update tramite id {}", id, e);
return ResponseEntity.internalServerError().build();
}
}
@ApiResponses({
@ApiResponse(responseCode = "204", description = "Eliminado"),
@ApiResponse(responseCode = "404", description = "No encontrado")
})
@DeleteMapping("/{id}")
@Operation(summary = "Eliminar trámite")
public ResponseEntity<Void> delete(@PathVariable Integer id) {
if (!repository.existsById(id)) {
return ResponseEntity.notFound().build();
}
try {
repository.deleteById(id);
return ResponseEntity.ok().build();
} catch (Exception e) {
log.error("Failed to delete tramite id {}", id, e);
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
}
}