Skip to content
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

Feature/1474 키워드의 퀴즈 목록을 조회할 때 각 퀴즈 별로 학습 여부를 알 수 있다 #1509

Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class QuizStepDefinitions extends AcceptanceSteps {

//:todo keyword 와 합친 이후에 작성 예정
@When("퀴즈를 생성하면")
public void 퀴즈를생성하면(Long sessionId, Long keywordId) {
public void 퀴즈를_생성하면(Long sessionId, Long keywordId) {
final QuizRequest 수달이_만든_테스트_퀴즈 = new QuizRequest("수달이 만든 테스트 퀴즈");
context.invokeHttpPost("", 수달이_만든_테스트_퀴즈);
}

@Then("퀴즈가 추가된다")
public void 강의를추가된다() {
public void 퀴즈가_추가된다() {
assertThat(context.response.statusCode()).isEqualTo(HttpStatus.CREATED.value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class QuizDocumentation extends NewDocumentation {

@Test
void Keyword별_Quiz_목록_조회() {
given(quizService.findQuizzesByKeywordId(any())).willReturn(QUIZZES_RESPONSE);
given(quizService.findQuizzesByKeywordId(any(), any())).willReturn(QUIZZES_RESPONSE);

given
.contentType(MediaType.APPLICATION_JSON_VALUE)
Expand Down Expand Up @@ -82,6 +82,7 @@ public class QuizDocumentation extends NewDocumentation {
);

private static final QuizzesResponse QUIZZES_RESPONSE = new QuizzesResponse(1L,
Arrays.asList(new QuizResponse(1L, "브라운을 위해 낸 퀴즈"), new QuizResponse(1L, "포코를 위해 낸 퀴즈")));
Arrays.asList(new QuizResponse(1L, "브라운을 위해 낸 퀴즈", true),
new QuizResponse(1L, "포코를 위해 낸 퀴즈", false)));

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package wooteco.prolog.roadmap.application;

import static java.util.Objects.isNull;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_ORDER_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_QUIZ_NOT_FOUND_EXCEPTION;

import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,6 +15,7 @@
import wooteco.prolog.roadmap.application.dto.QuizzesResponse;
import wooteco.prolog.roadmap.domain.Keyword;
import wooteco.prolog.roadmap.domain.Quiz;
import wooteco.prolog.roadmap.domain.repository.EssayAnswerRepository;
import wooteco.prolog.roadmap.domain.repository.KeywordRepository;
import wooteco.prolog.roadmap.domain.repository.QuizRepository;

Expand All @@ -23,6 +26,7 @@ public class QuizService {

private final KeywordRepository keywordRepository;
private final QuizRepository quizRepository;
private final EssayAnswerRepository essayAnswerRepository;

@Transactional
public Long createQuiz(Long keywordId, QuizRequest quizRequest) {
Expand All @@ -32,9 +36,19 @@ public Long createQuiz(Long keywordId, QuizRequest quizRequest) {
return quiz.getId();
}

public QuizzesResponse findQuizzesByKeywordId(Long keywordId) {
public QuizzesResponse findQuizzesByKeywordId(Long keywordId, Long memberId) {
final List<Quiz> quizzes = quizRepository.findFetchQuizByKeywordId(keywordId);
return QuizzesResponse.of(keywordId, quizzes);
final List<QuizResponse> quizResponses = quizzes.stream()
.map(quiz -> QuizResponse.of(quiz, isLearning(memberId, quiz.getId())))
.collect(Collectors.toList());
return new QuizzesResponse(keywordId, quizResponses);
}

private boolean isLearning(Long memberId, Long quizId) {
if (isNull(memberId)) {
return false;
}
return essayAnswerRepository.existsByQuizIdAndMemberId(quizId, memberId);
}

@Transactional
Expand All @@ -52,9 +66,9 @@ public void deleteQuiz(Long quizId) {
quizRepository.deleteById(quizId);
}

public QuizResponse findById(Long quizId) {
public QuizResponse findById(Long quizId, Long memberId) {
final Quiz quiz = quizRepository.findById(quizId)
.orElseThrow(() -> new BadRequestException(ROADMAP_QUIZ_NOT_FOUND_EXCEPTION));
return QuizResponse.of(quiz);
return QuizResponse.of(quiz, isLearning(memberId, quizId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package wooteco.prolog.roadmap.application.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.roadmap.domain.Quiz;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class EssayAnswerQuizResponse {

private Long quizId;
private String question;

public EssayAnswerQuizResponse(Long quizId, String question) {
this.quizId = quizId;
this.question = question;
}

public static EssayAnswerQuizResponse from(Quiz quiz) {
return new EssayAnswerQuizResponse(quiz.getId(), quiz.getQuestion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class EssayAnswerResponse {

private Long id;
private QuizResponse quiz;
private EssayAnswerQuizResponse quiz;
private String answer;
private MemberResponse author;
private LocalDateTime createdAt;
Expand All @@ -24,7 +24,7 @@ public static EssayAnswerResponse of(EssayAnswer essayAnswer) {
EssayAnswerResponse response = new EssayAnswerResponse();

response.id = essayAnswer.getId();
response.quiz = QuizResponse.of(essayAnswer.getQuiz());
response.quiz = EssayAnswerQuizResponse.from(essayAnswer.getQuiz());
response.answer = essayAnswer.getAnswer();
response.author = MemberResponse.of(essayAnswer.getMember());
response.createdAt = essayAnswer.getCreatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ public class QuizResponse {

private Long quizId;
private String question;
private Boolean isLearning;

public QuizResponse(Long quizId, String question) {
public QuizResponse(Long quizId, String question, boolean isLearning) {
this.quizId = quizId;
this.question = question;
this.isLearning = isLearning;
}

public static QuizResponse of(Quiz quiz) {
return new QuizResponse(quiz.getId(), quiz.getQuestion());
public static QuizResponse of(Quiz quiz, boolean isLearning) {
return new QuizResponse(quiz.getId(), quiz.getQuestion(), isLearning);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package wooteco.prolog.roadmap.application.dto;

import java.util.List;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.roadmap.domain.Quiz;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand All @@ -20,10 +18,4 @@ public QuizzesResponse(Long keywordId,
this.keywordId = keywordId;
this.data = data;
}

public static QuizzesResponse of(Long keywordId, List<Quiz> quizzes) {
final List<QuizResponse> responses = quizzes.stream().map(QuizResponse::of)
.collect(Collectors.toList());
return new QuizzesResponse(keywordId, responses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import wooteco.prolog.roadmap.domain.EssayAnswer;

public interface EssayAnswerRepository extends JpaRepository<EssayAnswer, Long>,
JpaSpecificationExecutor<EssayAnswer> {

@Query("select ea from EssayAnswer ea where ea.quiz.id = :quizId and ea.member.id = :memberId")
boolean existsByQuizIdAndMemberId(Long quizId, Long memberId);

Optional<EssayAnswer> findByIdAndMemberId(Long id, Long memberId);

List<EssayAnswer> findByQuizIdOrderByIdDesc(Long quizId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import wooteco.prolog.roadmap.domain.EssayAnswer;
import wooteco.prolog.studylog.application.dto.EssayAnswersResponse;


@RestController
@RequestMapping
public class EssayAnswerController {
Expand Down Expand Up @@ -78,8 +79,9 @@ public ResponseEntity<Void> deleteEssayAnswerById(@PathVariable Long essayAnswer
}

@GetMapping("/quizzes/{quizId}")
public ResponseEntity<QuizResponse> findQuizById(@PathVariable Long quizId) {
return ResponseEntity.ok(quizService.findById(quizId));
public ResponseEntity<QuizResponse> findQuizById(@PathVariable Long quizId,
@AuthMemberPrincipal LoginMember member) {
return ResponseEntity.ok(quizService.findById(quizId, member.getId()));
}

@GetMapping("/quizzes/{quizId}/essay-answers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wooteco.prolog.login.domain.AuthMemberPrincipal;
import wooteco.prolog.login.ui.LoginMember;
import wooteco.prolog.roadmap.application.QuizService;
import wooteco.prolog.roadmap.application.dto.QuizRequest;
import wooteco.prolog.roadmap.application.dto.QuizResponse;
Expand All @@ -35,14 +37,16 @@ public ResponseEntity<Void> create(@PathVariable Long sessionId, @PathVariable L
}

@GetMapping("/{quizId}")
public ResponseEntity<QuizResponse> findQuizById(@PathVariable Long quizId) {
return ResponseEntity.ok(quizService.findById(quizId));
public ResponseEntity<QuizResponse> findQuizById(@PathVariable Long quizId,
@AuthMemberPrincipal LoginMember member) {
return ResponseEntity.ok(quizService.findById(quizId, member.getId()));
}

@GetMapping
public ResponseEntity<QuizzesResponse> findQuizzesByKeyword(@PathVariable Long sessionId,
@PathVariable Long keywordId) {
return ResponseEntity.ok(quizService.findQuizzesByKeywordId(keywordId));
@PathVariable Long keywordId,
@AuthMemberPrincipal LoginMember member) {
return ResponseEntity.ok(quizService.findQuizzesByKeywordId(keywordId, member.getId()));
}

@PutMapping("/{quizId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.member.domain.Role;
import wooteco.prolog.member.domain.repository.MemberRepository;
import wooteco.prolog.roadmap.application.dto.EssayAnswerQuizResponse;
import wooteco.prolog.roadmap.application.dto.EssayAnswerRequest;
import wooteco.prolog.roadmap.application.dto.EssayAnswerResponse;
import wooteco.prolog.roadmap.application.dto.EssayAnswerSearchRequest;
import wooteco.prolog.roadmap.application.dto.QuizResponse;
import wooteco.prolog.roadmap.domain.Curriculum;
import wooteco.prolog.roadmap.domain.Keyword;
import wooteco.prolog.roadmap.domain.Quiz;
Expand Down Expand Up @@ -98,7 +98,7 @@ void init() {
new Member("username3", "nickname3", Role.CREW, 113L, "https://"));
member4 = memberRepository.save(
new Member("username4", "nickname4", Role.CREW, 115L, "https://"));

final EssayAnswerRequest essayAnswer1 = new EssayAnswerRequest(quiz1.getId(), "대답1");
final EssayAnswerRequest essayAnswer2 = new EssayAnswerRequest(quiz2.getId(), "대답2");
final EssayAnswerRequest essayAnswer3 = new EssayAnswerRequest(quiz2.getId(), "대답3");
Expand Down Expand Up @@ -330,7 +330,7 @@ void init() {
private EssayAnswerResponse 예상_답변_응답(final Long essayAnswerId, final Quiz quiz,
final String answer, final Member member) {
return new EssayAnswerResponse(essayAnswerId,
new QuizResponse(quiz.getId(), quiz.getQuestion()),
new EssayAnswerQuizResponse(quiz.getId(), quiz.getQuestion()),
answer, new MemberResponse(member.getId(), member.getUsername(), member.getNickname(),
Role.CREW, "https://"), LocalDateTime.now(), LocalDateTime.now());
}
Expand Down
Loading