-
Notifications
You must be signed in to change notification settings - Fork 247
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
π 1λ¨κ³ - λ κ±°μ μ½λ 리ν©ν°λ§ #284
Changes from all commits
666746f
ab24276
c8f0cf4
21c7185
4d6473d
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 |
---|---|---|
@@ -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; | ||
|
@@ -76,4 +77,11 @@ public void toQuestion(Question question) { | |
public String toString() { | ||
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]"; | ||
} | ||
|
||
public Answer delete(NsUser writer) throws CannotDeleteException { | ||
if (!this.isOwner(writer)) { | ||
throw new CannotDeleteException("λ€λ₯Έ μ¬λμ΄ μ΄ λ΅λ³μ΄ μμ΄ μμ ν μ μμ΅λλ€."); | ||
} | ||
return this.setDeleted(true); | ||
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. μκ±° κ·Όλ° μμ λ λμ§ μμκΉμ ? 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. π₯² μꡬμ¬νμ κΌ νμν μ½λλΌκ³ μκ°νλλ° 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. μμ μ΄μ°¨νΌ deleteκ° deleted μΈν ν΄μ£Όλ μν μ λ§‘κ² λμΌλκΉ setterλ μμ΄μ Έλ λμ§ μλ νλ μκΈ°μμ΅λλ€! 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. π‘!! μν!! λ§μ΅λλ€!! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package nextstep.qna.service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import nextstep.qna.domain.Answer; | ||
import nextstep.qna.domain.ContentType; | ||
import nextstep.qna.domain.DeleteHistory; | ||
import nextstep.qna.domain.DeleteHistoryRepository; | ||
import nextstep.qna.domain.Question; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
@@ -18,4 +23,17 @@ public class DeleteHistoryService { | |
public void saveAll(List<DeleteHistory> deleteHistories) { | ||
deleteHistoryRepository.saveAll(deleteHistories); | ||
} | ||
|
||
public void saveAll(Question question, List<Answer> answers) { | ||
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. Questionκ³Ό Answerκ° κ°κ°μ μμ λ μ΄λ ₯μ 리ν΄ν΄μ€λ€λ©΄ μ΄ λ©μλλ μλ§λ€μ΄μ Έλ λμ§ μμκΉμ ? 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. μ¬λ¬κ°μ§ λ°©λ²μ΄ μμκ² κ°μλ° μ λ 2κ°μ§ λ°©λ²μ μκ°νμ΅λλ€.
μ λ μ΄μ€μ λ λ²μ§Έ λ°©λ²μ ννμ΅λλ€. μ λ μ΄λ° μκ°μ κ°μ§κ³ ꡬνμ νλλ° νΉμ μ ν λκ»μ λ€λ₯Έ μμ΄λμ΄λ₯Ό κ°μ§κ³ κ³μ κ°μ? 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. μ ? μ€λͺ νμ 2λ² λ΄μ©μ μ κ° μλν λ°μ μ νν μΌμΉνλλ° κ΅¬νμ΄λ λ΄μ©μ΄ λ€λ₯Έκ±° κ°μμ? γ γ μ½κ° μκ°ν΄λ³΄λ©΄ .. μλ§ Questionμ μμ μ Answersμ μμ λ λ¬Άμ΄λκ³ DeleteHistoryλ λ³κ°λ‘ 보μ κ² κ°μλ°μ..! |
||
List<DeleteHistory> deleteHistories = new ArrayList<>(); | ||
deleteHistories.add( | ||
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriter(), | ||
LocalDateTime.now())); | ||
for (Answer answer : answers) { | ||
deleteHistories.add( | ||
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), | ||
LocalDateTime.now())); | ||
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. μ΄λ ₯ ν μ΄λΈμ λ³΄ν΅ μ¬νμ μ΄μ νΈλνΉμ μν΄ μ°Ύμ보λ λ°μ΄ν°μΈλ°, μ΄λ κ² λ§€λ² nowλ₯Ό νΈμΆνλ©΄ λ°λ¦¬μΈμ»¨λλ¨μλ‘ μκ°μ΄ λ¬λΌμ Έμ λμ€μ λμΌν μκ°μ μΌμ΄λ μμ λ€μ μ°Ύμ λ μ‘°κΈ μ΄λ €μμ§ μκ° μμ΄μ μΈλΆμμ μκ°μ μ£Όμ λ°μμ κ°μ μκ°μ μ¬μ©νλκ±Έ μΆμ²λ립λλ€! |
||
} | ||
this.saveAll(deleteHistories); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
package nextstep.qna.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.users.domain.NsUser; | ||
import nextstep.users.domain.NsUserTest; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
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"); | ||
|
||
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"); | ||
|
||
@DisplayName("λ΅λ³ λ±λ‘μμ λ‘κ·ΈμΈ μ μ κ° μΌμΉν λ μμΈλ₯Ό λμ§μ§ μμ΅λλ€.") | ||
@Test | ||
void doesNotTrowExceptionWhenDeleteLoginUserMatchedWithAnswerWriter() { | ||
NsUser writer = A1.getWriter(); | ||
assertThatNoException().isThrownBy(() -> A1.delete(writer)); | ||
assertThat(A1.isDeleted()).isTrue(); | ||
} | ||
|
||
@DisplayName("λ΅λ³ λ±λ‘μμ λ‘κ·ΈμΈ μ μ κ° μΌμΉνμ§ μμλ° μμ νλ €κ³ νλ©΄ μμΈλ₯Ό λμ§λλ€.") | ||
@Test | ||
void throwExceptionWhenDeleteLoginUserDoesNotMatchedWithAnswerWriter() { | ||
NsUser writer = A1.getWriter(); | ||
assertThatThrownBy(() -> A2.delete(writer)).isInstanceOf(CannotDeleteException.class); | ||
assertThat(A2.isDeleted()).isFalse(); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,35 @@ | ||
package nextstep.qna.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.users.domain.NsUser; | ||
import nextstep.users.domain.NsUserTest; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
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"); | ||
|
||
@DisplayName("μμ ν κΆνμ΄ μλ€λ©΄ μ§λ¬Έμ΄ μμ λ©λλ€.") | ||
@Test | ||
void canDeleteIfYouHavePermission() throws CannotDeleteException { | ||
NsUser writer = Q1.getWriter(); | ||
assertThatNoException().isThrownBy(() -> Q1.delete(writer)); | ||
assertThat(Q1.isDeleted()).isTrue(); | ||
} | ||
|
||
@DisplayName("μμ ν κΆνμ΄ μλ€λ©΄ μ§λ¬Έμ μμ ν μ μμ΅λλ€.") | ||
@Test | ||
void canNotDeleteIfYouDontHavePermission() throws CannotDeleteException { | ||
NsUser writer = Q1.getWriter(); | ||
assertThatThrownBy(() -> Q2.delete(writer)).isInstanceOf(CannotDeleteException.class); | ||
assertThat(Q2.isDeleted()).isFalse(); | ||
} | ||
|
||
} |
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.
λ©μλλ κ΅³μ΄ this μλΆμ¬λ λ κ²κ°μμ!