From bc072aafa156b303b0ea2681cc23f449bc1734b3 Mon Sep 17 00:00:00 2001 From: lucashemi Date: Mon, 27 Feb 2023 22:31:42 -0300 Subject: alpha version --- .../voll/api/controller/AppointmentController.java | 35 ++++++++++++++++++++++ .../med/voll/api/controller/PatientController.java | 5 ++++ .../voll/api/domain/appointment/Appointment.java | 13 ++++++++ .../appointment/AppointmentDetailingData.java | 13 ++++++++ .../domain/appointment/AppointmentUpdateData.java | 12 ++++---- .../domain/appointment/AppointmentsSchedule.java | 25 ++++++++++++++++ .../voll/api/domain/patient/PatientRepository.java | 5 ++++ 7 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 api/src/main/java/med/voll/api/domain/appointment/AppointmentDetailingData.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/med/voll/api/controller/AppointmentController.java b/api/src/main/java/med/voll/api/controller/AppointmentController.java index e5a2ba4..1344f20 100644 --- a/api/src/main/java/med/voll/api/controller/AppointmentController.java +++ b/api/src/main/java/med/voll/api/controller/AppointmentController.java @@ -3,12 +3,23 @@ package med.voll.api.controller; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import jakarta.validation.Valid; import med.voll.api.domain.appointment.*; +import med.voll.api.domain.doctor.Doctor; +import med.voll.api.domain.doctor.DoctorDetailingData; +import med.voll.api.domain.doctor.DoctorListingData; +import med.voll.api.domain.doctor.DoctorUpdateData; +import med.voll.api.domain.patient.Patient; import org.springframework.beans.factory.annotation.Autowired; +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.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import org.springframework.web.util.UriComponentsBuilder; +import javax.print.Doc; + @RestController @RequestMapping("/appointments") @SecurityRequirement(name = "bearer-key") @@ -23,6 +34,23 @@ public class AppointmentController { return ResponseEntity.ok(dto); } + @GetMapping + public ResponseEntity> list(@PageableDefault(sort = {"date"}, direction = Sort.Direction.ASC) Pageable pagination) { + var page = appointmentsSchedule.list(pagination); + return ResponseEntity.ok(page); + } + + @PutMapping + @Transactional + public ResponseEntity update(@RequestBody @Valid AppointmentUpdateData data) throws Exception { + Appointment appointment = appointmentsSchedule.getReferenceById(data.id()); + Patient patient = appointmentsSchedule.getPatient(data.idPatient()); + Doctor doctor = appointmentsSchedule.getDoctor(data); + appointment.updateInformation(data, doctor, patient); + + return ResponseEntity.ok(new AppointmentListingData(appointment)); + } + @DeleteMapping @Transactional public ResponseEntity cancel(@RequestBody @Valid AppointmentDeletionData data) { @@ -30,5 +58,12 @@ public class AppointmentController { return ResponseEntity.noContent().build(); } + @GetMapping("/{id}") + public ResponseEntity detail(@PathVariable Long id) { + Appointment appointment = appointmentsSchedule.getReferenceById(id); + + return ResponseEntity.ok(new AppointmentListingData(appointment)); + } + } diff --git a/api/src/main/java/med/voll/api/controller/PatientController.java b/api/src/main/java/med/voll/api/controller/PatientController.java index 5675cfb..16c0973 100644 --- a/api/src/main/java/med/voll/api/controller/PatientController.java +++ b/api/src/main/java/med/voll/api/controller/PatientController.java @@ -2,6 +2,7 @@ package med.voll.api.controller; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import jakarta.validation.Valid; +import jakarta.validation.ValidationException; import med.voll.api.domain.patient.PatientListingData; import med.voll.api.domain.patient.Patient; import med.voll.api.domain.patient.PatientRepository; @@ -53,6 +54,10 @@ public class PatientController { @Transactional public ResponseEntity delete(@PathVariable Long id) { Patient patient = patientRepository.getReferenceById(id); + var appointments = patientRepository.findAnyAppointmentFrom(id); + if (appointments != null) { + throw new ValidationException("Can't delete patient with active appointments"); + } patient.delete(); return ResponseEntity.noContent().build(); diff --git a/api/src/main/java/med/voll/api/domain/appointment/Appointment.java b/api/src/main/java/med/voll/api/domain/appointment/Appointment.java index a48c15f..be236cf 100644 --- a/api/src/main/java/med/voll/api/domain/appointment/Appointment.java +++ b/api/src/main/java/med/voll/api/domain/appointment/Appointment.java @@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; import med.voll.api.domain.doctor.Doctor; import med.voll.api.domain.patient.Patient; +import javax.print.Doc; import java.time.LocalDateTime; @Table(name = "appointments") @@ -44,4 +45,16 @@ public class Appointment { this.reasonForCancellation = reasonForCancellation; this.active = false; } + + public void updateInformation(AppointmentUpdateData data, Doctor doctor, Patient patient) { + if (doctor != null) { + this.doctor = doctor; + } + if (patient != null) { + this.patient = patient; + } + if (data.date() != null) { + this.date = data.date(); + } + } } diff --git a/api/src/main/java/med/voll/api/domain/appointment/AppointmentDetailingData.java b/api/src/main/java/med/voll/api/domain/appointment/AppointmentDetailingData.java new file mode 100644 index 0000000..c4e1dc5 --- /dev/null +++ b/api/src/main/java/med/voll/api/domain/appointment/AppointmentDetailingData.java @@ -0,0 +1,13 @@ +package med.voll.api.domain.appointment; + +import med.voll.api.domain.doctor.Specialty; + +import java.time.LocalDate; +import java.time.LocalTime; + +public record AppointmentDetailingData(Long id, LocalDate date, LocalTime time, String doctorName, Specialty specialty, String patientName) { + + public AppointmentDetailingData(Appointment appointment) { + this(appointment.getId(), appointment.getDate().toLocalDate(), appointment.getDate().toLocalTime(), appointment.getDoctor().getName(), appointment.getDoctor().getSpecialty(), appointment.getPatient().getName()); + } +} diff --git a/api/src/main/java/med/voll/api/domain/appointment/AppointmentUpdateData.java b/api/src/main/java/med/voll/api/domain/appointment/AppointmentUpdateData.java index c13712c..ddcde78 100644 --- a/api/src/main/java/med/voll/api/domain/appointment/AppointmentUpdateData.java +++ b/api/src/main/java/med/voll/api/domain/appointment/AppointmentUpdateData.java @@ -2,18 +2,16 @@ package med.voll.api.domain.appointment; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import med.voll.api.domain.doctor.Specialty; import java.time.LocalDateTime; public record AppointmentUpdateData( @NotNull Long id, - Integer year, - Integer month, - Integer day, - Integer hour, - Integer minute, - String doctor, - String patient + LocalDateTime date, + Long idDoctor, + Long idPatient, + Specialty specialty ) { } diff --git a/api/src/main/java/med/voll/api/domain/appointment/AppointmentsSchedule.java b/api/src/main/java/med/voll/api/domain/appointment/AppointmentsSchedule.java index fd7d4e8..3ad069e 100644 --- a/api/src/main/java/med/voll/api/domain/appointment/AppointmentsSchedule.java +++ b/api/src/main/java/med/voll/api/domain/appointment/AppointmentsSchedule.java @@ -5,8 +5,11 @@ import med.voll.api.domain.appointment.validations.cancellation.AppointmentCance import med.voll.api.domain.appointment.validations.scheduling.AppointmentSchedulingValidator; import med.voll.api.domain.doctor.Doctor; import med.voll.api.domain.doctor.DoctorRepository; +import med.voll.api.domain.patient.Patient; import med.voll.api.domain.patient.PatientRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; @@ -49,6 +52,10 @@ public class AppointmentsSchedule { return new AppointmentListingData(appointment); } + public Page list(Pageable pagination) { + return appointmentRepository.findAllByActiveTrue(pagination).map(AppointmentDetailingData::new); + } + public void cancel(AppointmentDeletionData data) { if (!appointmentRepository.existsById(data.idAppointment())) { throw new ValidationException("Invalid appointment id"); @@ -72,4 +79,22 @@ public class AppointmentsSchedule { return doctorRepository.chooseRandomDoctorAvailable(data.specialty(), data.date()); } + public Appointment getReferenceById(Long id) { + return appointmentRepository.getReferenceById(id); + } + + public Doctor getDoctor(AppointmentUpdateData data) throws Exception { + if (data.idDoctor() != null) { + return doctorRepository.getReferenceById(data.idDoctor()); + } + Doctor doctor = doctorRepository.chooseRandomDoctorAvailable(data.specialty(), data.date()); + if (doctor == null) { + throw new ValidationException("No doctor available!"); + } + return doctor; + } + + public Patient getPatient(Long idPatient) { + return patientRepository.getReferenceById(idPatient); + } } diff --git a/api/src/main/java/med/voll/api/domain/patient/PatientRepository.java b/api/src/main/java/med/voll/api/domain/patient/PatientRepository.java index dbdcd87..c53d47a 100644 --- a/api/src/main/java/med/voll/api/domain/patient/PatientRepository.java +++ b/api/src/main/java/med/voll/api/domain/patient/PatientRepository.java @@ -15,4 +15,9 @@ public interface PatientRepository extends JpaRepository { where p.id = :id """) boolean findActiveById(Long id); + + @Query(""" + select p.id from Patient p join Appointment a on p.id = a.patient.id where a.active = true and p.id = :id + """) + Long findAnyAppointmentFrom(Long id); } -- cgit v1.2.3-18-g5258