-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathAnswers.java
More file actions
37 lines (28 loc) · 999 Bytes
/
Answers.java
File metadata and controls
37 lines (28 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package nextstep.qna.domain;
import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Answers {
private final List<Answer> answers;
public Answers(List<Answer> answers) {
this.answers = new ArrayList<>(answers);
}
public void add(Answer answer) {
answers.add(answer);
}
public List<Answer> getAnswers() {
return answers;
}
public List<DeleteHistory> deleteAll(NsUser loginUser) {
if(!canAllDelete(loginUser)) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
return answers.stream().map(Answer::delete).collect(Collectors.toList());
}
private boolean canAllDelete(NsUser loginUser) {
return answers.stream().filter(answer -> answer.isOwner(loginUser)).count()
== answers.size();
}
}