package studycave.studycaverestservice.model.material;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MaterialRepository extends JpaRepository<Material, Long> {
    Material save(Material material);
    List<Material> findAll();


    @Query("select m from Material m where (:permission is null or m.permission = :permission) and "
            + "(:owner is null or m.owner = :owner) and m.group is null")
    List<Material> findByOptionalPermissionAndOptionalOwnerAndGroupIdNull(@Param("permission") String permission,
                                                                          @Param("owner") Integer owner);


    @Query("select t from Material t where (t.permission = 'GROUP' and t.group.id = :g and t.status = 'VERIFIED' )")
    List<Material> findMaterialByGroup(@Param("g") Integer g);

    @Query("select t from Material t where (t.permission = 'GROUP' and t.group.id = :g and t.status = 'UNVERIFIED' )")
    List<Material> findWaitingMaterialByGroupKey(@Param("g") Integer g);

}
