Skip to content

Commit

Permalink
[COT-118] Feature: 랜덤 퀴즈 제출 API 구현 (#246)
Browse files Browse the repository at this point in the history
* feat: develop reply to randomQuiz API

* style: rename parameter

quizId -> randomQuizId

* refactor: change RandomQuiz reply API from POST to GET

* refactor: change type of randomquiz result

* refactor: change type of quiz result

* refactor: change api path

* Revert "refactor: change type of quiz result"

This reverts commit fe14323.
  • Loading branch information
gikhoon authored Dec 25, 2024
1 parent 4755228 commit f363a04
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.cotato.csquiz.api.quiz.dto.RandomQuizReplyResponse;
import org.cotato.csquiz.api.quiz.dto.RandomTutorialQuizResponse;
import org.cotato.csquiz.domain.education.service.RandomQuizService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "CS 퀴즈 탭 정보", description = "외부인용 CS 퀴즈 탭 관련 API 입니다.")
Expand All @@ -23,4 +26,11 @@ public class RandomQuizController {
public ResponseEntity<RandomTutorialQuizResponse> pickRandomQuiz() {
return ResponseEntity.ok().body(randomQuizService.pickRandomQuiz());
}

@Operation(summary = "외부인용 랜덤 퀴즈 제출 API")
@GetMapping("/{randomQuizId}/reply")
public ResponseEntity<RandomQuizReplyResponse> replyToRandomQuiz(@PathVariable(name = "randomQuizId") final Long randomQuizId,
@RequestParam(name = "input") Integer input) {
return ResponseEntity.ok().body(randomQuizService.replyToRandomQuiz(randomQuizId, input));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.cotato.csquiz.api.quiz.dto;

import jakarta.validation.constraints.NotNull;

public record RandomQuizReplyRequest(
@NotNull
Integer input
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.cotato.csquiz.api.quiz.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;

public record RandomQuizReplyResponse(
@Schema(requiredMode = RequiredMode.REQUIRED)
boolean result
) {
public static RandomQuizReplyResponse from(Boolean isCorrect) {
return new RandomQuizReplyResponse(isCorrect);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SecurityConfig {
"/v2/api/policies",
"/v2/api/events/**",
"/v1/api/generation/current",
"/v2/api/random-quizzes"
"/v2/api/random-quizzes/**"
};

private final JwtTokenProvider jwtTokenProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public String getImageUrl() {
}
return image.getUrl();
}

public boolean isCorrect(Integer input) {
return answerNumber.equals(input);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.cotato.csquiz.domain.education.service;

import lombok.RequiredArgsConstructor;
import org.cotato.csquiz.api.quiz.dto.RandomQuizReplyResponse;
import org.cotato.csquiz.api.quiz.dto.RandomTutorialQuizResponse;
import org.cotato.csquiz.domain.education.entity.RandomQuiz;
import org.cotato.csquiz.domain.education.service.component.RandomQuizReader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,4 +18,9 @@ public class RandomQuizService {
public RandomTutorialQuizResponse pickRandomQuiz() {
return RandomTutorialQuizResponse.from(randomQuizReader.getRandomQuiz());
}

public RandomQuizReplyResponse replyToRandomQuiz(final Long quizId, final Integer input) {
RandomQuiz randomQuiz = randomQuizReader.findById(quizId);
return RandomQuizReplyResponse.from(randomQuiz.isCorrect(input));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public RandomQuiz getRandomQuiz() {
Page<RandomQuiz> quizPage = randomQuizRepository.findAll(PageRequest.of(randomPage, 1));
return quizPage.getContent().get(0);
}

public RandomQuiz findById(Long id) {
return randomQuizRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("해당 퀴즈가 존재하지 않습니다."));
}
}

0 comments on commit f363a04

Please sign in to comment.