Skip to content
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
2 changes: 1 addition & 1 deletion src/main/java/codesquad/ImsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(String[] args) {


/**
* Todo : Step4 ~ing
* Todo : Step5 ~ing
* 1. html 자원들 중복 제거하면서 스타일이 제대로 안나오는 부분이 있다, 추후 중복 제거 다시해서 레이아웃이 제대로 나오도록
* 2. 회원가입/가입정보수정 시 유효하지 않은 값 입력하고 submit 시 어떤 경우에 alert, 어떤 경우에 오류페이지 나오는지 확인 후 개선
*/
1 change: 1 addition & 0 deletions src/main/java/codesquad/domain/Milestone.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codesquad.domain;

import codesquad.domain.issue.Issue;
import org.hibernate.annotations.Where;
import support.domain.AbstractEntity;

Expand Down
110 changes: 110 additions & 0 deletions src/main/java/codesquad/domain/issue/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package codesquad.domain.issue;

import codesquad.CannotDeleteException;
import codesquad.UnAuthorizedException;
import codesquad.domain.ContentType;
import codesquad.domain.DeleteHistory;
import codesquad.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import support.domain.AbstractEntity;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Comment extends AbstractEntity {
private static final Logger log = LoggerFactory.getLogger(Comment.class);

@Size(min = 1)
@Lob
private String comment;

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "fk_comment_writer"))
private User writer;

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "fk_comment_issue"))
private Issue issue;

private boolean deleted = false;

public Comment() {

}

public Comment(User loginUser, Issue issue, String comment) {
this.writer = loginUser;
this.issue = issue;
this.comment = comment;
}

public Comment update(User loginUser, String body) {
if (!isMatchWriter(loginUser)) {
throw new UnAuthorizedException();
}

this.comment = body;
return this;
}

public List<DeleteHistory> delete(User loginUser) {
if (!isMatchWriter(loginUser)) {
throw new CannotDeleteException("작성자만 삭제 가능합니다.");
}
this.deleted = true;

List<DeleteHistory> temp = new ArrayList<>();
temp.add(new DeleteHistory(ContentType.COMMENT, getId(), writer));
return temp;
}

public boolean isMatchWriter(User loginUser) {
return this.writer.equals(loginUser);
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}

public User getWriter() {
return writer;
}

public void setWriter(User writer) {
this.writer = writer;
}

public Issue getIssue() {
return issue;
}

public void setIssue(Issue issue) {
this.issue = issue;
}

