Skip to content

Commit

Permalink
[BE/#28] Feat : 이슈 상세페이지에서 코멘트 목록 출력 API 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
hanurii committed Jun 22, 2020
1 parent a916ec2 commit 6b2c054
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public List<Comment> findAllComments() {
);
}

public List<Comment> findAllCommentsByIssueId(Long issueId) {
String sql = "SELECT c.id, c.description, c.created_date_time, c.issue_id, c.user_id FROM comment c WHERE c.issue_id = ?";
return jdbcTemplate.query(
sql,
(rs,rowNum) ->
Comment.of(
rs.getLong("id"),
rs.getString("description"),
rs.getTimestamp("created_date_time").toLocalDateTime(),
rs.getLong("issue_id"),
rs.getLong("user_id"))
, issueId);
}

public void save(NewIssueDto newIssueDto) {
String sql =
"INSERT INTO comment(description, created_date_time, issue_id, user_id) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public List<Label> findAllLabels() {
);
}

public List<LabelSummary> findLabelSummaryByIssueId(Long issueId) {
public List<LabelSummary> findLabelSummariesByIssueId(Long issueId) {
return jdbcTemplate.query(
"SELECT label.id, label.name, label.background_color, label.color FROM label " +
"JOIN issue_has_label ON label.id = issue_has_label.label_id " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public List<User> findAllUsers() {
);
}

public List<UserSummary> findUserSummaryByIssueId(Long issueId) {
public List<UserSummary> findUserSummariesByIssueId(Long issueId) {
return jdbcTemplate.query(
"SELECT user.id, user.name, user.avatar_url FROM user " +
"JOIN assignee a ON user.id = a.user_id " +
Expand All @@ -58,4 +58,16 @@ public User findUserByUserId(Long userId) {
rs.getTimestamp("created_date_time").toLocalDateTime())
,userId);
}

public UserSummary findUserSummaryByIssueIdAndCommentId(Long issueId, Long commentId) {
return jdbcTemplate.queryForObject(
"SELECT user.id, user.name, user.avatar_url FROM user " +
"JOIN comment c ON user.id = c.user_id " +
"WHERE c.issue_id = ? AND c.id",
(rs, rowNum) ->
UserSummary.of(rs.getLong("id"),
rs.getString("name"),
rs.getString("avatar_url"))
, issueId, commentId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.codesquad.issuetracker.hamill.dto.response;

import com.codesquad.issuetracker.hamill.vo.UserVO.UserSummary;
import com.codesquad.issuetracker.hamill.vo.commentVO.CommentSummary;
import com.codesquad.issuetracker.hamill.vo.commentVO.CommentInformation;
import com.codesquad.issuetracker.hamill.vo.issueVO.IssueDetails;
import com.codesquad.issuetracker.hamill.vo.labelVO.LabelInformation;
import com.codesquad.issuetracker.hamill.vo.milestoneVO.MilestoneInformation;
Expand All @@ -12,26 +12,26 @@ public class IssueDto {

private IssueDetails issue;

private List<CommentSummary> comments;
private CommentInformation commentInfo;

private LabelInformation labelInfo;

private MilestoneInformation milestoneInfo;

private List<UserSummary> users;

private IssueDto(IssueDetails issue, List<CommentSummary> comments, LabelInformation labelInfo, MilestoneInformation milestoneInfo, List<UserSummary> users) {
private IssueDto(IssueDetails issue, CommentInformation commentInfo, LabelInformation labelInfo, MilestoneInformation milestoneInfo, List<UserSummary> users) {
this.issue = issue;
this.comments = comments;
this.commentInfo = commentInfo;
this.labelInfo = labelInfo;
this.milestoneInfo = milestoneInfo;
this.users = users;
}

public static IssueDto of(IssueDetails issue, List<CommentSummary> comments, LabelInformation labelInfo, MilestoneInformation milestoneInfo, List<UserSummary> users) {
public static IssueDto of(IssueDetails issue, CommentInformation commentInfo, LabelInformation labelInfo, MilestoneInformation milestoneInfo, List<UserSummary> users) {
return new Builder()
.issue(issue)
.comments(comments)
.commentInfo(commentInfo)
.labelInfo(labelInfo)
.milestoneInfo(milestoneInfo)
.users(users)
Expand All @@ -42,8 +42,8 @@ public IssueDetails getIssue() {
return issue;
}

public List<CommentSummary> getComments() {
return comments;
public CommentInformation getCommentInfo() {
return commentInfo;
}

public LabelInformation getLabelInfo() {
Expand All @@ -60,7 +60,7 @@ public List<UserSummary> getUsers() {

private static class Builder {
private IssueDetails issue;
private List<CommentSummary> comments;
private CommentInformation commentInfo;
private LabelInformation labelInfo;
private MilestoneInformation milestoneInfo;
private List<UserSummary> users;
Expand All @@ -73,8 +73,8 @@ public Builder issue(IssueDetails issue) {
return this;
}

public Builder comments(List<CommentSummary> comments) {
this.comments = comments;
public Builder commentInfo(CommentInformation commentInfo) {
this.commentInfo = commentInfo;
return this;
}

Expand All @@ -94,7 +94,7 @@ public Builder users(List<UserSummary> users) {
}

public IssueDto build() {
return new IssueDto(issue, comments, labelInfo, milestoneInfo, users);
return new IssueDto(issue, commentInfo, labelInfo, milestoneInfo, users);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
package com.codesquad.issuetracker.hamill.service;

import com.codesquad.issuetracker.hamill.dao.CommentDao_Hamill;
import com.codesquad.issuetracker.hamill.dao.IssueDao_Hamill;
import com.codesquad.issuetracker.hamill.domain.Comment;
import com.codesquad.issuetracker.hamill.domain.Issue;
import com.codesquad.issuetracker.hamill.domain.User;
import com.codesquad.issuetracker.hamill.dto.request.NewCommentDto;
import com.codesquad.issuetracker.hamill.dto.request.NewIssueDto;
import com.codesquad.issuetracker.hamill.vo.UserVO.UserSummary;
import com.codesquad.issuetracker.hamill.vo.commentVO.CommentInformation;
import com.codesquad.issuetracker.hamill.vo.commentVO.CommentSummary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

@Service
public class CommentService_Hamill {

private static final Logger logger = LoggerFactory.getLogger(CommentService_Hamill.class);

private final CommentDao_Hamill commentDao_hamill;
private final UserService_Hamill userService_hamill;

public CommentService_Hamill(CommentDao_Hamill commentDao_hamill) {
public CommentService_Hamill(CommentDao_Hamill commentDao_hamill, UserService_Hamill userService_hamill) {
this.commentDao_hamill = commentDao_hamill;
this.userService_hamill = userService_hamill;
}

public List<CommentSummary> findCommentSummaries() {
List<Comment> comments = commentDao_hamill.findAllComments();
List<CommentSummary> commentSummaries = new ArrayList<>();
public CommentInformation findCommentInformation(Issue issue) {
List<Comment> comments = commentDao_hamill.findAllCommentsByIssueId(issue.getId());

for (Comment value : comments) {
CommentSummary commentSummary = CommentSummary.of(value.getId(), value.getDescription(), value.getCreatedDateTime(), value.getUserId());
commentSummaries.add(commentSummary);
}
List<CommentSummary> commentSummaries = comments.stream()
.map(this::mapToUserSummaryInCommentSummary).collect(Collectors.toList());

return commentSummaries;
return CommentInformation.of(comments.size(), commentSummaries);
}

private CommentSummary mapToUserSummaryInCommentSummary(Comment comment) {
User user = userService_hamill.findUserByUserId(comment.getUserId());
UserSummary userSummary = UserSummary.of(user.getId(), user.getName(), user.getAvatarUrl());

return CommentSummary.of(
comment.getId(),
comment.getDescription(),
comment.getCreatedDateTime(),
userSummary
);
}

public Comment findCommentByIssueId(Long issueId) {
return commentDao_hamill.findCommentByIssueId(issueId);
}

// 이슈를 새로 생성할 때 comment 테이블에 데이터를 저장합니다
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.codesquad.issuetracker.hamill.dto.response.IssueDto;
import com.codesquad.issuetracker.hamill.dto.response.ListOfIssuesDto;
import com.codesquad.issuetracker.hamill.vo.UserVO.UserSummary;
import com.codesquad.issuetracker.hamill.vo.commentVO.CommentSummary;
import com.codesquad.issuetracker.hamill.vo.commentVO.CommentInformation;
import com.codesquad.issuetracker.hamill.vo.issueVO.IssueDetails;
import com.codesquad.issuetracker.hamill.vo.labelVO.LabelInformation;
import com.codesquad.issuetracker.hamill.vo.labelVO.LabelSummary;
Expand Down Expand Up @@ -48,14 +48,13 @@ public ListOfIssuesDto getIssuesAndAllElements() {
MilestoneInformation milestoneInfo = milestoneService_hamill.findMilestoneInformation();
List<UserSummary> userSummaries = userService_hamill.findUserSummaries();


return ListOfIssuesDto.of(issueDetails, labelInfo, milestoneInfo, userSummaries);
}

private IssueDetails mapToIssueDetails(Issue issue) {
Milestone milestone = milestoneService_hamill.findMilestoneByMilestoneId(issue.getMilestoneId());
List<LabelSummary> attachedLabels = labelService_hamill.findLabelSummaryByIssueId(issue.getId());
List<UserSummary> allocatedAssignees = userService_hamill.findUserSummaryByIssueId(issue.getId());
List<LabelSummary> attachedLabels = labelService_hamill.findLabelSummariesByIssueId(issue.getId());
List<UserSummary> allocatedAssignees = userService_hamill.findUserSummariesByIssueId(issue.getId());
User user = userService_hamill.findUserByUserId(issue.getUserId());

return IssueDetails.of(
Expand All @@ -69,24 +68,25 @@ private IssueDetails mapToIssueDetails(Issue issue) {
issue.isOpened());
}

@Transactional
public void save(NewIssueDto newIssueDto) {

issueDao_Hamill.save(newIssueDto);
commentService_hamill.save(newIssueDto);
}

public IssueDto getIssueAndAllElements(Long issueId) {
Issue issue = issueDao_Hamill.findIssueByIssueId(issueId);
IssueDetails issueDetail = mapToIssueDetails(issue);
List<CommentSummary> comments = commentService_hamill.findCommentSummaries();
CommentInformation commentInfo = commentService_hamill.findCommentInformation(issue);
LabelInformation labelInfo = labelService_hamill.findLabelInformation();
MilestoneInformation milestoneInfo = milestoneService_hamill.findMilestoneInformation();
List<UserSummary> userSummaries = userService_hamill.findUserSummaries();

return IssueDto.of(issueDetail, comments, labelInfo, milestoneInfo, userSummaries);
return IssueDto.of(issueDetail, commentInfo, labelInfo, milestoneInfo, userSummaries);
}

@Transactional
public void save(NewIssueDto newIssueDto) {

issueDao_Hamill.save(newIssueDto);
commentService_hamill.save(newIssueDto);
}



// public IssuesDto findIssueByIssueId(Long issueId) {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ public LabelService_Hamill(LabelDao_Hamill labelDao_hamill) {
public LabelInformation findLabelInformation() {
List<Label> labels = labelDao_hamill.findAllLabels();

// // 명령형 프로그래밍
// Set<LabelSummary> labelSummaries2 = new HashSet<>();
// for (Label value : labels) {
// LabelSummary labelSummary = LabelSummary.of(value.getId(), value.getName(), value.getHexCode());
// labelSummaries2.add(labelSummary);
// }

// 함수형 프로그래밍
List<LabelSummary> labelSummaries = labels.stream()
.map(label -> of(label.getId(), label.getName(), label.getBackgroundColor(), label.getColor()))
Expand All @@ -44,13 +37,7 @@ public LabelInformation findLabelInformation() {
return LabelInformation.of(labels.size(), labelSummaries);
}

public List<LabelSummary> findLabelSummaryByIssueId(Long issueId) {
return labelDao_hamill.findLabelSummaryByIssueId(issueId);
public List<LabelSummary> findLabelSummariesByIssueId(Long issueId) {
return labelDao_hamill.findLabelSummariesByIssueId(issueId);
}






}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public UserService_Hamill(UserDao_Hamill userDao_hamill) {
this.userDao_hamill = userDao_hamill;
}

public List<UserSummary> findUserInformation() {
public List<UserSummary> findUserSummaries() {
List<User> users = userDao_hamill.findAllUsers();
List<UserSummary> userSummaries = new ArrayList<>();

Expand All @@ -35,11 +35,15 @@ public List<UserSummary> findUserInformation() {
return userSummaries;
}

public List<UserSummary> findUserSummaryByIssueId(Long issueId) {
return userDao_hamill.findUserSummaryByIssueId(issueId);
public List<UserSummary> findUserSummariesByIssueId(Long issueId) {
return userDao_hamill.findUserSummariesByIssueId(issueId);
}

public User findUserByUserId(Long userId) {
return userDao_hamill.findUserByUserId(userId);
}

public UserSummary findUserSummaryByIssueIdAndCommentId(Long issueId, Long commentId) {
return userDao_hamill.findUserSummaryByIssueIdAndCommentId(issueId, commentId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.codesquad.issuetracker.hamill.vo.commentVO;

import com.codesquad.issuetracker.hamill.vo.UserVO.UserSummary;

import java.time.LocalDateTime;

public class CommentSummary {
Expand All @@ -10,21 +12,21 @@ public class CommentSummary {

private LocalDateTime createdAt;

private Long userId;
private UserSummary commenter;

private CommentSummary(Long id, String description, LocalDateTime createdAt, Long userId) {
private CommentSummary(Long id, String description, LocalDateTime createdAt, UserSummary commenter) {
this.id = id;
this.description = description;
this.createdAt = createdAt;
this.userId = userId;
this.commenter = commenter;
}

public static CommentSummary of(Long id, String description, LocalDateTime createdAt, Long userId) {
public static CommentSummary of(Long id, String description, LocalDateTime createdAt, UserSummary commenter) {
return new Builder()
.id(id)
.description(description)
.createdAt(createdAt)
.userId(userId)
.commenter(commenter)
.build();
}

Expand All @@ -40,15 +42,15 @@ public LocalDateTime getCreatedAt() {
return createdAt;
}

public Long getUserId() {
return userId;
public UserSummary getCommenter() {
return commenter;
}

private static class Builder {
private Long id;
private String description;
private LocalDateTime createdAt;
private Long userId;
private UserSummary commenter;

private Builder id(Long id) {
this.id = id;
Expand All @@ -65,13 +67,13 @@ private Builder createdAt(LocalDateTime createdAt) {
return this;
}

private Builder userId(Long userId) {
this.userId = userId;
private Builder commenter(UserSummary commenter) {
this.commenter = commenter;
return this;
}

private CommentSummary build() {
return new CommentSummary(id, description, createdAt, userId);
return new CommentSummary(id, description, createdAt, commenter);
}
}
}

0 comments on commit 6b2c054

Please sign in to comment.