Entity

@Entity
@Getter
@NoArgsConstructor
public class PostLike {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private Long postId;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private boolean pLike;


    // 게시물 좋아요

    public PostLike (Long postId, String username, boolean pLike) {
        this.postId = postId;
        this.username = username;
        this.pLike = pLike;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

 

PostService

// 게시글 좋아요
    @Transactional
    public MsgResponseDto savePostLike(Long id, HttpServletRequest request) {
        String token = jwtUtil.resolveToken(request); // JWT 안에 있ㄴ는 정보를 담는 clams 객체
        Claims claims;

        Post post = postRepository.findById(id).orElseThrow(
                () -> new IllegalArgumentException("게시글이 존재하지 않습니다.")
        );

        if (token == null) {
            return null;
        }
        if (!jwtUtil.validateToken(token)) {
            throw new IllegalArgumentException("토큰이 유효하지 않습니다.");
        }
        claims = jwtUtil.getUserInfoFromToken(token);

        User user = userRepository.findByUsername(claims.getSubject()).orElseThrow(
                () -> new IllegalArgumentException("사용자가 존재하지 않습니다.")
        );

        Long likeCnt = postLikeRepository.findByPostIdAndUsername(id, user.getUsername());

        PostLike postLike = new PostLike(id, user.getUsername(), true);
        if (likeCnt == 0) {
            postLikeRepository.save(postLike);
        } else {
            if (postLikeRepository.checkPlike(id, user.getUsername())) {
               postLikeRepository.delete(postLike);
                return new MsgResponseDto("좋아요 취소", HttpStatusCode.valueOf(200));
            }
        }
        return new MsgResponseDto("좋아요", HttpStatusCode.valueOf(200));
    }

PostLikeRepository

    @Query("select count (pl) from PostLike pl where pl.postId = :postId and pl.pLike = true and pl.username = :username")
    Long findByPostIdAndUsername(@Param("postId") Long postId, @Param("username") String username);

    @Query("select pl.pLike from PostLike pl where pl.postId = :postId and pl.username = :username")
    boolean checkPlike(@Param("postId") Long postId, @Param("username") String username);

    @Query("select count (pl) from PostLike pl where pl.postId = :postId and pl.pLike = true")
    Long countPlike(@Param("postId") Long postId);

    @Query("delete from PostLike pl where pl.postId = :postId and pl.username = :username")
    boolean deletePLike(@Param("postId") Long postId, @Param("username") String username);

좋아요는 잘 진행이 됐는데 좋아요 취소하는 부분에서 DB에서도 POSTMAN에서도 삭제가 진행이 안되는 것이다

그래서 repository에 delete 쿼리문을 추가해봤지만 여전히 실패,,

ㅇ으아 문제를 못찾겠다ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ어엉어

 

디버깅으로 확인했더니 예외로 넘겨졌따.


PostService

 if (likeCnt == 0) {
            PostLike postLike = new PostLike(id, user.getUsername(), true);
            postLikeRepository.save(postLike);
            return new MsgResponseDto("좋아요", HttpStatusCode.valueOf(200));
        } else {
            PostLike postLike = postLikeRepository.findByPostIdAndUsername(id, user.getUsername());
            if (postLike.isPLike()) { //boolean 타입이기 때문에 get이 아니라 is로
                postLike.update(false);

                return new MsgResponseDto("좋아요 취소", HttpStatusCode.valueOf(200));
            } else {
                postLike.update(true);
                return new MsgResponseDto("좋아요", HttpStatusCode.valueOf(200));
            }
        }

PostLikeRepository

    @Query("select count (pl) from PostLike pl where pl.postId = :postId and pl.pLike = true and pl.username = :username")
    Long CntByPostIdAndUsername(@Param("postId") Long postId, @Param("username") String username);

    @Query("select pl from PostLike pl where pl.postId = :postId and pl.pLike = true and pl.username = :username")
    PostLike findByPostIdAndUsername(@Param("postId") Long postId, @Param("username") String username);

    @Query("select count (pl) from PostLike pl where pl.postId = :postId and pl.pLike = true")
    Long countPlike(@Param("postId") Long postId);

이렇게 바꾸었더니 드디어 성공했따!

여기서 중요한 것은 postLike.isPLike() 에서 boolean 타입이기 때문에 getPLike가 아니라  isPLike로 해야한다는 것이다!

왜냐면 boolean이 Fals랑 True 두가지 밖에 없기 때문이다!

+ Recent posts