@Override
public String toString() {
return "Comment{" +
"id=" + getId() +
"comment='" + comment + '\'' +
", writer=" + writer +
", issue=" + issue +
'}';
}
}
11 changes: 11 additions & 0 deletions src/main/java/codesquad/domain/issue/CommentRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package codesquad.domain.issue;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {
Iterable<Comment> findByDeleted(boolean deleted);

List<Comment> findByIssue(long issueId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package codesquad.domain;
package codesquad.domain.issue;

import codesquad.CannotDeleteException;
import codesquad.UnAuthorizedException;
import codesquad.domain.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Where;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import support.domain.AbstractEntity;

import javax.persistence.*;
Expand All @@ -12,6 +17,8 @@

@Entity
public class Issue extends AbstractEntity {
private static final Logger log = LoggerFactory.getLogger(Issue.class);

@Size(min = 3, max = 100)
@Column(nullable = false, length = 100)
private String subject;
Expand All @@ -29,12 +36,18 @@ public class Issue extends AbstractEntity {
private User assignee;

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "fk_issue_to_milestone"))
@JoinColumn(foreignKey = @ForeignKey(name = "fk_issue_milestone"))
private Milestone milestone;

@ManyToMany
private List<Label> labels = new ArrayList<>();

@JsonIgnore
@OneToMany(mappedBy = "issue", cascade = CascadeType.ALL, orphanRemoval = true)
@Where(clause = "deleted = false")
@OrderBy("id DESC")
private List<Comment> comments = new ArrayList<>();

private boolean deleted = false;

private boolean closed = false;
Expand All @@ -53,10 +66,6 @@ public boolean isMatchWriter(User loginUser) {
return this.writer.equals(loginUser);
}

public boolean isClosed() {
return this.closed;
}

public void update(User loginUser, Issue target) {
if (!isMatchWriter(loginUser)) {
throw new UnAuthorizedException();
Expand All @@ -66,6 +75,7 @@ public void update(User loginUser, Issue target) {
this.comment = target.comment;
}

//TODO : comment 유무에 따른 delete 조건 변경 필요
public List<DeleteHistory> delete(User loginUser) {
if (!isMatchWriter(loginUser)) {
throw new CannotDeleteException("작성자만 삭제 가능합니다.");
Expand All @@ -77,15 +87,16 @@ public List<DeleteHistory> delete(User loginUser) {
return temp;
}

//TODO : 아래 4개 메소드 중복제거 가능할까?
public void toMilestone(User loginUser, Milestone milestone) {
if(!isMatchWriter(loginUser)) {
if (!isMatchWriter(loginUser)) {
throw new UnAuthorizedException();
}

this.milestone = milestone;
}

public void assignedBy(User loginUser, User assignee) {
public void toAssignee(User loginUser, User assignee) {
if (!isMatchWriter(loginUser)) {
throw new UnAuthorizedException();
}
Expand All @@ -94,13 +105,21 @@ public void assignedBy(User loginUser, User assignee) {
}

public void addLabel(User loginUser, Label label) {
if(!isMatchWriter(loginUser)) {
if (!isMatchWriter(loginUser)) {
throw new UnAuthorizedException();
}

this.labels.add(label);
}

public void changeOpeningAndClosingStatus(User loginUser) {
if (!isMatchWriter(loginUser)) {
throw new UnAuthorizedException();
}

this.closed = !closed;
}

public String getSubject() {
return subject;
}
Expand Down Expand Up @@ -133,6 +152,22 @@ public void setMilestone(Milestone milestone) {
this.milestone = milestone;
}

public boolean isClosed() {
return this.closed;
}

public void setClosed(boolean closed) {
this.closed = closed;
}

public List<Comment> getComments() {
return comments;
}

public void setComments(List<Comment> comments) {
this.comments = comments;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -158,4 +193,4 @@ public String toString() {
", writer=" + writer +
'}';
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package codesquad.domain;
package codesquad.domain.issue;

import org.springframework.data.jpa.repository.JpaRepository;

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/codesquad/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package codesquad.service;

import codesquad.CannotDeleteException;
import codesquad.domain.User;
import codesquad.domain.issue.Comment;
import codesquad.domain.issue.CommentRepository;
import codesquad.domain.issue.Issue;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;

@Service
public class CommentService {
@Resource(name = "issueService")
private IssueService issueService;

@Resource(name = "commentRepository")
private CommentRepository commentRepository;

@Resource(name = "deleteHistoryService")
private DeleteHistoryService deleteHistoryService;

public Comment findById(long id) {
return commentRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

public Comment add(Comment comment) {
return commentRepository.save(comment);
}

@Transactional
public Comment create(User loginUser, long issueId, String body) {
Issue issue = issueService.findById(issueId);
Comment comment = new Comment(loginUser, issue, body);
return add(comment);
}

@Transactional
public void delete(User loginUser, long id) throws CannotDeleteException {
Comment original = findById(id);
deleteHistoryService.saveAll(original.delete(loginUser));
}

@Transactional
public Comment update(User loginUser, long id, String body) {
Comment comment = commentRepository.findById(id).orElseThrow(NoResultException::new);
return comment.update(loginUser, body);
}
}
28 changes: 24 additions & 4 deletions src/main/java/codesquad/service/IssueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import codesquad.CannotDeleteException;
import codesquad.domain.*;
import codesquad.domain.issue.Comment;
import codesquad.domain.issue.Issue;
import codesquad.domain.issue.IssueRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.List;

@Service
public class IssueService {
private static final Logger log = LoggerFactory.getLogger(IssueService.class);

@Resource(name = "issueRepository")
private IssueRepository issueRepository;

Expand Down Expand Up @@ -63,26 +72,37 @@ public void delete(User loginUser, long id) throws CannotDeleteException {
}

@Transactional
public void addToMilestone(User loginUser, long id, long milestoneId) {
public Issue addToMilestone(User loginUser, long id, long milestoneId) {
Issue issue = findById(id);
Milestone milestone = milestoneService.findById(milestoneId);

milestone.addIssue(loginUser, issue);
return issue;
}

@Transactional
public void setAssignee(User loginUser, long id, long assigneeId) {
public Issue setAssignee(User loginUser, long id, long assigneeId) {
Issue issue = findById(id);
User assignee = userService.findById(assigneeId);

issue.assignedBy(loginUser, assignee);
issue.toAssignee(loginUser, assignee);
return issue;
}

@Transactional
public void addLabel(User loginUser, long id, long labelId) {
public Issue addLabel(User loginUser, long id, long labelId) {
Issue issue = findById(id);
Label label = labelRepository.findById(labelId).orElseThrow(EntityNotFoundException::new);

issue.addLabel(loginUser, label);
return issue;
}

@Transactional
public Issue changeOpeningAndClosingStatus(User loginUser, long id) {
Issue issue = findById(id);
issue.changeOpeningAndClosingStatus(loginUser);

return issue;
}
}
Loading