diff --git a/src/main/java/balancetalk/global/config/RedisConfig.java b/src/main/java/balancetalk/global/config/RedisConfig.java index 435f326ed..9d940e1f2 100644 --- a/src/main/java/balancetalk/global/config/RedisConfig.java +++ b/src/main/java/balancetalk/global/config/RedisConfig.java @@ -7,6 +7,9 @@ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableRedisRepositories @@ -27,6 +30,16 @@ public RedisConnectionFactory redisConnectionFactory() { public RedisTemplate redisTemplate() { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); + + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + redisTemplate.setDefaultSerializer(new StringRedisSerializer()); + redisTemplate.afterPropertiesSet(); return redisTemplate; } + + @Bean + public RedisSerializer springSessionDefaultRedisSerializer() { + return new GenericJackson2JsonRedisSerializer(); + } } diff --git a/src/main/java/balancetalk/global/exception/ErrorCode.java b/src/main/java/balancetalk/global/exception/ErrorCode.java index e7d0aa430..debc6c7c2 100644 --- a/src/main/java/balancetalk/global/exception/ErrorCode.java +++ b/src/main/java/balancetalk/global/exception/ErrorCode.java @@ -55,6 +55,7 @@ public enum ErrorCode { NOT_FOUND_MEMBER(NOT_FOUND, "존재하지 않는 회원입니다."), NOT_FOUND_VOTE(NOT_FOUND, "해당 게시글에서 투표한 기록이 존재하지 않습니다."), NOT_FOUND_BOOKMARK(NOT_FOUND, "해당 게시글에서 북마크한 기록이 존재하지 않습니다."), + NOT_FOUND_POST_LIKE(NOT_FOUND, "해당 게시글을 추천한 기록이 존재하지 않습니다. "), NOT_FOUND_COMMENT(NOT_FOUND, "존재하지 않는 댓글입니다."), NOT_FOUND_DIRECTORY(NOT_FOUND, "존재하지 않는 디렉토리입니다."), NOT_SUPPORTED_FILE_TYPE(NOT_FOUND, "지원하지 않는 파일 형식입니다."), diff --git a/src/main/java/balancetalk/global/jwt/JwtAuthenticationFilter.java b/src/main/java/balancetalk/global/jwt/JwtAuthenticationFilter.java index 02cf5f810..be150ed6f 100644 --- a/src/main/java/balancetalk/global/jwt/JwtAuthenticationFilter.java +++ b/src/main/java/balancetalk/global/jwt/JwtAuthenticationFilter.java @@ -28,9 +28,6 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha Authentication auth = jwtTokenProvider.getAuthentication(token); SecurityContextHolder.getContext().setAuthentication(auth); } - else if (token == null) { - throw new BalanceTalkException(ErrorCode.EMPTY_JWT_TOKEN); - } } catch (Exception e) { log.error("error={}", e.getMessage()); request.setAttribute("exception", e.getMessage()); diff --git a/src/main/java/balancetalk/module/comment/domain/Comment.java b/src/main/java/balancetalk/module/comment/domain/Comment.java index eb6798696..8be674fb5 100644 --- a/src/main/java/balancetalk/module/comment/domain/Comment.java +++ b/src/main/java/balancetalk/module/comment/domain/Comment.java @@ -46,6 +46,9 @@ public class Comment extends BaseTimeEntity { @JoinColumn(name = "parent_id") private Comment parent; + @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) + private List replies = new ArrayList<>(); + @OneToMany(mappedBy = "comment", cascade = CascadeType.ALL) private List likes = new ArrayList<>(); diff --git a/src/main/java/balancetalk/module/comment/dto/CommentResponse.java b/src/main/java/balancetalk/module/comment/dto/CommentResponse.java index 77e9f5d36..c0dc8f7f2 100644 --- a/src/main/java/balancetalk/module/comment/dto/CommentResponse.java +++ b/src/main/java/balancetalk/module/comment/dto/CommentResponse.java @@ -43,6 +43,9 @@ public class CommentResponse { @Schema(description = "추천 여부", example = "true") private boolean myLike; + @Schema(description = "답글 수", example = "3") + private int replyCount; + @Schema(description = "댓글 생성 날짜") private LocalDateTime createdAt; @@ -63,6 +66,7 @@ public static CommentResponse fromEntity(Comment comment, Long balanceOptionId, .likesCount(comment.getLikes().size()) //.reportedCount(comment.reportedCount()) .myLike(myLike) + .replyCount(comment.getReplies().size()) .createdAt(comment.getCreatedAt()) .lastModifiedAt(comment.getLastModifiedAt()) .profileImageUrl(getProfileImageUrl(comment.getMember())) diff --git a/src/main/java/balancetalk/module/post/application/PostService.java b/src/main/java/balancetalk/module/post/application/PostService.java index b1756e446..1669111d6 100644 --- a/src/main/java/balancetalk/module/post/application/PostService.java +++ b/src/main/java/balancetalk/module/post/application/PostService.java @@ -92,7 +92,7 @@ public Page findAll(String token, Pageable pageable) { member.hasVoted(post))); } - @Transactional(readOnly = true) + @Transactional public PostResponse findById(Long postId, String token) { Post post = getCurrentPost(postId); @@ -152,6 +152,7 @@ public Long likePost(Long postId) { throw new BalanceTalkException(ALREADY_LIKE_POST); } + PostLike postLike = PostLike.builder() .post(post) .member(member) @@ -164,12 +165,15 @@ public Long likePost(Long postId) { public void cancelLikePost(Long postId) { Post post = getCurrentPost(postId); Member member = getCurrentMember(memberRepository); - if (post.likesCount() == 0) { - throw new BalanceTalkException(ALREADY_CANCEL_LIKE_POST); + if (notExistsPostLikeBy(member, post)) { + throw new BalanceTalkException(NOT_FOUND_POST_LIKE); } postLikeRepository.deleteByMemberAndPost(member, post); } + private boolean notExistsPostLikeBy(Member member, Post post) { + return !postLikeRepository.existsByMemberAndPost(member, post); + } private Post getCurrentPost(Long postId) { return postRepository.findById(postId) .orElseThrow(() -> new BalanceTalkException(NOT_FOUND_POST)); diff --git a/src/main/java/balancetalk/module/post/domain/PostRepository.java b/src/main/java/balancetalk/module/post/domain/PostRepository.java index 185dc6dad..075da36cf 100644 --- a/src/main/java/balancetalk/module/post/domain/PostRepository.java +++ b/src/main/java/balancetalk/module/post/domain/PostRepository.java @@ -19,10 +19,10 @@ public interface PostRepository extends JpaRepository { List findByTitleContaining(String keyword); - @Query(value = "SELECT * " + - "FROM post p " + - "NATURAL JOIN post_tag pt " + - "NATURAL JOIN tag t " + - "WHERE p.post_id = pt.post_id AND pt.tag_id = t.tag_id AND t.name = :tagName", nativeQuery = true) + @Query("SELECT p " + + "FROM Post p " + + "JOIN p.postTags pt " + + "JOIN pt.tag t " + + "WHERE t.name = :tagName") List findByPostTagsContaining(String tagName); }