Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 댓글 목록 조회 #82

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package kr.codesquad.issuetracker.application;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kr.codesquad.issuetracker.domain.Comment;
import kr.codesquad.issuetracker.exception.ApplicationException;
import kr.codesquad.issuetracker.exception.ErrorCode;
import kr.codesquad.issuetracker.infrastructure.persistence.CommentRepository;
import kr.codesquad.issuetracker.presentation.response.CommentsResponse;
import kr.codesquad.issuetracker.presentation.response.Slice;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
Expand All @@ -31,4 +35,15 @@ public void modify(String modifiedComment, Integer commentId, Integer userId) {
comment.modifyContent(modifiedComment);
commentRepository.update(comment);
}

@Transactional(readOnly = true)
public Slice<CommentsResponse> getComments(Integer issueId, Integer cursor) {
if (!commentRepository.isExistCommentByIssueId(issueId)) {
return new Slice<>(List.of(), false, 0);
}
List<CommentsResponse> comments = commentRepository.findAll(issueId, cursor);
cursor = comments.get(comments.size() - 1).getId();
boolean hasMore = commentRepository.hasMoreComment(issueId, cursor);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List의 size가 10 미만이면 hasMore이 false인 것을 알 수 있지 않을까요??

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10의 배수이면 모를 수도 있을 것 같아요!

return new Slice<>(comments, hasMore, cursor);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.codesquad.issuetracker.infrastructure.persistence;

import java.util.List;
import java.util.Optional;

import javax.sql.DataSource;
Expand All @@ -12,6 +13,7 @@
import org.springframework.stereotype.Repository;

import kr.codesquad.issuetracker.domain.Comment;
import kr.codesquad.issuetracker.presentation.response.CommentsResponse;

@Repository
public class CommentRepository {
Expand Down Expand Up @@ -47,4 +49,37 @@ public void update(Comment comment) {
.addValue("commentId", comment.getId());
jdbcTemplate.update(sql, param);
}

public List<CommentsResponse> findAll(Integer issueId, Integer cursor) {
String sql = "SELECT c.id, u.login_id, u.profile_url, c.content, c.created_at "
+ "FROM comment c "
+ "JOIN user_account u ON c.user_account_id = u.id "
+ "WHERE c.issue_id = :issueId AND c.is_deleted = false AND c.id > :cursor LIMIT 10 ";
MapSqlParameterSource param = new MapSqlParameterSource()
.addValue("issueId", issueId)
.addValue("cursor", cursor);
return jdbcTemplate.query(sql, param, (rs, rownum) -> new CommentsResponse(
rs.getInt("id"),
rs.getString("login_id"),
rs.getString("profile_url"),
rs.getString("content"),
rs.getTimestamp("created_at").toLocalDateTime())
);
}

public boolean isExistCommentByIssueId(Integer issueId) {
String sql = "SELECT EXISTS (SELECT 1 FROM comment c JOIN issue i ON c.issue_id = i.id "
+ "WHERE c.issue_id = :issueId)";
MapSqlParameterSource param = new MapSqlParameterSource()
.addValue("issueId", issueId);
return jdbcTemplate.queryForObject(sql, param, boolean.class);
}

public boolean hasMoreComment(Integer issueId, Integer cursor) {
String sql = "SELECT EXISTS (SELECT 1 FROM comment WHERE issue_id = :issueId AND id > :cursor)";
MapSqlParameterSource param = new MapSqlParameterSource()
.addValue("issueId", issueId)
.addValue("cursor", cursor);
return jdbcTemplate.queryForObject(sql, param, boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import kr.codesquad.issuetracker.application.CommentService;
import kr.codesquad.issuetracker.presentation.auth.AuthPrincipal;
import kr.codesquad.issuetracker.presentation.request.CommentRequest;
import kr.codesquad.issuetracker.presentation.response.CommentsResponse;
import kr.codesquad.issuetracker.presentation.response.Slice;
import lombok.RequiredArgsConstructor;

@RequestMapping("/api/issues/{issueId}/comments")
Expand All @@ -36,4 +40,10 @@ public ResponseEntity<Void> modify(@RequestBody CommentRequest request, @PathVar
commentService.modify(request.getContent(), commentId, userId);
return ResponseEntity.status(HttpStatus.OK).build();
}

@GetMapping
public ResponseEntity<Slice<CommentsResponse>> commentList(@PathVariable Integer issueId,
@RequestParam Integer cursor) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적으로는 slice size를 SQL에서 고정하지않고 RequestParam으로(default 10) 줘도 좋을 것 같습니다.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋은 방법이네용

return ResponseEntity.status(HttpStatus.OK).body(commentService.getComments(issueId, cursor));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kr.codesquad.issuetracker.presentation.response;

import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CommentsResponse {

private Integer id;
private String username;
private String profileUrl;
private String content;
private LocalDateTime createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kr.codesquad.issuetracker.presentation.response;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Slice<T> {

private List<T> data;
private Boolean hasMore;
private Integer nextCursor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package kr.codesquad.issuetracker.application;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import kr.codesquad.issuetracker.ApplicationTest;
import kr.codesquad.issuetracker.domain.Issue;
import kr.codesquad.issuetracker.domain.UserAccount;
import kr.codesquad.issuetracker.infrastructure.persistence.IssueRepository;
import kr.codesquad.issuetracker.infrastructure.persistence.UserAccountRepository;
import kr.codesquad.issuetracker.presentation.response.CommentsResponse;
import kr.codesquad.issuetracker.presentation.response.Slice;

@ApplicationTest
class CommentServiceTest {

@Autowired
private CommentService commentService;
@Autowired
private IssueRepository issueRepository;
@Autowired
private UserAccountRepository accountRepository;

@DisplayName("댓글 목록 조회에 성공한다.")
@Test
void getCommentsTest() {
// given
accountRepository.save(new UserAccount("pieeee", "123123"));
issueRepository.save(new Issue("제목", "내용", true, 1, null));
for (int i = 0; i < 15; i++) {
commentService.register(1, "댓글", 1);
}

// when
Slice<CommentsResponse> comments = commentService.getComments(1, 0);

// then
assertAll(
() -> assertThat(comments.getHasMore()).isTrue(),
() -> assertThat(comments.getNextCursor()).isEqualTo(10),
() -> assertThat(comments.getData().size()).isEqualTo(10)
);
}

@DisplayName("해당 이슈에 아무 댓글이 없을 시 빈 리스트를 반환한다.")
@Test
void emptyCommentList() {
// given
accountRepository.save(new UserAccount("pieeee", "123123"));
issueRepository.save(new Issue("제목", "내용", true, 1, null));

// when
Slice<CommentsResponse> comments = commentService.getComments(1, 0);

// then
assertAll(
() -> assertThat(comments.getData()).isEmpty(),
() -> assertThat(comments.getHasMore()).isFalse(),
() -> assertThat(comments.getNextCursor()).isEqualTo(0)
);
}
}