-
Notifications
You must be signed in to change notification settings - Fork 308
Step1 - 레거시 코드 리팩터링 #804
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
Step1 - 레거시 코드 리팩터링 #804
Changes from all commits
263e542
b414d7f
06dc6e1
12e7e60
0c6c63a
51cd6d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import nextstep.qna.CannotDeleteException; | ||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Answers { | ||
| private final List<Answer> answers; | ||
|
|
||
| public Answers() { | ||
| this(new ArrayList<>()); | ||
| } | ||
|
|
||
| public Answers(Answer answer) { | ||
| this(List.of(answer)); | ||
| } | ||
|
|
||
| public Answers(List<Answer> answers) { | ||
| this.answers = answers; | ||
| } | ||
|
|
||
|
|
||
| public List<Answer> getAnswers() { | ||
| return this.answers; | ||
| } | ||
|
|
||
| public void add(Answer answer) { | ||
| this.answers.add(answer); | ||
| } | ||
|
|
||
| public void delete(NsUser loginUser) throws CannotDeleteException { | ||
| for (Answer answer : answers) { | ||
| answer.delete(loginUser); | ||
| } | ||
| } | ||
|
|
||
| public List<DeleteHistory> createDeleteHistories() { | ||
| List<DeleteHistory> deleteHistories = new ArrayList<>(); | ||
|
|
||
| for (Answer answer : answers) { | ||
| deleteHistories.add(answer.createDeleteHistory()); | ||
| } | ||
|
|
||
| return deleteHistories; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public abstract class BaseEntity { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| private Long id; | ||
|
|
||
| private LocalDateTime createdDate = LocalDateTime.now(); | ||
|
|
||
| private LocalDateTime updatedDate; | ||
|
|
||
| private boolean deleted = false; | ||
|
|
||
| public BaseEntity() {} | ||
|
|
||
| public BaseEntity(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| protected void markAsDeleted() { | ||
| this.deleted = true; | ||
| } | ||
|
|
||
| public boolean isDeleted() { | ||
| return deleted; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| public class PostContent { | ||
| private final NsUser writer; | ||
| private final String title; | ||
| private final String contents; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answer는 title이 없는데 이 객체를 사용해도 될까? |
||
|
|
||
| public PostContent(NsUser writer, String title, String contents) { | ||
| this.writer = writer; | ||
| this.title = title; | ||
| this.contents = contents; | ||
| } | ||
|
|
||
| public NsUser getWriter() { | ||
| return this.writer; | ||
| } | ||
|
|
||
| public boolean isOwner(NsUser loginUser) { | ||
| return writer.equals(loginUser); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,34 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import nextstep.qna.CannotDeleteException; | ||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Question { | ||
| private Long id; | ||
| public class Question extends BaseEntity { | ||
| private PostContent postContent; | ||
|
|
||
| private String title; | ||
|
|
||
| private String contents; | ||
|
|
||
| private NsUser writer; | ||
|
|
||
| private List<Answer> answers = new ArrayList<>(); | ||
|
|
||
| private boolean deleted = false; | ||
|
|
||
| private LocalDateTime createdDate = LocalDateTime.now(); | ||
|
|
||
| private LocalDateTime updatedDate; | ||
| private Answers answers = new Answers(); | ||
|
|
||
| public Question() { | ||
| } | ||
|
|
||
| public Question(NsUser writer, String title, String contents) { | ||
| this(0L, writer, title, contents); | ||
| this(0L, new PostContent(writer, title, contents)); | ||
| } | ||
|
|
||
| public Question(Long id, NsUser writer, String title, String contents) { | ||
| this.id = id; | ||
| this.writer = writer; | ||
| this.title = title; | ||
| this.contents = contents; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| this(id, new PostContent(writer, title, contents)); | ||
| } | ||
|
|
||
| public Question setTitle(String title) { | ||
| this.title = title; | ||
| return this; | ||
| } | ||
|
|
||
| public String getContents() { | ||
| return contents; | ||
| } | ||
|
|
||
| public Question setContents(String contents) { | ||
| this.contents = contents; | ||
| return this; | ||
| public Question(Long id, PostContent postContent) { | ||
| super(id); | ||
| this.postContent = postContent; | ||
| } | ||
|
|
||
| public NsUser getWriter() { | ||
| return writer; | ||
| return postContent.getWriter(); | ||
| } | ||
|
|
||
| public void addAnswer(Answer answer) { | ||
|
|
@@ -69,24 +37,45 @@ public void addAnswer(Answer answer) { | |
| } | ||
|
|
||
| public boolean isOwner(NsUser loginUser) { | ||
| return writer.equals(loginUser); | ||
| return postContent.isOwner(loginUser); | ||
| } | ||
|
|
||
| public Question setDeleted(boolean deleted) { | ||
| this.deleted = deleted; | ||
| return this; | ||
| public void markAsDeleted(NsUser loginUser) throws CannotDeleteException { | ||
| validateDeletableBy(loginUser); | ||
| markAsDeleted(); | ||
| } | ||
|
|
||
| public boolean isDeleted() { | ||
| return deleted; | ||
| public void delete(NsUser loginUser) throws CannotDeleteException { | ||
| this.markAsDeleted(loginUser); | ||
| answers.delete(loginUser); | ||
| } | ||
|
|
||
| public List<DeleteHistory> createDeleteHistories() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| List<DeleteHistory> histories = new ArrayList<>(); | ||
|
|
||
| histories.add(new DeleteHistory( | ||
| ContentType.QUESTION, | ||
| getId(), | ||
| getWriter(), | ||
| LocalDateTime.now() | ||
| )); | ||
|
|
||
| histories.addAll(answers.createDeleteHistories()); | ||
|
|
||
| return histories; | ||
| } | ||
|
|
||
| public List<Answer> getAnswers() { | ||
| return answers; | ||
| public void validateDeletableBy(NsUser loginUser) throws CannotDeleteException { | ||
| if (!isOwner(loginUser)) { | ||
| throw new CannotDeleteException("질문을 삭제할 권한이 없습니다."); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]"; | ||
| return "Question{" + | ||
| "postContent=" + postContent + | ||
| ", answers=" + answers + | ||
| '}'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일급 콜렉션 추가 👍