package org.codiki.core.repositories;

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

import org.codiki.core.entities.persistence.Category;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryRepository extends CrudRepository<Category, Long> {
	@Query("SELECT c FROM Category c LEFT JOIN FETCH c.listSubCategories WHERE c.id = :id")
	Optional<Category> findByIdWithSubCategories(@Param("id") Long id);

	@Query("SELECT DISTINCT(c) FROM Category c LEFT JOIN FETCH c.listSubCategories ORDER BY c.name")
	List<Category> findAllWithSubCategories();
}
