Skip to content

Commit

Permalink
feat: develop reply to randomQuiz API
Browse files Browse the repository at this point in the history
  • Loading branch information
gikhoon committed Dec 17, 2024
1 parent 560087e commit 777948d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.cotato.csquiz.api.quiz.dto.RandomQuizReplyRequest;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -23,4 +29,11 @@ public class RandomQuizController {
public ResponseEntity<RandomTutorialQuizResponse> pickRandomQuiz() {
return ResponseEntity.ok().body(randomQuizService.pickRandomQuiz());
}

@Operation(summary = "외부인용 랜덤 퀴즈 제출 API")
@PostMapping("/{quizId}/reply")
public ResponseEntity<RandomQuizReplyResponse> replyToRandomQuiz(@PathVariable(name = "quizId") final Long quizId,
@Valid @RequestBody RandomQuizReplyRequest request) {
return ResponseEntity.ok().body(randomQuizService.replyToRandomQuiz(quizId, request.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)
String result
) {
public static RandomQuizReplyResponse from(Boolean isCorrect) {
return new RandomQuizReplyResponse(isCorrect.toString());
}
}
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 777948d

Please sign in to comment.