-
Notifications
You must be signed in to change notification settings - Fork 308
1단계 - 레거시 코드 리팩터링 #810
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
Merged
Merged
1단계 - 레거시 코드 리팩터링 #810
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
34b449f
docs: 질문 삭제하기 기능 목록 작성
username0w d52ed7e
feat: 질문 삭제 권한 체크 로직을 도메인으로 이동
username0w 505ebb0
feat: 답변 삭제 권한 체크 로직을 도메인으로 이동
username0w 2a20b56
refactor: 질문, 답변 삭제 상태 변경 로직을 도메인으로 이동
username0w 88922da
refactor: 질문, 답변 삭제 시 삭제 이력 반환
username0w 8109451
feat: Answers 추가 및 deleteAll 메서드 구현
username0w 327afdb
refactor: Question의 Answer 컬렉션을 Answers로 전환하고 add 메서드 연결
username0w 80b02db
feat: QuestionContent 추가 및 단위 테스트
username0w d3f264e
refactor: Question 클래스 title/contents 필드를 QuestionContent로 변경
username0w 8643b28
feat: BaseTimeEntity, BaseEntity 클래스 생성
username0w a686182
refactor: Question 구조 개선 및 BaseTimeEntity/BaseEntity 적용
username0w 0d50ca6
refactor: Answer 클래스 BaseTimeEntity/BaseEntity 적용
username0w 9253673
refactor: setDeleted 를 markAsDeleted 로 변경
username0w cd6320e
refactor: 삭제 메서드 deleteWithHistory로 변경 및 책임 분리
username0w 9307cb1
refactor: BaseEntity를 SoftDeletableBaseEntity로 변경
username0w d9aee8d
refactor: markAsDeleted 접근 제어자를 protected로 변경
username0w f967844
refactor: 삭제 상태 변경과 삭제 히스토리 생성 책임 분리
username0w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import nextstep.qna.CannotDeleteException; | ||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| public class Answers { | ||
|
|
||
| private final List<Answer> answers; | ||
|
|
||
| public Answers(Answer... answers) { | ||
| this(new ArrayList<>(Arrays.asList(answers))); | ||
| } | ||
|
|
||
| public Answers(List<Answer> answers) { | ||
| this.answers = answers; | ||
| } | ||
|
|
||
| public List<Answer> answers() { | ||
| return Collections.unmodifiableList(new ArrayList<>(answers)); | ||
| } | ||
|
|
||
| public void add(Answer answer) { | ||
| answers.add(answer); | ||
| } | ||
|
|
||
| public void delete(NsUser user) throws CannotDeleteException { | ||
| for (Answer answer : answers) { | ||
| answer.delete(user); | ||
| } | ||
| } | ||
|
|
||
| public List<DeleteHistory> toDeleteHistories() throws CannotDeleteException { | ||
| List<DeleteHistory> deleteHistories = new ArrayList<>(); | ||
| for (Answer answer : answers) { | ||
| deleteHistories.addAll(answer.toDeleteHistories()); | ||
| } | ||
| return deleteHistories; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public abstract class BaseTimeEntity { | ||
|
|
||
| private LocalDateTime createdDate = LocalDateTime.now(); | ||
|
|
||
| private LocalDateTime updatedDate; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,92 +1,60 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import nextstep.qna.CannotDeleteException; | ||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| public class Question { | ||
| private Long id; | ||
|
|
||
| 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; | ||
| public class Question extends SoftDeletableBaseEntity { | ||
| private QuestionContent content; | ||
|
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. 👍 |
||
|
|
||
| public Question() { | ||
| } | ||
| private Answers answers = new Answers(); | ||
|
|
||
| public Question(NsUser writer, String title, String contents) { | ||
| this(0L, writer, title, contents); | ||
| public Question(NsUser writer, QuestionContent content) { | ||
| this(0L, writer, content); | ||
| } | ||
|
|
||
| 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 Question(Long id, NsUser writer, QuestionContent content) { | ||
| super(id, writer); | ||
| this.content = content; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
|
|
||
| public Question setTitle(String title) { | ||
| this.title = title; | ||
| return this; | ||
| return content.title(); | ||
| } | ||
|
|
||
| public String getContents() { | ||
| return contents; | ||
| } | ||
|
|
||
| public Question setContents(String contents) { | ||
| this.contents = contents; | ||
| return this; | ||
| } | ||
|
|
||
| public NsUser getWriter() { | ||
| return writer; | ||
| return content.contents(); | ||
| } | ||
|
|
||
| public void addAnswer(Answer answer) { | ||
| answer.toQuestion(this); | ||
| answers.add(answer); | ||
| } | ||
|
|
||
| public boolean isOwner(NsUser loginUser) { | ||
| return writer.equals(loginUser); | ||
| } | ||
|
|
||
| public Question setDeleted(boolean deleted) { | ||
| this.deleted = deleted; | ||
| return this; | ||
| public Answers getAnswers() { | ||
| return answers; | ||
| } | ||
|
|
||
| public boolean isDeleted() { | ||
| return deleted; | ||
| public void delete(NsUser user) throws CannotDeleteException { | ||
| if (!isOwner(user)) { | ||
| throw new CannotDeleteException("질문을 삭제할 권한이 없습니다."); | ||
| } | ||
| markAsDeleted(); | ||
| answers.delete(user); | ||
| } | ||
|
|
||
| public List<Answer> getAnswers() { | ||
| return answers; | ||
| public List<DeleteHistory> toDeleteHistories() throws CannotDeleteException { | ||
| List<DeleteHistory> deleteHistories = new ArrayList<>(); | ||
| deleteHistories.add( | ||
| new DeleteHistory(ContentType.QUESTION, getId(), getWriter(), LocalDateTime.now())); | ||
| deleteHistories.addAll(answers.toDeleteHistories()); | ||
| return deleteHistories; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]"; | ||
| return "Question [id=" + getId() + ", content=" + content + ", writer=" + getWriter() + "]"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| public class QuestionContent { | ||
|
|
||
| private final String title; | ||
|
|
||
| private final String contents; | ||
|
|
||
| public QuestionContent(String title, String contents) { | ||
| this.title = title; | ||
| this.contents = contents; | ||
| } | ||
|
|
||
| public String title() { | ||
| return title; | ||
| } | ||
|
|
||
| public String contents() { | ||
| return contents; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "QuestionContent{" + | ||
| ", title=" + title + ", contents=" + contents + | ||
| '}'; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/nextstep/qna/domain/SoftDeletableBaseEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package nextstep.qna.domain; | ||
|
|
||
| import nextstep.users.domain.NsUser; | ||
|
|
||
| public abstract class SoftDeletableBaseEntity extends BaseTimeEntity { | ||
|
|
||
| private Long id; | ||
|
|
||
| private NsUser writer; | ||
|
|
||
| private boolean deleted = false; | ||
|
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. soft delete로 삭제하는 컨텐츠의 경우 상속하는 의미로 BaseEntity보다 SoftDeletableBaseEntity와 같은 이름으로 구현하는 것은 어떨까? |
||
|
|
||
| protected SoftDeletableBaseEntity(Long id, NsUser writer) { | ||
| this.id = id; | ||
| this.writer = writer; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public NsUser getWriter() { | ||
| return writer; | ||
| } | ||
|
|
||
| protected void markAsDeleted() { | ||
| this.deleted = true; | ||
| } | ||
|
|
||
| public boolean isOwner(NsUser loginUser) { | ||
| return writer.equals(loginUser); | ||
| } | ||
|
|
||
| public boolean isDeleted() { | ||
| return deleted; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
일급 콜렉션 👍