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

커리큘럼 별 퀴즈 API 생성 #1610

Merged
merged 3 commits into from
Nov 2, 2023
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
14 changes: 12 additions & 2 deletions backend/src/documentation/adoc/quiz.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ include::{snippets}/quiz/detail/http-response.adoc[]

==== Request

include::{snippets}/quiz/list/http-request.adoc[]
include::{snippets}/quiz/list-keyword/http-request.adoc[]

==== Response

include::{snippets}/quiz/list/http-response.adoc[]
include::{snippets}/quiz/list-keyword/http-response.adoc[]

=== 커리큘럼별 퀴즈 목록 조회

==== Request

include::{snippets}/quiz/list-curriculum/http-request.adoc[]

==== Response

include::{snippets}/quiz/list-curriculum/http-response.adoc[]

=== 퀴즈 삭제

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@


import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import wooteco.prolog.NewDocumentation;
import wooteco.prolog.roadmap.application.QuizService;
import wooteco.prolog.roadmap.application.dto.CurriculumQuizResponse;
import wooteco.prolog.roadmap.application.dto.QuizRequest;
import wooteco.prolog.roadmap.application.dto.QuizResponse;
import wooteco.prolog.roadmap.application.dto.QuizzesResponse;
Expand Down Expand Up @@ -61,10 +68,47 @@ public class QuizDocumentation extends NewDocumentation {

given
.when().get("/sessions/{sessionId}/keywords/{keywordId}/quizs", 1L, 1L)
.then().log().all().apply(document("quiz/list"))
.then().log().all().apply(document("quiz/list-keyword"))
.statusCode(HttpStatus.OK.value());
}

@Test
void Curriculum별_Quiz_조회() {
given(quizService.findQuizzesByCurriculumId(anyLong()))
.willReturn(makeMockResponse());

given
.when().get("/curriculums/{curriculumId}/quizzes", 1L)
.then().log().all().apply(document("quiz/list-curriculum"))
.statusCode(HttpStatus.OK.value());
}

private List<CurriculumQuizResponse> makeMockResponse() {
CurriculumQuizResponse response1 = new CurriculumQuizResponse() {
@Override
public Long getId() {
return 1L;
}

@Override
public String getQuestion() {
return "question1";
}
};
CurriculumQuizResponse response2 = new CurriculumQuizResponse() {
@Override
public Long getId() {
return 2L;
}

@Override
public String getQuestion() {
return "question2";
}
};
return Arrays.asList(response1, response2);
}

