package com.izouma.nineth.repo;

import com.izouma.nineth.domain.News;
import com.izouma.nineth.dto.RecommendCollection;
import com.izouma.nineth.dto.RecommendNews;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface NewsRepo extends JpaRepository<News, Long>, JpaSpecificationExecutor<News> {
    @Query("update News t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    @Query("update News t set t.likes = t.likes + ?2 where t.id = ?1")
    @Modifying
    @Transactional
    @CacheEvict(value = "news", key = "#id")
    void addLike(Long id, int num);

    @Query("select new com.izouma.nineth.dto.RecommendNews(n,r) from News n join Recommend r on n.id = r.collectionId " +
                "where n.del = false and r.type = ?1 and r.category = 'NEWS' order by r.sort desc")
    List<RecommendNews> recommend(String type);

}