package de.techdev.trackr.domain.employee.vacation;

import de.techdev.trackr.domain.employee.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.Date;
import java.util.List;

/**
 * @author Moritz Schulze
 */
public interface VacationRequestRepository extends CrudRepository<VacationRequest, Long> {

    @Override
    @PostAuthorize("hasRole('ROLE_SUPERVISOR') or principal?.id == returnObject.employee.id")
    VacationRequest findOne(Long aLong);

    @Override
    @RestResource(exported = false)
    Iterable<VacationRequest> findAll();

    @PreAuthorize("hasRole('ROLE_SUPERVISOR') or principal?.id == #employee.id")
    List<VacationRequest> findByEmployeeOrderByStartDateAsc(@Param("employee") Employee employee);

    @PreAuthorize("hasRole('ROLE_SUPERVISOR')")
    List<VacationRequest> findByStatusOrderBySubmissionTimeAsc(@Param("status") VacationRequest.VacationRequestStatus status);

    @RestResource(exported = false)
    List<VacationRequest> findBySubmissionTimeBeforeAndStatus(Date date, VacationRequest.VacationRequestStatus status);

    /**
     * Find vacation requests of a certain status that overlap with a period.
     */
    @RestResource(exported = false)
    List<VacationRequest> findByStartDateBetweenOrEndDateBetweenAndStatus(
            Date startLower, Date startHigher,
            Date endLower, Date endHigher,
            VacationRequest.VacationRequestStatus status);
}

@@ -97,16 +97,70 @@
 ployee;%0A
+import org.springframework.data.jpa.repository.Query;%0A
 import o
@@ -1532,152 +1532,411 @@
     
-List%3CVacationRequest%3E findByStartDateBetweenOrEndDateBetweenAndStatus(%0A            Date startLower, Date startHigher,%0A            Date endLower,
+@Query(%22SELECT vr FROM VacationRequest vr WHERE (vr.startDate BETWEEN :startLower AND :endLower OR vr.endDate BETWEEN :startHigher AND :endHigher) AND vr.status = :status%22)%0A    List%3CVacationRequest%3E findByStartDateBetweenOrEndDateBetweenAndStatus(%0A            @Param(%22startLower%22) Date startLower, @Param(%22startHigher%22) Date startHigher,%0A            @Param(%22endLower%22) Date endLower, @Param(%22endHigher%22)
  Dat
@@ -1959,16 +1959,33 @@
         
+ @Param(%22status%22)
  Vacatio

add custom query for approved requests