| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package com.izouma.nineth.service;
- import com.izouma.nineth.domain.Like;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.repo.CollectionRepo;
- import com.izouma.nineth.repo.LikeRepo;
- import com.izouma.nineth.utils.JpaUtils;
- import lombok.AllArgsConstructor;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import java.util.List;
- @Service
- @AllArgsConstructor
- public class LikeService {
- private LikeRepo likeRepo;
- private CollectionRepo collectionRepo;
- public Page<Like> all(PageQuery pageQuery) {
- return likeRepo.findAll(JpaUtils.toSpecification(pageQuery, Like.class), JpaUtils.toPageRequest(pageQuery));
- }
- public void like(Long userId, Long collectionId) {
- List<Like> list = likeRepo.findByUserIdAndCollectionId(userId, collectionId);
- if (!list.isEmpty()) return;
- likeRepo.save(Like.builder()
- .userId(userId)
- .collectionId(collectionId)
- .build());
- collectionRepo.addLike(collectionId, 1);
- }
- public void unlike(Long userId, Long collectionId) {
- List<Like> list = likeRepo.findByUserIdAndCollectionId(userId, collectionId);
- if (!list.isEmpty()) {
- likeRepo.deleteAll(list);
- collectionRepo.addLike(collectionId, -list.size());
- }
- }
- }
|