package com.oguerreiro.resilient.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.oguerreiro.resilient.domain.ResilientLog;

/**
 * Spring Data JPA repository for the InputDataUploadLog entity.
 */
@Repository
public interface ResilientLogRepository extends ResilientJpaRepository<ResilientLog, Long> {
  /* Interface ResilientJpaRepository eager fetching Override methods */
  /* **************************************************************** */
  @Override
  @Query(value = "select resilientLog from ResilientLog resilientLog", countQuery = "select count(resilientLog) from ResilientLog resilientLog")
  Page<ResilientLog> findAllWithEagerRelationships(Pageable pageable);

  @Override
  @Query("select resilientLog from ResilientLog resilientLog")
  List<ResilientLog> findAllWithEagerRelationships();

  @Override
  @Query("select resilientLog from ResilientLog resilientLog where resilientLog.id =:id")
  Optional<ResilientLog> findOneWithEagerRelationships(@Param("id") Long id);

  /* Custom ResilientLogRepository methods */
  /* ******************************************* */
  Integer countByOwnerId(Long ownerId);

  @Query("select resilientLog from ResilientLog resilientLog where resilientLog.ownerId =:ownerId")
  List<ResilientLog> findAllByOwnerId(@Param("ownerId") Long ownerId);

  @Modifying
  @Query("delete from ResilientLog l where l.ownerId=:ownerId")
  void deleteAllByOwnerId(@Param("ownerId") Long ownerId);
}
