CollectionRepo.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.Collection;
  3. import org.springframework.cache.annotation.CacheEvict;
  4. import org.springframework.cache.annotation.CachePut;
  5. import org.springframework.cache.annotation.Cacheable;
  6. import org.springframework.data.jpa.repository.JpaRepository;
  7. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  8. import org.springframework.data.jpa.repository.Modifying;
  9. import org.springframework.data.jpa.repository.Query;
  10. import javax.annotation.Nonnull;
  11. import javax.transaction.Transactional;
  12. import java.time.LocalDateTime;
  13. import java.util.List;
  14. import java.util.Optional;
  15. public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
  16. @Query("update Collection t set t.del = true where t.id = ?1")
  17. @Modifying
  18. @Transactional
  19. @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
  20. void softDelete(Long id);
  21. @Cacheable("collection")
  22. Optional<Collection> findById(Long id);
  23. Optional<Collection> findByIdAndDelFalse(Long id);
  24. @Query("update Collection t set t.likes = t.likes + ?2 where t.id = ?1")
  25. @Modifying
  26. @Transactional
  27. void addLike(Long id, int num);
  28. @Query(value = "select distinct c from Collection c join Like l on l.collectionId = c.id " +
  29. "where l.userId = ?1 and l.del = false and c.del = false")
  30. List<Collection> userLikes(Long userId);
  31. List<Collection> findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime time);
  32. @Nonnull
  33. @CachePut(value = "collection", key = "#collection.id")
  34. @CacheEvict(value = {"recommend"})
  35. Collection save(@Nonnull Collection collection);
  36. @Query("select c from Collection c join Recommend r on c.id = r.collectionId " +
  37. "where c.del = false and c.onShelf = true and r.type = ?1 order by r.sort desc")
  38. @Cacheable("recommend")
  39. List<Collection> recommend(String type);
  40. }