-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Hackathon-PassOn/feat/get-random-quiz-api
feat : 랜덤 퀴즈 요청 API 구현
- Loading branch information
Showing
9 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/cmc/cmc15th_hackathon/domain/menu/repository/QuizRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cmc.cmc15th_hackathon.domain.menu.repository; | ||
|
||
import cmc.cmc15th_hackathon.domain.quiz.entity.Quiz; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface QuizRepository extends JpaRepository<Quiz, Long> { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/cmc/cmc15th_hackathon/domain/quiz/controller/QuizController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cmc.cmc15th_hackathon.domain.quiz.controller; | ||
|
||
import cmc.cmc15th_hackathon.domain.quiz.response.QuizResponse; | ||
import cmc.cmc15th_hackathon.domain.quiz.service.QuizService; | ||
import cmc.cmc15th_hackathon.global.common.CustomResponseEntity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class QuizController { | ||
private final QuizService quizService; | ||
|
||
@GetMapping("/quiz") | ||
public CustomResponseEntity<QuizResponse.Random> getRandomQuiz() { | ||
return CustomResponseEntity.success(quizService.getRandomQuiz()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/cmc/cmc15th_hackathon/domain/quiz/response/QuizResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cmc.cmc15th_hackathon.domain.quiz.response; | ||
|
||
import lombok.*; | ||
|
||
public class QuizResponse { | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public static class Random { | ||
private String content; | ||
private String answer; | ||
|
||
public static QuizResponse.Random to(String content, String answer) { | ||
return Random.builder() | ||
.content(content) | ||
.answer(answer) | ||
.build(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/cmc/cmc15th_hackathon/domain/quiz/service/QuizService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmc.cmc15th_hackathon.domain.quiz.service; | ||
|
||
import cmc.cmc15th_hackathon.domain.menu.repository.QuizRepository; | ||
import cmc.cmc15th_hackathon.domain.quiz.entity.Quiz; | ||
import cmc.cmc15th_hackathon.domain.quiz.response.QuizResponse; | ||
import cmc.cmc15th_hackathon.global.common.Result; | ||
import cmc.cmc15th_hackathon.global.exception.CustomException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Random; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class QuizService { | ||
|
||
private final QuizRepository quizRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public QuizResponse.Random getRandomQuiz() { | ||
Quiz quiz = quizRepository.findById(getRandomNumber()).orElseThrow( | ||
() -> new CustomException(Result.FAIL) | ||
); | ||
|
||
return QuizResponse.Random.to(quiz.getContent(), quiz.getAnswer()); | ||
} | ||
|
||
public static long getRandomNumber() { | ||
Random random = new Random(); | ||
return random.nextLong(7) + 1; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/cmc/cmc15th_hackathon/docs/quiz/QuizControllerDocsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmc.cmc15th_hackathon.docs.quiz; | ||
|
||
import cmc.cmc15th_hackathon.docs.RestDocsSupport; | ||
import cmc.cmc15th_hackathon.domain.quiz.controller.QuizController; | ||
import cmc.cmc15th_hackathon.domain.quiz.response.QuizResponse; | ||
import cmc.cmc15th_hackathon.domain.quiz.service.QuizService; | ||
import com.epages.restdocs.apispec.ResourceSnippetParameters; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; | ||
import static org.springframework.restdocs.payload.JsonFieldType.NUMBER; | ||
import static org.springframework.restdocs.payload.JsonFieldType.STRING; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
public class QuizControllerDocsTest extends RestDocsSupport { | ||
private final QuizService quizService = mock(QuizService.class); | ||
|
||
@Override | ||
protected Object initController() { | ||
return new QuizController(quizService); | ||
} | ||
|
||
@DisplayName("랜덤 퀴즈 요청 API") | ||
@Test | ||
void getRandomQuiz() throws Exception { | ||
// given | ||
given(quizService.getRandomQuiz()) | ||
.willReturn(QuizResponse.Random.builder() | ||
.content("7*6?") | ||
.answer("42") | ||
.build()); | ||
|
||
ResourceSnippetParameters parameters = ResourceSnippetParameters.builder() | ||
.tag("퀴즈 API") | ||
.summary("랜덤 퀴즈 요청 API") | ||
.responseFields( | ||
fieldWithPath("code").type(NUMBER).description("상태 코드"), | ||
fieldWithPath("message").type(STRING).description("상태 메시지"), | ||
fieldWithPath("data.content").type(STRING).description("퀴즈 내용"), | ||
fieldWithPath("data.answer").type(STRING).description("퀴즈 정답")) | ||
.build(); | ||
|
||
RestDocumentationResultHandler document = documentHandler("getRandomQuiz", prettyPrint(), parameters); | ||
|
||
// when // then | ||
mockMvc.perform( | ||
RestDocumentationRequestBuilders.get("/quiz")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andDo(document); | ||
} | ||
} |