Skip to content

Commit 1a67442

Browse files
committed
fix : delete error
1 parent 67ae5ce commit 1a67442

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

src/main/java/com/back/domain/cocktail/comment/repository/CocktailCommentRepository.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
@Repository
1111
public interface CocktailCommentRepository extends JpaRepository<CocktailComment, Long> {
1212

13-
List<CocktailComment> findTop10ByCocktailIdAndStatusOrderByIdDesc(
14-
Long cocktailId,
15-
CommentStatus status
13+
List<CocktailComment> findTop10ByCocktailIdAndStatusInOrderByIdDesc(
14+
Long cocktailId, List<CommentStatus> statuses
1615
);
1716

18-
List<CocktailComment> findTop10ByCocktailIdAndStatusAndIdLessThanOrderByIdDesc(
19-
Long cocktailId,
20-
CommentStatus status,
21-
Long lastId
17+
List<CocktailComment> findTop10ByCocktailIdAndStatusInAndIdLessThanOrderByIdDesc(
18+
Long cocktailId, List<CommentStatus> statuses, Long lastId
2219
);
2320

2421
boolean existsByCocktailIdAndUserIdAndStatusNot(Long cocktailId, Long id, CommentStatus status);

src/main/java/com/back/domain/cocktail/comment/service/CocktailCommentService.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.back.domain.cocktail.repository.CocktailRepository;
1010
import com.back.domain.post.comment.enums.CommentStatus;
1111
import com.back.domain.user.entity.User;
12+
import com.back.global.exception.UnauthorizedException;
1213
import com.back.global.rq.Rq;
1314
import lombok.RequiredArgsConstructor;
1415
import org.springframework.stereotype.Service;
@@ -46,6 +47,7 @@ public CocktailCommentResponseDto createCocktailComment(Long cocktailId, Cocktai
4647
.cocktail(cocktail)
4748
.user(user)
4849
.content(reqBody.content())
50+
.status(reqBody.status())
4951
.build();
5052

5153
return new CocktailCommentResponseDto(cocktailCommentRepository.save(cocktailComment));
@@ -54,17 +56,34 @@ public CocktailCommentResponseDto createCocktailComment(Long cocktailId, Cocktai
5456
// 칵테일 댓글 다건 조회 로직 (무한스크롤)
5557
@Transactional(readOnly = true)
5658
public List<CocktailCommentResponseDto> getCocktailComments(Long cocktailId, Long lastId) {
59+
User actor = rq.getActor(); // 서비스에서 호출 가능
60+
61+
if (actor == null) {
62+
throw new UnauthorizedException("로그인이 필요합니다.");
63+
}
64+
Long currentUserId = actor.getId();
65+
List<CocktailComment> comments;
66+
5767
if (lastId == null) {
58-
return cocktailCommentRepository.findTop10ByCocktailIdAndStatusOrderByIdDesc(cocktailId, CommentStatus.PUBLIC)
59-
.stream()
60-
.map(CocktailCommentResponseDto::new)
61-
.toList();
68+
comments = cocktailCommentRepository
69+
.findTop10ByCocktailIdAndStatusInOrderByIdDesc(cocktailId, List.of(CommentStatus.PUBLIC, CommentStatus.PRIVATE)
70+
);
6271
} else {
63-
return cocktailCommentRepository.findTop10ByCocktailIdAndStatusAndIdLessThanOrderByIdDesc(cocktailId, CommentStatus.PUBLIC, lastId)
64-
.stream()
65-
.map(CocktailCommentResponseDto::new)
66-
.toList();
72+
comments = cocktailCommentRepository
73+
.findTop10ByCocktailIdAndStatusInAndIdLessThanOrderByIdDesc(cocktailId, List.of(CommentStatus.PUBLIC, CommentStatus.PRIVATE),
74+
lastId);
6775
}
76+
77+
return comments.stream()
78+
.filter(comment ->{
79+
if(comment.getStatus() == CommentStatus.PUBLIC) return true;
80+
if(comment.getStatus() == CommentStatus.PRIVATE) {
81+
return comment.getUser().getId().equals(currentUserId);
82+
}
83+
return false;
84+
})
85+
.map(CocktailCommentResponseDto::new)
86+
.toList();
6887
}
6988

7089
// 칵테일 댓글 단건 조회 로직
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.back.global.exception;
2+
3+
public class UnauthorizedException extends ServiceException {
4+
5+
private static final int UNAUTHORIZED_CODE = 401;
6+
private static final String UNAUTHORIZED_MSG = "로그인이 필요합니다.";
7+
8+
public UnauthorizedException() {
9+
super(UNAUTHORIZED_CODE, UNAUTHORIZED_MSG);
10+
}
11+
12+
public UnauthorizedException(String msg) {
13+
super(UNAUTHORIZED_CODE, msg);
14+
}
15+
}

0 commit comments

Comments
 (0)