Files
MyINPulse/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/AppointmentService.java
Théo Le Lez aaa6e46d0c
Some checks failed
Format / formatting (push) Successful in 6s
Build / build (push) Failing after 39s
CI / build (push) Successful in 11s
fix (kinda) : refactored update of data, still trying to fix bug
from EntrepreneurApiServiceTests (code is a bit messy with prints and comments dw)
2025-04-06 19:22:18 +02:00

121 lines
4.7 KiB
Java

package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Appointment;
import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.repository.AppointmentRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;
@Service
public class AppointmentService {
private static final Logger logger = LogManager.getLogger(AppointmentService.class);
private AppointmentRepository appointmentRepository;
@Autowired
AppointmentService(AppointmentRepository appointmentRepository) {
this.appointmentRepository = appointmentRepository;
}
public Iterable<Appointment> getallAppointments() {
return this.appointmentRepository.findAll();
}
public Appointment getAppointmentById(Long id) {
Optional<Appointment> appointment = this.appointmentRepository.findById(id);
if (appointment.isEmpty()) {
logger.error("getAppointmentById : No appointment found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce rendez vous n'existe pas");
}
return appointment.get();
}
public Appointment addNewAppointment(Appointment appointment) {
return this.appointmentRepository.save(appointment);
}
public void deleteAppointmentById(Long id) {
this.appointmentRepository.deleteById(id);
}
public void updateAppointmentDate(long idAppointment, LocalDate date) {
Appointment appointment = getAppointmentById(idAppointment);
appointment.setAppointmentDate(date);
this.appointmentRepository.save(appointment);
}
public void updateAppointmentTime(long idAppointment, LocalTime time) {
Appointment appointment = getAppointmentById(idAppointment);
appointment.setAppointmentTime(time);
this.appointmentRepository.save(appointment);
}
public void updateAppointmentDuration(long idAppointment, LocalTime duration) {
Appointment appointment = getAppointmentById(idAppointment);
appointment.setAppointmentDuration(duration);
this.appointmentRepository.save(appointment);
}
public void updateAppointmentPlace(long idAppointment, String place) {
Appointment appointment = getAppointmentById(idAppointment);
appointment.setAppointmentPlace(place);
this.appointmentRepository.save(appointment);
}
public void updateAppointmentSubject(long idAppointment, String subject) {
Appointment appointment = getAppointmentById(idAppointment);
appointment.setAppointmentSubject(subject);
this.appointmentRepository.save(appointment);
}
public void updateAppointmentListSectionCell(long idAppointment, SectionCell sectionCell) {
Appointment appointment = getAppointmentById(idAppointment);
appointment.updateListSectionCell(sectionCell);
this.appointmentRepository.save(appointment);
}
public Appointment updateAppointment(
Long id,
LocalDate appointmentDate,
LocalTime appointmentTime,
LocalTime appointmentDuration,
String appointmentPlace,
String appointmentSubject,
SectionCell sectionCell) {
Optional<Appointment> appointment = this.appointmentRepository.findById(id);
if (appointment.isEmpty()) {
logger.error("updateAppointment : No appointment found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce rendez vous n'existe pas");
}
if (appointmentDate != null) {
appointment.get().setAppointmentDate(appointmentDate);
}
if (appointmentTime != null) {
appointment.get().setAppointmentTime(appointmentTime);
}
if (appointmentDuration != null) {
appointment.get().setAppointmentDuration(appointmentDuration);
}
if (appointmentPlace != null) {
appointment.get().setAppointmentPlace(appointmentPlace);
}
if (appointmentSubject != null) {
appointment.get().setAppointmentSubject(appointmentSubject);
}
if (sectionCell != null) {
appointment.get().updateListSectionCell(sectionCell);
}
return this.appointmentRepository.save(appointment.get());
}
}