Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
c4960b6
docs : ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ ๋ฌธ์„œ ์ž‘์„ฑ
chanani Nov 25, 2025
a117751
docs : ๋‚˜์˜ ํ•™์Šต ๋ชฉํ‘œ, PR์ „ ํ™•์ธํ•ด์•ผํ•  ๋ชฉ๋ก ์ž‘์„ฑ(ํ˜ธ์„ฑ๋‹˜์ด ์ž‘์„ฑํ•œ ๋‚ด์šฉ์— ๊ฐ๋ช… ๋ฐ›์•„ ์ €๋„ ๊ด€๋ฆฌํ•˜๋ ค๊ณ ํ•ฉ๋‹ˆ๋‹ค.)
chanani Nov 25, 2025
3cdb2ed
refactor : Question ๊ฐ์ฒด์— ์งˆ๋ฌธ ์‚ญ์ œ ๊ถŒํ•œ ์ฒดํฌ ๋ฉ”์„œ๋“œ ์ƒ์„ฑ ๋ฐ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ, Answer ๊ฐ์ฒด์— ์‚ญ์ œโ€ฆ
chanani Nov 25, 2025
9c9d031
refactor : Question ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(answer) ํ™œ์šฉํ•ด ์ง์ ‘ Answer ๊ฐ์ฒด์˜ validateAnsโ€ฆ
chanani Nov 25, 2025
625a895
refactor : ์งˆ๋ฌธ, ๋‹ต๋ณ€ ์‚ญ์ œ ๋กœ์ง Question ๊ฐ์ฒด๋กœ ์ด๋™(Deleted ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋กœ์ง์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด์„œ โ€ฆ
chanani Nov 25, 2025
188c9d7
refactor : Question ํด๋ž˜์Šค์˜ List<Answer> ๋ณ€์ˆ˜ ํฌ์žฅ
chanani Nov 25, 2025
8312ece
docs : ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋‚ด์šฉ Todo ์ถ”๊ฐ€
chanani Nov 26, 2025
f1b3547
refactor : ๊ณต๋™์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” createdDate, updatedDate, delted๋ฅผ BaseModel๋กœ ๋ถ„๋ฆฌ
chanani Nov 26, 2025
54e1f6d
refactor : title, contents ๊ฐ์ฒด ํฌ์žฅ
chanani Nov 26, 2025
c4db9bd
refactor : delete() ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ œ๊ฑฐ ๋ฐ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ˆ˜์ •
chanani Nov 26, 2025
400b772
refactor : ๊ฒ€์ฆํ•˜๋Š” ๋กœ์ง๊ณผ ์ƒํƒœ ๋ณ€๊ฒฝํ•˜๋Š” ๋กœ์ง์„ ํ•จ๊ป˜ ๊ตฌํ˜„,
chanani Nov 26, 2025
73effd2
feat : Answers ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ƒ์„ฑ
chanani Nov 26, 2025
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# ํ•™์Šต ๊ด€๋ฆฌ ์‹œ์Šคํ…œ(Learning Management System)

## ๋‹จ๊ณ„๋ณ„ ๋ฌธ์„œ
- [๐Ÿš€ 1๋‹จ๊ณ„ - ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ ๋ฆฌํŒฉํ„ฐ๋ง](./docs/01-refactoring.md)

## ์ง„ํ–‰ ๋ฐฉ๋ฒ•
* ํ•™์Šต ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์˜ ์ˆ˜๊ฐ•์‹ ์ฒญ ์š”๊ตฌ์‚ฌํ•ญ์„ ํŒŒ์•…ํ•œ๋‹ค.
* ์š”๊ตฌ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๊ตฌํ˜„์„ ์™„๋ฃŒํ•œ ํ›„ ์ž์‹ ์˜ github ์•„์ด๋””์— ํ•ด๋‹นํ•˜๋Š” ๋ธŒ๋žœ์น˜์— Pull Request(์ดํ•˜ PR)๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ ๋ฆฌ๋ทฐ ์š”์ฒญ์„ ํ•œ๋‹ค.
Expand Down
37 changes: 37 additions & 0 deletions docs/01-refactoring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ๐Ÿš€ 1๋‹จ๊ณ„ - ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ ๋ฆฌํŒฉํ„ฐ๋ง
***

## ์ฝ”๋“œ ๋ฆฌ๋ทฐ
> PR ๋งํฌ :
## ๋‚˜์˜ ํ•™์Šต ๋ชฉํ‘œ
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ‘


### 1. TDD ์‚ฌ์ดํด์„ ์˜์‹ํ•˜๋ฉฐ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ๋‹ค.
- `์‹คํŒจ โ†’ ์„ฑ๊ณต โ†’ ๋ฆฌํŒฉํ„ฐ๋ง` ๊ณผ์ •์œผ๋กœ ์ž‘์—…ํ•œ๋‹ค.
- ์Šต๊ด€์ฒ˜๋Ÿผ ์ด ๊ณผ์ •์„ ์ง€๋‚˜์น˜๋ฉด, ๋‹ค์‹œ ๋Œ์•„์™€ ์‚ฌ์ดํด์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
- TDD ์‚ฌ์ดํด ์Šต๊ด€์„ ๋งŒ๋“ค์ž.

### 2. ๋„๋ฉ”์ธ์— ํŠนํ™”๋œ ์ด๋ฆ„์„ ์ง“๋Š”๋‹ค.
- AI์—๊ฒŒ ์ถ”์ฒœ ๋ฐ›๋Š” ์Šต๊ด€์„ ๋งŒ๋“ค์ž.

### 3. ๋ฐ˜๋ณต๋œ ํ”ผ๋“œ๋ฐฑ์€ ํ”ผํ•˜์ž.
- ๊ฐ™์€ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋„๋ก, ์ฒดํฌ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ ๊ฒ€ํ•˜์ž.

## ์งˆ๋ฌธ ์‚ญ์ œํ•˜๊ธฐ ์š”๊ตฌ์‚ฌํ•ญ

- [x] ์งˆ๋ฌธ ๋ฐ์ดํ„ฐ๋ฅผ ์™„์ „ํžˆ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ์˜ ์ƒํƒœ๋ฅผ ์‚ญ์ œ ์ƒํƒœ(deleted - boolean type)๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
- [x] ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž์™€ ์งˆ๋ฌธํ•œ ์‚ฌ๋žŒ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์‚ญ์ œ ๊ฐ€๋Šฅํ•˜๋‹ค.
- [x] ๋‹ต๋ณ€์ด ์—†๋Š” ๊ฒฝ์šฐ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
- [x] ์งˆ๋ฌธ์ž์™€ ๋‹ต๋ณ€ ๊ธ€์˜ ๋ชจ๋“  ๋‹ต๋ณ€์ž๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
- [x] ์งˆ๋ฌธ์„ ์‚ญ์ œํ•  ๋•Œ ๋‹ต๋ณ€ ๋˜ํ•œ ์‚ญ์ œํ•ด์•ผ ํ•˜๋ฉฐ, ๋‹ต๋ณ€์˜ ์‚ญ์ œ ๋˜ํ•œ ์‚ญ์ œ ์ƒํƒœ(deleted)๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
- [x] ์งˆ๋ฌธ์ž์™€ ๋‹ต๋ณ€์ž๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ๋‹ต๋ณ€์„ ์‚ญ์ œํ•  ์ˆ˜ ์—†๋‹ค.
- [x] ์งˆ๋ฌธ๊ณผ ๋‹ต๋ณ€ ์‚ญ์ œ ์ด๋ ฅ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ DeleteHistory๋ฅผ ํ™œ์šฉํ•ด ๋‚จ๊ธด๋‹ค.

## ๋ฆฌํŒฉํ„ฐ๋ง ์š”๊ตฌ์‚ฌํ•ญ

- [x] nextstep.qna.service.QnaService์˜ deleteQuestion()๋Š” ์•ž์˜ ์งˆ๋ฌธ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์ด๋‹ค.
์ด ๋ฉ”์†Œ๋“œ๋Š” ๋‹จ์œ„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ์™€ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๊ฐ€ ์„ž์—ฌ ์žˆ๋‹ค.
- [x] QnaService์˜ deleteQuestion() ๋ฉ”์„œ๋“œ์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ(ํ•ต์‹ฌ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง)๋ฅผ ๋„๋ฉ”์ธ ๋ชจ๋ธ ๊ฐ์ฒด์— ๊ตฌํ˜„ํ•œ๋‹ค.
- [x] QnaService์˜ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง์„ ๋„๋ฉ”์ธ ๋ชจ๋ธ๋กœ ์ด๋™ํ•˜๋Š” ๋ฆฌํŒฉํ„ฐ๋ง์„ ์ง„ํ–‰ํ•  ๋•Œ TDD๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.
- [x] QnaService์˜ deleteQuestion() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋Š” src/test/java ํด๋” nextstep.qna.service.QnaServiceTest์ด๋‹ค.
๋„๋ฉ”์ธ ๋ชจ๋ธ๋กœ ๋กœ์ง์„ ์ด๋™ํ•œ ํ›„์—๋„ QnaServiceTest์˜ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ•ด์•ผ ํ•œ๋‹ค.
12 changes: 12 additions & 0 deletions docs/check-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# โœ”๏ธ PR ์ „ ํ™•์ธํ•ด์•ผ๋  ๋ชฉ๋ก
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ‘

***

- ๊ฐ์ฒด์ง€ํ–ฅ ์ƒํ™œ์ฒด์กฐ ์›์น™์„ ์ค€์ˆ˜ํ•˜์˜€๋Š”๊ฐ€ ?
- TDD ์‚ฌ์ดํด๋กœ ๊ตฌํ˜„ํ•˜์˜€๋Š”๊ฐ€ ?
- TDD ์‚ฌ์ดํด ๋‹จ์œ„๋กœ ์ปค๋ฐ‹ํ•˜์˜€๋Š”๊ฐ€ ?
- ๊ฐ์ฒด์—๊ฒŒ ์ƒํƒœ๋ฅผ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ  ์ฑ…์ž„์„ ๋งก๊ฒผ๋Š”๊ฐ€ ?
- ํ…Œ์Šฝ์œผ๋Š” ๊ฐ์ฒด์˜ ์ฑ…์ž„๊ณผ ๊ฒฐ๊ณผ๋งŒ ๊ฒ€์ฆํ–ˆ๋Š”๊ฐ€ ?
- ๋ถˆ๋ณ€์„ฑ์„ ์ตœ๋Œ€ํ•œ ์œ ์ง€ํ–ˆ๋Š”๊ฐ€ ?
- ๋„๋ฉ”์ธ ์šฉ์–ด๋กœ ๋„ค์ด๋ฐ ํ–ˆ๋Š”๊ฐ€ ?
- ํ˜‘๋ ฅ ๊ตฌ์กฐ๊ฐ€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ฝํžˆ๋Š”๊ฐ€ ?
- ๊ฐ™์€ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ˜๋ณตํ•˜์ง€๋Š” ์•Š์•˜๋Š”๊ฐ€ ?
15 changes: 13 additions & 2 deletions src/main/java/nextstep/qna/domain/Answer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextstep.qna.domain;

import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.UnAuthorizedException;
import nextstep.users.domain.NsUser;
Expand Down Expand Up @@ -30,11 +31,11 @@ public Answer(NsUser writer, Question question, String contents) {

public Answer(Long id, NsUser writer, Question question, String contents) {
this.id = id;
if(writer == null) {
if (writer == null) {
throw new UnAuthorizedException();
}

if(question == null) {
if (question == null) {
throw new NotFoundException();
}

Expand All @@ -52,6 +53,10 @@ public Answer setDeleted(boolean deleted) {
return this;
}

public void updateDeleted(){
this.deleted = true;
}

public boolean isDeleted() {
return deleted;
}
Expand All @@ -72,6 +77,12 @@ public void toQuestion(Question question) {
this.question = question;
}

public void validateAnswerOwner(NsUser loginUser) throws CannotDeleteException {
if (!this.isOwner(loginUser)) {
throw new CannotDeleteException("๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์“ด ๋‹ต๋ณ€์ด ์žˆ์–ด ์‚ญ์ œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜„์žฌ๋Š” valid์™€ updateDeleted๊ฐ€ ๋ถ„๋ฆฌ๋˜์–ด ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ.
์ด ๋‘˜์„ ํ•˜๋‚˜๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์•„๋ž˜์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ์–ด๋А ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์„๊นŒ?

Suggested change
public void delete(NsUser loginUser) throws CannotDeleteException {
// ์œ ํšจ์„ฑ ์ฒดํฌ์™€ ์‚ญ์ œ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ํ–ˆ๋Š”์ง€ ํ™•์ธ ํ•„์š”

@Override
public String toString() {
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]";
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/nextstep/qna/domain/Answers.java
Original file line number Diff line number Diff line change
@@ -0,0 +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 Answers {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ผ๊ธ‰ ์ฝœ๋ ‰์…˜ ์ ์šฉ ๐Ÿ‘


private List<Answer> answers = new ArrayList<>();

public void add(Answer answer) {
this.answers.add(answer);
}

public List<Answer> getAnswers() {
return answers;
}

public void validateAnswerOwner(NsUser loginUser) throws CannotDeleteException {
for (Answer answer : this.answers) {
answer.validateAnswerOwner(loginUser);
}
}

public void addDeleteAnswerHistory(List<DeleteHistory> deleteHistories) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void addDeleteAnswerHistory(List<DeleteHistory> deleteHistories) {
public List<DeleteHistory> toDeleteAnswerHistories() {
List<DeleteHistory> deleteHistories = new ArrayList<>();

๊ฐœ์ธ์ ์œผ๋กœ List ์ฝœ๋ ‰์…˜์„ ์ธ์ž๋กœ ์ „๋‹ฌํ•ด ์ €์žฅํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์œ„์™€ ๊ฐ™์ด ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ ์ง์ ‘ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ๋‹ค๋ฅธ ๊ฐ์ฒด์™€์˜ ์˜์กด ๊ด€๊ณ„๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„ ํ˜ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ž„

for (Answer answer : this.answers) {
answer.updateDeleted();
deleteHistories.add(new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()));
}
}
}
38 changes: 35 additions & 3 deletions src/main/java/nextstep/qna/domain/Question.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextstep.qna.domain;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;

import java.time.LocalDateTime;
Expand All @@ -15,7 +16,7 @@ public class Question {

private NsUser writer;

private List<Answer> answers = new ArrayList<>();
private Answers answers = new Answers();

private boolean deleted = false;

Expand Down Expand Up @@ -65,7 +66,7 @@ public NsUser getWriter() {

public void addAnswer(Answer answer) {
answer.toQuestion(this);
answers.add(answer);
this.answers.add(answer);
}

public boolean isOwner(NsUser loginUser) {
Expand All @@ -82,11 +83,42 @@ public boolean isDeleted() {
}

public List<Answer> getAnswers() {
return answers;
return answers.getAnswers();
}

public void validateDeletable(NsUser loginUser) throws CannotDeleteException {
validateOwner(loginUser);
answers.validateAnswerOwner(loginUser);
}

private void validateOwner(NsUser loginUser) throws CannotDeleteException {
if (!this.isOwner(loginUser)) {
throw new CannotDeleteException("์งˆ๋ฌธ์„ ์‚ญ์ œํ•  ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.");
}
}

public List<DeleteHistory> delete(long questionId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public List<DeleteHistory> delete(long questionId) {
public List<DeleteHistory> delete() {

questionid๋Š” Question์ด ๊ฐ€์ง€๊ณ  ์žˆ์Œ.
๊ตณ์ด ํ•„์š”์—†์ง€ ์•Š์„๊นŒ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public List<DeleteHistory> delete(long questionId) {
public void delete(NsUser loginUser) {

Answer ํ”ผ๋“œ๋ฐฑ๊ณผ ๊ฐ™์ด ํ˜„์žฌ ๋‘˜๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋Š” ๊ตฌํ˜„๊ณผ ์œ„์™€ ๊ฐ™์ด ์‚ญ์ œ ๊ถŒํ•œ ์ฒดํฌ์—ฌ๋ถ€์™€ deleted ์ƒํƒœ ๋ณ€๊ฒฝ ๋กœ์ง์„ ํ•จ๊ป˜ ๊ตฌํ˜„ํ•˜๋Š” ์œ„ ์ ‘๊ทผ ๋ฐฉ์‹ ์ค‘ ์–ด๋А ์ ‘๊ทผ ๋ฐฉ์‹์ด ์ข‹์„์ง€ ๊ณ ๋ฏผํ•ด ๋ณด๋ฉด ์–ด๋–จ๊นŒ?

// todo 2๊ฐœ์˜ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋А๊ปด์ง‘๋‹ˆ๋‹ค. deleted ์ƒํƒœ๋ฅผ Setter ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ๋ ์ง€ ๊ณ ๋ฏผ์ž…๋‹ˆ๋‹ค.
updateDeleted();

List<DeleteHistory> deleteHistories = new ArrayList<>();
deleteHistories.add(new DeleteHistory(ContentType.QUESTION, questionId, this.writer, LocalDateTime.now()));
addDeleteAnswerHistory(deleteHistories);

return deleteHistories;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜„์žฌ๋Š” delete()์—์„œ ์ƒํƒœ ๋ณ€๊ฒฝ๊ณผ List ์ƒ์„ฑ ํ›„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜„์žฌ ๊ตฌํ˜„๊ณผ delete()์™€ toDeleteHistories()์™€ ๊ฐ™์ด ์ƒํƒœ ๋ณ€๊ฒฝ๊ณผ List ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ๋‘ ๊ฐœ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ ์ค‘ ์–ด๋А ์ ‘๊ทผ ๋ฐฉ์‹์ด ์ข‹์„๊นŒ?

private void addDeleteAnswerHistory(List<DeleteHistory> deleteHistories) {
this.answers.addDeleteAnswerHistory(deleteHistories);
}

private void updateDeleted() {
this.deleted = true;
}

@Override
public String toString() {
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]";
}

}
22 changes: 2 additions & 20 deletions src/main/java/nextstep/qna/service/QnAService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service("qnaService")
Expand All @@ -26,24 +24,8 @@ public class QnAService {
@Transactional
public void deleteQuestion(NsUser loginUser, long questionId) throws CannotDeleteException {
Question question = questionRepository.findById(questionId).orElseThrow(NotFoundException::new);
if (!question.isOwner(loginUser)) {
throw new CannotDeleteException("์งˆ๋ฌธ์„ ์‚ญ์ œํ•  ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.");
}

List<Answer> answers = question.getAnswers();
for (Answer answer : answers) {
if (!answer.isOwner(loginUser)) {
throw new CannotDeleteException("๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์“ด ๋‹ต๋ณ€์ด ์žˆ์–ด ์‚ญ์ œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
}

List<DeleteHistory> deleteHistories = new ArrayList<>();
question.setDeleted(true);
deleteHistories.add(new DeleteHistory(ContentType.QUESTION, questionId, question.getWriter(), LocalDateTime.now()));
for (Answer answer : answers) {
answer.setDeleted(true);
deleteHistories.add(new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()));
}
question.validateDeletable(loginUser);
List<DeleteHistory> deleteHistories = question.delete(questionId);
deleteHistoryService.saveAll(deleteHistories);
}
}
18 changes: 18 additions & 0 deletions src/test/java/nextstep/qna/domain/AnswerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package nextstep.qna.domain;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUserTest;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

public class AnswerTest {
public static final Answer A1 = new Answer(NsUserTest.JAVAJIGI, QuestionTest.Q1, "Answers Contents1");
public static final Answer A2 = new Answer(NsUserTest.SANJIGI, QuestionTest.Q1, "Answers Contents2");

@Test
void ๋‹ค๋ฅธ_์‚ฌ์šฉ์ž๊ฐ€_์ž‘์„ฑํ•œ_๋‹ต๋ณ€์ด_์กด์žฌํ•˜๋ฉด_์—๋Ÿฌ๋ฐœ์ƒ(){
assertThrows(CannotDeleteException.class, () -> {
A1.validateAnswerOwner(NsUserTest.SANJIGI);
});
}

@Test
void ๋‹ต๋ณ€์‚ญ์ œ(){
A1.updateDeleted();
assertThat(A1.isDeleted()).isTrue();
}
}
24 changes: 24 additions & 0 deletions src/test/java/nextstep/qna/domain/QuestionTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
package nextstep.qna.domain;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUserTest;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

public class QuestionTest {
public static final Question Q1 = new Question(NsUserTest.JAVAJIGI, "title1", "contents1");
public static final Question Q2 = new Question(NsUserTest.SANJIGI, "title2", "contents2");

@Test
void ๊ธ€์“ด์ด๊ฐ€_๋กœ๊ทธ์ธํ•œ_์œ ์ €์™€_๊ฐ™์ง€์•Š์œผ๋ฉด_์—๋Ÿฌ๋ฐœ์ƒ(){
assertThrows(CannotDeleteException.class, () -> {
Q1.validateDeletable(NsUserTest.SANJIGI);
});
}

@Test
void ์งˆ๋ฌธ_๋‹ต๊ธ€_์‚ญ์ œ_๋ชฉ๋ก_์กฐํšŒ(){
List<DeleteHistory> deleteHistories = Q1.delete(1L);
assertThat(deleteHistories).hasSize(1);
}



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answers ์ผ๊ธ‰ ์ฝœ๋ ‰์…˜์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋„ ์ถ”๊ฐ€ํ•˜๋ฉด ์–ด๋–จ๊นŒ?


}