Skip to content

Commit

Permalink
[Feat] : 댓글 미리보기 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
wellbeing-dough committed Jan 23, 2024
1 parent d851d6b commit b17865e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -38,17 +39,25 @@ public ResponseEntity<HttpStatus> updateComment(@Valid @RequestBody final Update
}

@Operation(summary = "댓글 삭제")
@DeleteMapping("/v1/comments/{commentId}")
public ResponseEntity<HttpStatus> deleteComment(@PathVariable("commentId") final Long commentId, final UserId userId) {
@DeleteMapping("/v1/comments/{comment-id}")
public ResponseEntity<HttpStatus> deleteComment(@PathVariable("comment-id") final Long commentId, final UserId userId) {
commentService.deleteComment(commentId, userId.getId());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Operation(summary = "댓글 리스트 조회")
@GetMapping("/v1/comments/{postId}")
public ResponseEntity<Slice<CommentResponse>> getComments(@PathVariable("postId") final Long postId, @RequestParam final int page, @RequestParam final int size, final UserId userId) {
@GetMapping("/v1/comments/{post-id}")
public ResponseEntity<Slice<CommentResponse>> getComments(@PathVariable("post-id") final Long postId, @RequestParam final int page, @RequestParam final int size, final UserId userId) {
final Slice<CommentResponse> commentResponses = commentService.getComments(postId, page, size, userId.getId());
return ResponseEntity.ok().body(commentResponses);
}

@Operation(summary = "댓글 미리 보기")
@GetMapping("/v1/comments/{post-id}/preview")
public ResponseEntity<List<CommentResponse>> getPreviewComments(@PathVariable("post-id") final Long postId, final UserId userId) {
final List<CommentResponse> commentPreviewResponse = commentService.getPreviewComments(postId, userId.getId());
return ResponseEntity.ok().body(commentPreviewResponse);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface CommentRepositoryCustom {
List<CommentResponse> findSliceByPostIdWithUserId(Long postId, Long userId, Pageable pageable);

List<CommentResponse> findPreviewByPostId(Long postId, Long userId, Long commentPreviewCount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import kr.co.studyhubinu.studyhubserver.comment.dto.response.CommentResponse;
import kr.co.studyhubinu.studyhubserver.user.dto.data.UserData;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static kr.co.studyhubinu.studyhubserver.comment.domain.QCommentEntity.commentEntity;
import static kr.co.studyhubinu.studyhubserver.user.domain.QUserEntity.userEntity;
Expand Down Expand Up @@ -47,6 +50,34 @@ public List<CommentResponse> findSliceByPostIdWithUserId(Long postId, Long userI
.fetch();
}

@Override
public List<CommentResponse> findPreviewByPostId(Long postId, Long userId, Long commentPreviewCount) {
JPAQuery<CommentResponse> data = jpaQueryFactory
.select(Projections.constructor(CommentResponse.class,
commentEntity.id,
commentEntity.content,
commentEntity.createdDate,
loginPredicate(userId),
Projections.constructor(
UserData.class,
userEntity.id,
userEntity.major,
userEntity.nickname,
userEntity.imageUrl
)
))
.from(commentEntity)
.leftJoin(userEntity).on(commentEntity.userId.eq(userEntity.id))
.where(commentEntity.postId.eq(postId))
.orderBy(commentEntity.createdDate.desc())
.limit(commentPreviewCount);
data.fetch();

return data.stream()
.sorted(Comparator.comparing(CommentResponse::getCreatedDate))
.collect(Collectors.toList());
}

private BooleanExpression loginPredicate(Long userId) {
if (userId != null) {
return Expressions.booleanTemplate("{0} = {1}", commentEntity.userId, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class CommentService {

private static final Long COMMENT_PREVIEW_COUNT = 5L;

private final CommentRepository commentRepository;
private final CommentValidator commentValidator;
private final UserRepository userRepository;
Expand Down Expand Up @@ -73,4 +77,8 @@ private void validateStudyPostExist(Long postId) {
private CommentEntity findComment(Long commentId) {
return commentRepository.findById(commentId).orElseThrow(CommentNotFoundException::new);
}

public List<CommentResponse> getPreviewComments(Long postId, Long userId) {
return commentRepository.findPreviewByPostId(postId, userId, COMMENT_PREVIEW_COUNT);
}
}

0 comments on commit b17865e

Please sign in to comment.