Skip to content

Commit 9284c57

Browse files
authored
๐Ÿš€ 1๋‹จ๊ณ„ - ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ ๋ฆฌํŒฉํ„ฐ๋ง (#792)
* ์งˆ๋ฌธ ์‚ญ์ œํ•˜๊ธฐ : ๋„๋ฉ”์ธ ๋ชจ๋ธํ™” * ์งˆ๋ฌธ ์‚ญ์ œํ•˜๊ธฐ : ๊ทœ์น™7 - 3๊ฐœ์ด์ƒ์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ํด๋ž˜์Šค๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค
1 parent de587b9 commit 9284c57

File tree

15 files changed

+434
-178
lines changed

15 files changed

+434
-178
lines changed

โ€ŽStep.mdโ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ์งˆ๋ฌธ ์‚ญ์ œํ•˜๊ธฐ
2+
- ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž = ์งˆ๋ฌธํ•œ ์‚ฌ์šฉ์ž -> ์‚ญ์ œ ๊ฐ€๋Šฅ
3+
- ๋‹ต๋ณ€ ์—†์Œ -> ์‚ญ์ œ ๊ฐ€๋Šฅ
4+
- ์งˆ๋ฌธ ์‚ญ์ œ -> ๋‹ต๋ณ€ ์‚ญ์ œ : ๋ชจ๋‘ soft delete
5+
- ์งˆ๋ฌธ์ž์™€ ๋‹ต๋ณ€ ๊ธ€์˜ ๋ชจ๋“  ๋‹ต๋ณ€์ž๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ญ์ œ ๊ฐ€๋Šฅ
6+
- ์งˆ๋ฌธ์ž != ๋‹ต๋ณ€์ž -> ๋‹ต๋ณ€ ์‚ญ์ œ ๋ถˆ๊ฐ€๋Šฅ
7+
- ์งˆ๋ฌธ๊ณผ ๋‹ต๋ณ€ ์‚ญ์ œ ์ด๋ ฅ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ DeleteHistory๋ฅผ ํ™œ์šฉํ•ด ๋‚จ๊ธฐ๊ธฐ
8+
9+
## deleQuestion() ๋ฉ”์„œ๋“œ
10+
- ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ๋„๋ฉ”์ธ ๋ชจ๋ธ ๊ฐ์ฒด์— ๊ตฌํ˜„ํ•˜๊ธฐ
11+
- ๋„๋ฉ”์ธ ๋ชจ๋ธ๋กœ ์ด๋™ํ•˜๋Š” ๋ฆฌํŒฉํ„ฐ๋ง์„ ์ง„ํ–‰ํ•  ๋•Œ TDD๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

โ€Žsrc/main/java/nextstep/qna/domain/Answer.javaโ€Ž

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,62 @@
44
import nextstep.qna.UnAuthorizedException;
55
import nextstep.users.domain.NsUser;
66

7-
import java.time.LocalDateTime;
8-
9-
public class Answer {
10-
private Long id;
11-
12-
private NsUser writer;
13-
7+
public class Answer extends BaseEntity {
8+
private AnswerPost post;
149
private Question question;
10+
private boolean deleted;
1511

16-
private String contents;
17-
18-
private boolean deleted = false;
19-
20-
private LocalDateTime createdDate = LocalDateTime.now();
21-
22-
private LocalDateTime updatedDate;
23-
24-
public Answer() {
12+
public Answer(Long id, AnswerPost post, Question question, boolean deleted) {
13+
super(id);
14+
validateQuestion(question);
15+
this.post = post;
16+
this.question = question;
17+
this.deleted = deleted;
2518
}
2619

2720
public Answer(NsUser writer, Question question, String contents) {
2821
this(null, writer, question, contents);
2922
}
3023

3124
public Answer(Long id, NsUser writer, Question question, String contents) {
32-
this.id = id;
33-
if(writer == null) {
25+
this(id, new AnswerPost(writer, contents), question, false);
26+
validateWriter(writer);
27+
}
28+
29+
private static void validateWriter(NsUser writer) {
30+
if (writer == null) {
3431
throw new UnAuthorizedException();
3532
}
33+
}
3634

37-
if(question == null) {
35+
private static void validateQuestion(Question question) {
36+
if (question == null) {
3837
throw new NotFoundException();
3938
}
40-
41-
this.writer = writer;
42-
this.question = question;
43-
this.contents = contents;
44-
}
45-
46-
public Long getId() {
47-
return id;
48-
}
49-
50-
public Answer setDeleted(boolean deleted) {
51-
this.deleted = deleted;
52-
return this;
5339
}
5440

5541
public boolean isDeleted() {
5642
return deleted;
5743
}
5844

59-
public boolean isOwner(NsUser writer) {
60-
return this.writer.equals(writer);
45+
public boolean isOwner(NsUser user) {
46+
return post.isOwner(user);
6147
}
6248

6349
public NsUser getWriter() {
64-
return writer;
50+
return post.getWriter();
6551
}
6652

67-
public String getContents() {
68-
return contents;
53+
public void delete() {
54+
this.deleted = true;
6955
}
7056

71-
public void toQuestion(Question question) {
72-
this.question = question;
57+
public DeleteHistory createDeleteHistory() {
58+
return new DeleteHistory(ContentType.ANSWER, getId(), getWriter());
7359
}
7460

7561
@Override
7662
public String toString() {
77-
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]";
63+
return "Answer [id=" + getId() + ", writer=" + getWriter() + ", contents=" + post.getContents() + "]";
7864
}
7965
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package nextstep.qna.domain;
2+
3+
import nextstep.users.domain.NsUser;
4+
5+
public class AnswerPost {
6+
private final NsUser writer;
7+
private final String contents;
8+
9+
public AnswerPost(NsUser writer, String contents) {
10+
this.writer = writer;
11+
this.contents = contents;
12+
}
13+
14+
public boolean isOwner(NsUser user) {
15+
return writer.equals(user);
16+
}
17+
18+
public NsUser getWriter() {
19+
return writer;
20+
}
21+
22+
public String getContents() {
23+
return contents;
24+
}
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package nextstep.qna.domain;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import nextstep.qna.CannotDeleteException;
6+
import nextstep.users.domain.NsUser;
7+
8+
public class Answers {
9+
10+
private List<Answer> answers;
11+
12+
public Answers() {
13+
this(new ArrayList<>());
14+
}
15+
16+
public Answers(List<Answer> answers) {
17+
this.answers = new ArrayList<>(answers);
18+
}
19+
20+
public void create(Answer answer) {
21+
answers.add(answer);
22+
}
23+
24+
public void deleteBy(NsUser nsUser) throws CannotDeleteException {
25+
validateDeleteBy(nsUser);
26+
for (Answer answer : answers) {
27+
answer.delete();
28+
}
29+
}
30+
31+
private void validateDeleteBy(NsUser writer) throws CannotDeleteException {
32+
for (Answer answer : answers) {
33+
if (!answer.isOwner(writer)) {
34+
throw new CannotDeleteException("๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์“ด ๋‹ต๋ณ€์ด ์žˆ์–ด ์‚ญ์ œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
35+
}
36+
}
37+
}
38+
39+
public boolean allDeleted() {
40+
return answers.stream().allMatch(Answer::isDeleted);
41+
}
42+
43+
public boolean contains(Answer answer) {
44+
return answers.contains(answer);
45+
}
46+
47+
public List<DeleteHistory> createDeleteHistories() {
48+
List<DeleteHistory> deleteHistories = new ArrayList<>();
49+
for (Answer answer : answers) {
50+
if (answer.isDeleted()) {
51+
deleteHistories.add(answer.createDeleteHistory());
52+
}
53+
}
54+
return deleteHistories;
55+
}
56+
57+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package nextstep.qna.domain;
2+
3+
import java.time.LocalDateTime;
4+
5+
public abstract class BaseEntity {
6+
private Long id;
7+
private LocalDateTime createdDate = LocalDateTime.now();
8+
9+
protected BaseEntity() {
10+
}
11+
12+
protected BaseEntity(Long id) {
13+
this.id = id;
14+
}
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public LocalDateTime getCreatedDate() {
21+
return createdDate;
22+
}
23+
}

โ€Žsrc/main/java/nextstep/qna/domain/DeleteHistory.javaโ€Ž

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,42 @@
22

33
import nextstep.users.domain.NsUser;
44

5-
import java.time.LocalDateTime;
65
import java.util.Objects;
76

8-
public class DeleteHistory {
9-
private Long id;
10-
7+
public class DeleteHistory extends BaseEntity {
118
private ContentType contentType;
12-
139
private Long contentId;
14-
1510
private NsUser deletedBy;
1611

17-
private LocalDateTime createdDate = LocalDateTime.now();
18-
19-
public DeleteHistory() {
20-
}
21-
22-
public DeleteHistory(ContentType contentType, Long contentId, NsUser deletedBy, LocalDateTime createdDate) {
12+
public DeleteHistory(Long id, ContentType contentType, Long contentId, NsUser deletedBy) {
13+
super(id);
2314
this.contentType = contentType;
2415
this.contentId = contentId;
2516
this.deletedBy = deletedBy;
26-
this.createdDate = createdDate;
17+
}
18+
public DeleteHistory(ContentType contentType, Long contentId, NsUser deletedBy) {
19+
this(null, contentType, contentId, deletedBy);
2720
}
2821

2922
@Override
3023
public boolean equals(Object o) {
3124
if (this == o) return true;
3225
if (o == null || getClass() != o.getClass()) return false;
3326
DeleteHistory that = (DeleteHistory) o;
34-
return Objects.equals(id, that.id) &&
27+
return Objects.equals(getId(), that.getId()) &&
3528
contentType == that.contentType &&
3629
Objects.equals(contentId, that.contentId) &&
3730
Objects.equals(deletedBy, that.deletedBy);
3831
}
3932

4033
@Override
4134
public int hashCode() {
42-
return Objects.hash(id, contentType, contentId, deletedBy);
35+
return Objects.hash(getId(), contentType, contentId, deletedBy);
4336
}
4437

4538
@Override
4639
public String toString() {
47-
return "DeleteHistory [id=" + id + ", contentType=" + contentType + ", contentId=" + contentId + ", deletedBy="
48-
+ deletedBy + ", createdDate=" + createdDate + "]";
40+
return "DeleteHistory [id=" + getId() + ", contentType=" + contentType + ", contentId=" + contentId
41+
+ ", deletedBy=" + deletedBy + ", createdDate=" + getCreatedDate() + "]";
4942
}
5043
}

0 commit comments

Comments
ย (0)