LikeService.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.Like;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.repo.CollectionRepo;
  5. import com.izouma.nineth.repo.LikeRepo;
  6. import com.izouma.nineth.utils.JpaUtils;
  7. import lombok.AllArgsConstructor;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. @Service
  12. @AllArgsConstructor
  13. public class LikeService {
  14. private LikeRepo likeRepo;
  15. private CollectionRepo collectionRepo;
  16. public Page<Like> all(PageQuery pageQuery) {
  17. return likeRepo.findAll(JpaUtils.toSpecification(pageQuery, Like.class), JpaUtils.toPageRequest(pageQuery));
  18. }
  19. public void like(Long userId, Long collectionId) {
  20. List<Like> list = likeRepo.findByUserIdAndCollectionId(userId, collectionId);
  21. if (!list.isEmpty()) return;
  22. likeRepo.save(Like.builder()
  23. .userId(userId)
  24. .collectionId(collectionId)
  25. .build());
  26. collectionRepo.addLike(collectionId, 1);
  27. }
  28. public void unlike(Long userId, Long collectionId) {
  29. List<Like> list = likeRepo.findByUserIdAndCollectionId(userId, collectionId);
  30. if (!list.isEmpty()) {
  31. likeRepo.deleteAll(list);
  32. collectionRepo.addLike(collectionId, -list.size());
  33. }
  34. }
  35. }