package edu.gjoko.schedlr.repositories; import edu.gjoko.schedlr.entity.Appointment; import edu.gjoko.schedlr.entity.Stakeholder; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @Repository public interface AppointmentRepository extends JpaRepository { @Query(value = "select ap from Appointment as ap " + "where ap.service.business.id = :businessId " + "and ap.appointmentStatus = 'NEW'") List getActiveAppointmentsByBusiness(Long businessId); @Query( value = "select ap from Appointment as ap " + "where ap.service.business.id = :businessId " + "and (" + "(ap.startTime between :startDate and :endDate) " + "or (ap.endTime between :startDate and :endDate) " + "or (:startDate = ap.startTime and ap.startTime = :endDate)" + "or (:startDate > ap.startTime and ap.endTime > :endDate)" + ")") List findBlockingAppointments(Long businessId, LocalDateTime startDate, LocalDateTime endDate); @Query(value = "select ap from Appointment as ap " + "where ap.service.business.owner.id = :businessOwnerId " + "and ap.startTime > :now ") List findFutureAppointmentsByBusinessOwnerId(Long businessOwnerId, LocalDateTime now); @Query(value = "select ap from Appointment as ap " + "where ap.customer.id = :customerId " + "and ap.startTime < :now") List findPastAppointmentsByCustomerId(Long customerId, LocalDateTime now); @Query(value = "select ap from Appointment as ap " + "where ap.service.business.owner.id = :businessOwnerId " + "and ap.startTime < :now ") List findPastAppointmentsByBusinessOwnerId(Long businessOwnerId, LocalDateTime now); @Query(value = "select ap from Appointment as ap " + "where ap.customer.id = :customerId " + "and ap.startTime > :now ") List findFutureAppointmentsByCustomerId(Long customerId, LocalDateTime now); Optional findAppointmentByIdAndCustomer_Id(Long id, Long customerId); Optional findAppointmentByIdAndService_Business_Owner_Id(Long id, Long ownerId); }