Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public interface AppFeedbackApi {
/** 피드백 목록. year/round로 필터링하고 페이지 단위로 조회한다. */
@Operation(summary = "피드백 목록 조회",
description = "연도(year)·회차(round)로 필터링해 열린피드백 목록을 페이지 단위로 조회한다. "
+ "year를 생략하면 전체 연도, round를 생략하면 그 안의 전체 회차를 대상으로 한다.")
+ "year를 생략하면 전체 연도, round를 생략하면 그 안의 전체 회차를 대상으로 한다. "
+ "답변(answer·answeredAt)은 답변을 공개한 회차에서만 내려가고, 공개 전이면 null이다.")
@ApiErrorCode(type = FeedbackErrorCode.class, codes = {"INVALID_FEEDBACK_ROUND"})
ApiResponse<PageResponse<FeedbackResponse>> getFeedbacks(
AppApiUser apiUser,
Expand All @@ -36,7 +37,8 @@ ApiResponse<PageResponse<FeedbackResponse>> getFeedbacks(
);

/** 피드백 상세. */
@Operation(summary = "피드백 상세 조회")
@Operation(summary = "피드백 상세 조회",
description = "답변(answer·answeredAt)은 답변을 공개한 회차에서만 내려가고, 공개 전이면 null이다.")
@ApiErrorCode(type = FeedbackErrorCode.class, codes = {"FEEDBACK_NOT_FOUND"})
ApiResponse<FeedbackResponse> getFeedback(AppApiUser apiUser, Long feedbackId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public boolean isAnswered() {
return answer != null;
}

// 답변(내용·시각·작성자)을 비운 사본. 원본은 바꾸지 않는다
public Feedback withoutAnswer() {
return new Feedback(id, year, round, question, questionedAt, null, null, createdBy, null);
}

// 답변을 작성한다. 이미 답변이 있으면 덮어쓴다 — 답변 수정이 같은 경로로 들어오기 때문이다
public void writeAnswer(String answer, Long answeredBy, LocalDateTime answeredAt) {
this.answer = answer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
*/
public interface FeedbackService {

// 답변을 공개하지 않은 회차의 질문은 답변을 비워서 돌려준다
Feedback getById(Long id);

// 답변을 공개하지 않은 회차의 질문은 답변을 비워서 돌려준다
PageResult<Feedback> search(FeedbackPeriod period, PageOffset pageOffset);

// 회차는 현재 열려 있는 회차로 서버가 자동 배정한다
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import kr.ac.kookmin.stream.common.BusinessException;
import kr.ac.kookmin.stream.common.DateUtil;
import kr.ac.kookmin.stream.common.PageOffset;
Expand All @@ -26,13 +28,17 @@ class FeedbackServiceImpl implements FeedbackService {

@Override
public Feedback getById(Long id) {
return feedbackRepository.findById(id)
Feedback feedback = feedbackRepository.findById(id)
.orElseThrow(() -> new BusinessException(FeedbackErrorCode.FEEDBACK_NOT_FOUND));
return hideUnpublishedAnswer(feedback, findPublishedPeriods(feedback.getYear()));
}

@Override
public PageResult<Feedback> search(FeedbackPeriod period, PageOffset pageOffset) {
return feedbackRepository.search(period.year(), period.round(), pageOffset);
// 한 페이지에 여러 회차가 섞일 수 있어 공개 여부를 질문마다 조회하지 않고 미리 한 번에 구한다
Set<FeedbackPeriod> publishedPeriods = findPublishedPeriods(period.year());
return feedbackRepository.search(period.year(), period.round(), pageOffset)
.map(feedback -> hideUnpublishedAnswer(feedback, publishedPeriods));
}

@Override
Expand All @@ -58,4 +64,20 @@ public List<Integer> getAvailableYears() {
public List<Integer> getRoundsOf(int year) {
return feedbackRoundRepository.findRoundsByYear(year);
}

// 학생에게는 공개된 회차의 답변만 보인다. 공개 전 답변은 운영진이 임시저장한 것이다
private static Feedback hideUnpublishedAnswer(Feedback feedback, Set<FeedbackPeriod> publishedPeriods) {
if (publishedPeriods.contains(FeedbackPeriod.of(feedback.getYear(), feedback.getRound()))) {
return feedback;
}
return feedback.withoutAnswer();
}

// year가 null이면 전체 연도. 회차는 연 5~10개라 연도 단위로 다 읽어도 부담이 없다
private Set<FeedbackPeriod> findPublishedPeriods(Integer year) {
return feedbackRoundRepository.findAllByYear(year).stream()
.filter(FeedbackRound::isAnswersPublished)
.map(round -> FeedbackPeriod.of(round.getYear(), round.getRound()))
.collect(Collectors.toSet());
}
}
Loading