@RestController
@RequestMapping("/pacientes")
public class PacienteController {
@Autowired
private PacienteRepository repository;
@PostMapping
@Transactional
public ResponseEntity registrar(@RequestBody @Valid DatosRegistroPaciente datos,
UriComponentsBuilder uriBuilder) {
var paciente = new Paciente(datos);
repository.save(paciente);
var uri = uriBuilder.path("/pacientes/{id}").buildAndExpand(paciente.getId()).toUri();
return ResponseEntity.created(uri).body(new DatosDetallePaciente(paciente));
}
@GetMapping
public ResponseEntity<Page<DatosListaPaciente>> listar(
@PageableDefault(size = 10, sort = {"nombre"}) Pageable paginacion) {
var page = repository.findAll(paginacion).map(DatosListaPaciente::new);
return ResponseEntity.ok(page);
}
@PutMapping
@Transactional
public ResponseEntity actualizar(@RequestBody @Valid DatosActualizacionPaciente datos) {
var paciente = repository.getReferenceById(datos.id());
paciente.actualizarInformacion(datos);
return ResponseEntity.ok(new DatosDetallePaciente(paciente));
}
@DeleteMapping("/{id}")
@Transactional
public ResponseEntity eliminar(@PathVariable Long id) {
repository.deleteById(id);
return ResponseEntity.noContent().build();
}
}
public record DatosRegistroPaciente(
@NotBlank String nombre,
@NotBlank String email,
@NotBlank String telefono,
@NotBlank String documento,
@NotBlank String direccion
) {
}
public record DatosListaPaciente(
Long id,
String nombre,
String email,
String documento
) {
public DatosListaPaciente(Paciente paciente) {
this(
paciente.getId(),
paciente.getNombre(),
paciente.getEmail(),
paciente.getDocumento()
);
}
}
public record DatosActualizacionPaciente(
@NotNull Long id,
String nombre,
String telefono,
String direccion
) {
}
public record DatosDetallePaciente(
Long id,
String nombre,
String email,
String telefono,
String documento,
String direccion
) {
public DatosDetallePaciente(Paciente paciente) {
this(
paciente.getId(),
paciente.getNombre(),
paciente.getEmail(),
paciente.getTelefono(),
paciente.getDocumento(),
paciente.getDireccion()
);
}
}
@Entity
@Table(name = "pacientes")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
public class Paciente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nombre;
private String email;
private String telefono;
private String documento;
private String direccion;
public Paciente(DatosRegistroPaciente datos) {
this.nombre = datos.nombre();
this.email = datos.email();
this.telefono = datos.telefono();
this.documento = datos.documento();
this.direccion = datos.direccion();
}
public void actualizarInformacion(DatosActualizacionPaciente datos) {
if (datos.nombre() != null) {
this.nombre = datos.nombre();
}
if (datos.telefono() != null) {
this.telefono = datos.telefono();
}
if (datos.direccion() != null) {
this.direccion = datos.direccion();
}
}
}
public interface PacienteRepository extends JpaRepository<Paciente, Long> {
}
return ResponseEntity.created(uri).body(new DatosDetallePaciente(paciente));
return ResponseEntity.ok(page);
return ResponseEntity.ok(new DatosDetallePaciente(paciente));
return ResponseEntity.noContent().build();