Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.example.cp_main_be.domain.mission.quiz.enums.QuizType;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;


@Entity
@Getter
Expand All @@ -29,4 +32,9 @@ public class RealQuiz {

@Column(name = "reward_point")
private Long rewardPoints;

@Column private Boolean isCompleted;

@Column @CreationTimestamp private LocalDateTime createdAt;

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.cp_main_be.domain.realquiz.dto.response;

import com.example.cp_main_be.domain.mission.quiz.enums.QuizType;
import com.example.cp_main_be.domain.realquiz.RealQuizOption;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -13,12 +11,13 @@
@NoArgsConstructor
@AllArgsConstructor
public class RealQuizAnswerResponseDTO {
private Long quizId;

private String quizQuestion;
private QuizType quizType;
private Integer selectedOptionNumber; // 사용자가 선택한 답안 번호 추가
private Integer answerNumber;
private String answerDescription;
private Boolean isCorrect; // 사용자 답안 정답 여부
private List<RealQuizOption> quizOptions;
private Boolean isCompleted;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class RealQuizResponseDTO {
private QuizType quizType;
private Integer answerNumber;
private String answerDescription;
private Boolean isCompleted;
private List<RealQuizOptionResponseDTO> quizOptions;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ public ApiResponse<UserQuizCreateResponseDTO> createUserQuiz(
return ApiResponse.success(responseDTO);
}

@GetMapping("/list")
@Operation(summary = "자신의 퀴즈 목록 조회 API")

@GetMapping
@Operation(summary = "자신의 퀴즈 조회 API")
public ApiResponse<RealQuizResponseDTO> getRealQuiz(
@AuthenticationPrincipal User user, @RequestParam QuizType quizType) {
RealQuizResponseDTO responseDTO = realQuizService.getRealQuiz(user, quizType);
return ApiResponse.success(responseDTO);
}

@GetMapping("/{quizId}/answer")
@PostMapping("/{quizId}/answer")
@Operation(summary = "퀴즈 정답 제출 API")
public ApiResponse<RealQuizAnswerResponseDTO> getRealQuizAnswer(
@PathVariable Long quizId, @RequestBody RealQuizAnswerRequestDTO requestDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.example.cp_main_be.global.exception.QuizNotFoundException;
import com.example.cp_main_be.global.exception.UserNotFoundException;
import java.util.List;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -35,6 +36,7 @@ public RealQuizResponseDTO createRealQuiz(RealQuizCreateRequestDTO requestDTO) {
// 퀴즈 생성
RealQuiz realQuiz =
RealQuiz.builder()
.isCompleted(false)
.quizQuestion(requestDTO.getQuizQuestion())
.quizType(requestDTO.getQuizType())
.answerDescription(requestDTO.getAnswerDescription())
Expand All @@ -60,6 +62,7 @@ public RealQuizResponseDTO createRealQuiz(RealQuizCreateRequestDTO requestDTO) {
List<RealQuizResponseDTO.RealQuizOptionResponseDTO> result =
transformToRealQuizOptionResponseDTO(realQuizOptionList);
return RealQuizResponseDTO.builder()
.isCompleted(realQuiz.getIsCompleted())
.quizId(realQuiz.getId())
.quizQuestion(realQuiz.getQuizQuestion())
.quizType(realQuiz.getQuizType())
Expand Down Expand Up @@ -100,7 +103,9 @@ public RealQuizResponseDTO getRealQuiz(User user, QuizType quizType) {
if (userQuiz == null) {
List<RealQuiz> realQuizList = realQuizRepostitory.findAllByQuizType(quizType);
if (realQuizList == null) throw new QuizNotFoundException("불러올 퀴즈가 존재하지 않습니다");
RealQuiz realQuiz = realQuizList.get(0);
Random random = new Random();
int rand = random.nextInt(realQuizList.size());
RealQuiz realQuiz = realQuizList.get(rand);

// 할당한다.
userQuiz = UserQuiz.builder().realQuiz(realQuiz).user(user).build();
Expand Down Expand Up @@ -128,13 +133,16 @@ public RealQuizAnswerResponseDTO getRealQuizAnswer(

List<RealQuizOption> realQuizOptionList = realQuizOptionRepository.findAllByRealQuiz(realQuiz);

realQuiz.setIsCompleted(true);

return RealQuizAnswerResponseDTO.builder()
.answerDescription(realQuiz.getAnswerDescription())
.selectedOptionNumber(requestDTO.getSelectedOptionOrder())
.isCorrect(realQuiz.getAnswerNumber().equals(requestDTO.getSelectedOptionOrder()))
.isCompleted(true)
.answerNumber(realQuiz.getAnswerNumber())
.quizType(realQuiz.getQuizType())
.quizQuestion(realQuiz.getQuizQuestion())
.quizOptions(realQuizOptionList)
.build();
}

Expand All @@ -150,6 +158,7 @@ private RealQuizResponseDTO transformToRealQuizResponseDTO(RealQuiz realQuiz) {
.quizQuestion(realQuiz.getQuizQuestion())
.answerDescription(realQuiz.getAnswerDescription())
.quizOptions(result)
.isCompleted(realQuiz.getIsCompleted())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
"/swagger-resources/**",
"/h2-console/**",
"/error",
"/api/v1/realQuiz/*")
"/api/v1/realQuiz/**",
"/api/v1/realQuiz")
.permitAll() // 회원가입 및 토큰 재발급은 인증 없이 허용
.requestMatchers("/api/v1/admin/**")
.hasRole("ADMIN")
Expand Down
Loading