FollowService.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.Follow;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.repo.FollowRepo;
  5. import com.izouma.nineth.repo.UserRepo;
  6. import com.izouma.nineth.utils.JpaUtils;
  7. import lombok.AllArgsConstructor;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.security.core.parameters.P;
  10. import org.springframework.stereotype.Service;
  11. import java.util.List;
  12. @Service
  13. @AllArgsConstructor
  14. public class FollowService {
  15. private FollowRepo followRepo;
  16. private UserRepo userRepo;
  17. public Page<Follow> all(PageQuery pageQuery) {
  18. return followRepo.findAll(JpaUtils.toSpecification(pageQuery, Follow.class), JpaUtils.toPageRequest(pageQuery));
  19. }
  20. public void follow(Long userId, Long to) {
  21. List<Follow> list = followRepo.findByUserIdAndFollowUserId(userId, to);
  22. if (!list.isEmpty()) return;
  23. followRepo.save(Follow.builder()
  24. .userId(userId)
  25. .followUserId(to)
  26. .build());
  27. userRepo.updateFollows(userId);
  28. userRepo.updateFollowers(to);
  29. }
  30. public void unfollow(Long userId, Long to) {
  31. List<Follow> list = followRepo.findByUserIdAndFollowUserId(userId, to);
  32. if (!list.isEmpty()) {
  33. followRepo.deleteAll(list);
  34. userRepo.updateFollows(userId);
  35. userRepo.updateFollowers(to);
  36. }
  37. }
  38. boolean isFollow(Long userId, Long to) {
  39. return followRepo.findByUserIdAndFollowUserId(userId, to).size() > 0;
  40. }
  41. }