Théo Le Lez f9de5ed6bf
Some checks failed
Format / formatting (push) Failing after 6s
CI / build (push) Successful in 11s
feat: finished creating services from controllers, continued implementing entrepreneurServiceApi with some validation
2025-02-26 17:35:52 +01:00

80 lines
2.9 KiB
Java

package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Appointment;
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 Appointment updateAppointment(
Long id,
LocalDate appointmentDate,
LocalTime appointmentTime,
LocalTime appointmentDuration,
String appointmentPlace,
String appointmentSubject) {
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);
}
return this.appointmentRepository.save(appointment.get());
}
}