@Test
void 퀴즈_수정() {
doNothing().when(quizService).updateQuiz(any(), any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wooteco.prolog.common.exception.BadRequestException;
import wooteco.prolog.roadmap.application.dto.CurriculumQuizResponse;
import wooteco.prolog.roadmap.application.dto.QuizRequest;
import wooteco.prolog.roadmap.application.dto.QuizResponse;
import wooteco.prolog.roadmap.application.dto.QuizzesResponse;
Expand Down Expand Up @@ -73,4 +74,8 @@ public QuizResponse findById(Long quizId, Long memberId) {
.orElseThrow(() -> new BadRequestException(ROADMAP_QUIZ_NOT_FOUND_EXCEPTION));
return QuizResponse.of(quiz, isLearning(memberId, quizId));
}

public List<CurriculumQuizResponse> findQuizzesByCurriculumId(Long curriculumId) {
return quizRepository.findQuizzesByCurriculum(curriculumId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wooteco.prolog.roadmap.application.dto;

public interface CurriculumQuizResponse {

Long getId();

String getQuestion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import wooteco.prolog.roadmap.application.dto.CurriculumQuizResponse;
import wooteco.prolog.roadmap.domain.Quiz;

public interface QuizRepository extends JpaRepository<Quiz, Long> {
Expand All @@ -11,4 +13,12 @@ public interface QuizRepository extends JpaRepository<Quiz, Long> {
+ " JOIN FETCH q.keyword k"
+ " WHERE q.keyword.id = :keywordId")
List<Quiz> findFetchQuizByKeywordId(Long keywordId);

@Query(nativeQuery = true,
value = "SELECT q.id, q.question " +
"FROM curriculum c " +
"JOIN session s ON c.id = :curriculumId AND s.curriculum_id = c.id " +
"JOIN keyword k ON k.session_id = s.id " +
"JOIN quiz q ON q.keyword_id = k.id")
List<CurriculumQuizResponse> findQuizzesByCurriculum(@Param("curriculumId") Long curriculumId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wooteco.prolog.roadmap.ui;

import java.net.URI;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -14,6 +15,7 @@
import wooteco.prolog.login.domain.AuthMemberPrincipal;
import wooteco.prolog.login.ui.LoginMember;
import wooteco.prolog.roadmap.application.QuizService;
import wooteco.prolog.roadmap.application.dto.CurriculumQuizResponse;
import wooteco.prolog.roadmap.application.dto.QuizRequest;
import wooteco.prolog.roadmap.application.dto.QuizResponse;
import wooteco.prolog.roadmap.application.dto.QuizzesResponse;
Expand Down Expand Up @@ -64,6 +66,13 @@ public ResponseEntity<QuizzesResponse> newFindQuizzesByKeyword(
return ResponseEntity.ok(quizService.findQuizzesByKeywordId(keywordId, member.getId()));
}

@GetMapping("/curriculums/{curriculumId}/quizzes")
public ResponseEntity<List<CurriculumQuizResponse>> findQuizzesByCurriculum(
Comment on lines +69 to +70
Copy link
Contributor Author

Choose a reason for hiding this comment

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

이거 매핑을 어떻게하면 좋을까요?

@PathVariable Long curriculumId
) {
return ResponseEntity.ok(quizService.findQuizzesByCurriculumId(curriculumId));
}

@PutMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs/{quizId}")
ResponseEntity<Void> updateQuiz(@PathVariable Long sessionId,
@PathVariable Long keywordId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package wooteco.prolog.roadmap.repository;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import wooteco.prolog.roadmap.application.dto.CurriculumQuizResponse;
import wooteco.prolog.roadmap.domain.Curriculum;
import wooteco.prolog.roadmap.domain.Keyword;
import wooteco.prolog.roadmap.domain.Quiz;
import wooteco.prolog.roadmap.domain.repository.CurriculumRepository;
import wooteco.prolog.roadmap.domain.repository.KeywordRepository;
import wooteco.prolog.roadmap.domain.repository.QuizRepository;
import wooteco.prolog.session.domain.Session;
Expand All @@ -24,9 +29,14 @@ class QuizRepositoryTest {
@Autowired
private KeywordRepository keywordRepository;

@Autowired
private CurriculumRepository curriculumRepository;

@Autowired
private SessionRepository sessionRepository;

private Curriculum 백엔드;

private Session session_백엔드_레벨1;

private Keyword 자바;
Expand All @@ -38,15 +48,15 @@ class QuizRepositoryTest {

@BeforeEach
void setUp() {
session_백엔드_레벨1 = sessionRepository.save(new Session("백엔드Java 레벨1"));
백엔드 = curriculumRepository.save(new Curriculum("백엔드"));

session_백엔드_레벨1 = sessionRepository.save(new Session(백엔드.getId(), "백엔드Java 레벨1"));

자바 = keywordRepository.save(
new Keyword(null, "자바", "자바입니다", 1, 1, session_백엔드_레벨1.getId(), null, null));
session_백엔드_레벨1 = sessionRepository.save(new Session("백엔드Java 레벨1"));

깃 = keywordRepository.save(
new Keyword(null, "깃", "깃입니다", 2, 2, session_백엔드_레벨1.getId(), null, null));
session_백엔드_레벨1 = sessionRepository.save(new Session("백엔드Java 레벨1"));

자바_질문1 = quizRepository.save(new Quiz(자바, "자바의 아버지는 제임스 고슬링일까요 ? 제이슨일까요 ?"));
자바_질문2 = quizRepository.save(new Quiz(자바, "Stream 은 자바 몇 버전부터 지원했을까요?"));
Expand All @@ -61,6 +71,19 @@ void findQuizzesByKeyword() {
final List<Quiz> expect = Arrays.asList(자바_질문1, 자바_질문2);
final List<Quiz> actual = quizRepository.findFetchQuizByKeywordId(자바.getId());

Assertions.assertThat(actual).containsExactlyElementsOf(expect);
assertThat(actual).containsExactlyElementsOf(expect);
}

@DisplayName("커리큘럼 id 로 퀴즈 List 를 조회한다.")
@Test
void findQuizzesByCurriculum() {
// given
Long 백엔드_커리큘럼_Id = 백엔드.getId();

// when
List<CurriculumQuizResponse> acutal = quizRepository.findQuizzesByCurriculum(백엔드_커리큘럼_Id);

// then
assertThat(acutal).hasSize(3);
}
}
Loading