Skip to content

Commit

Permalink
[Feat] quizid-> UUID 사용 변경 배포
Browse files Browse the repository at this point in the history
[Feat] quizid-> UUID 사용 변경 배포
  • Loading branch information
esc-beep authored Jan 3, 2025
2 parents 20f21af + 756f4e6 commit 4009390
Show file tree
Hide file tree
Showing 23 changed files with 269 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ out/
.vscode/

### ENV ###
/src/main/resources/properties/env.properties
env.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public class AnswerController {

private final AnswerService answerService;

// @PostMapping("/api/answers")
// public ResponseEntity<String> saveAnswers(@RequestBody AnswerBatchRequestDto requestDto){
// answerService.saveAnswers(requestDto.getResponseId(), requestDto.getAnswers());
// return ResponseEntity.status(HttpStatus.CREATED).body("답변 저장 완료");
// }

@PostMapping("/api/answers")
public ResponseEntity<String> saveAnswers(@RequestBody AnswerBatchRequestDto requestDto){
answerService.saveAnswers(requestDto.getResponseId(), requestDto.getQuizId(), requestDto.getAnswers());

answerService.saveAnswers(requestDto.getResponseId(), requestDto.getShareKey(), requestDto.getAnswers());
return ResponseEntity.status(HttpStatus.CREATED).body("답변 저장 완료");
}

// @PostMapping("/api/answers")
// public ResponseEntity<String> saveAnswers(@RequestBody AnswerBatchRequestDto requestDto){
// answerService.saveAnswers(requestDto.getResponseId(), requestDto.getQuizId(), requestDto.getAnswers());
//
// return ResponseEntity.status(HttpStatus.CREATED).body("답변 저장 완료");
// }


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Getter
public class AnswerBatchRequestDto {
private Long responseId;
private Long quizId;
//private Long quizId;
private String shareKey;
private List<AnswerRequestDto> answers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import site.examready2025.quiz.domain.choice.entity.Choice;
import site.examready2025.quiz.domain.choice.repository.ChoiceRepository;
import site.examready2025.quiz.domain.question.repository.QuestionRepository;
import site.examready2025.quiz.domain.quiz.repository.QuizRepository;
import site.examready2025.quiz.domain.response.entity.Response;
import site.examready2025.quiz.domain.response.repository.ResponseRepository;

Expand All @@ -23,15 +24,20 @@ public class AnswerService {
private final ResponseRepository responseRepository;
private final QuestionRepository questionRepository;
private final ChoiceRepository choiceRepository;
private final QuizRepository quizRepository;


public void saveAnswers(Long responseId, Long quizId, List<AnswerRequestDto> answerRequestDtos){
// 정답 저장
public void saveAnswers(Long responseId, String shareKey, List<AnswerRequestDto> answerRequestDtos){
Response response = responseRepository.findById(responseId).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈 세션을 찾을 수 없습니다."));

Long quizId = quizRepository.findByShareKey(shareKey)
.orElseThrow(() -> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다."))
.getId();

for(AnswerRequestDto dto : answerRequestDtos){
// quizId, questionId, selectedChoiceId로 Choice를 검색
Choice choice= choiceRepository.findByQuizIdAndQuestionIdAndId(quizId, dto.getQuestionId(), dto.getSelectedChoiceId())
.orElseThrow(()-> new IllegalArgumentException("quizId로 Choice를 찾을 수 없습니다."));
Choice choice = choiceRepository.findByQuizIdAndQuestionIdAndId(quizId, dto.getQuestionId(), dto.getSelectedChoiceId())
.orElseThrow(()-> new IllegalArgumentException("quizId로 choice를 찾을 수 없습니다."));

Answer answer = Answer.builder()
.response(response)
Expand All @@ -42,21 +48,21 @@ public void saveAnswers(Long responseId, Long quizId, List<AnswerRequestDto> ans
}
}

// public void saveAnswers(Long responseId, List<AnswerRequestDto> answerRequestDtos){
// public void saveAnswers(Long responseId, Long quizId, List<AnswerRequestDto> answerRequestDtos){
// Response response = responseRepository.findById(responseId).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈 세션을 찾을 수 없습니다."));
//
// for(AnswerRequestDto dto : answerRequestDtos){
// Response response = responseRepository.findById(responseId).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈 세션을 찾을 수 없습니다. response id : "+responseId));
// Question question = questionRepository.findById(dto.getQuestionId())
// .orElseThrow(()-> new IllegalArgumentException("해당 질문을 찾을 수 없습니다. 질문 id : "+ dto.getQuestionId()));
//
// Choice choice = choiceRepository.findById(dto.getSelectedChoiceId()).orElseThrow(()-> new IllegalArgumentException("해당 보기를 찾을 수 없습니다. 보기 id : "+dto.getSelectedChoiceId()));
// // quizId, questionId, selectedChoiceId로 Choice를 검색
// Choice choice= choiceRepository.findByQuizIdAndQuestionIdAndId(quizId, dto.getQuestionId(), dto.getSelectedChoiceId())
// .orElseThrow(()-> new IllegalArgumentException("quizId로 Choice를 찾을 수 없습니다."));
//
// Answer answer = Answer.builder()
// .response(response)
// .question(question)
// .question(choice.getQuestion())
// .selectedChoice(choice)
// .build();
// answerRepository.save(answer);
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import site.examready2025.quiz.domain.choice.dto.ChoiceResponseDto;
import site.examready2025.quiz.domain.choice.dto.ChoicesWithQuestionDto;
import site.examready2025.quiz.domain.choice.service.ChoiceService;
import site.examready2025.quiz.domain.response.entity.Response;

import java.util.List;

Expand All @@ -19,30 +20,29 @@ public class ChoiceController {

private final ChoiceService choiceService;

// @PostMapping("/api/choices")
// public ResponseEntity<String> addChoices(@RequestBody List<ChoiceRequestDto> choiceRequestDtos){
// choiceService.addChoices(choiceRequestDtos);
// return ResponseEntity.status(HttpStatus.CREATED).body("보기 저장 완료");
// }

@PostMapping("/api/choices")
public ResponseEntity<String> addChoices(@RequestBody ChoiceBatchRequestDto choiceBatchRequestDto){
choiceService.addChoices(choiceBatchRequestDto.getQuizId(), choiceBatchRequestDto.getChoices());
choiceService.addChoices(choiceBatchRequestDto.getShareKey(), choiceBatchRequestDto.getChoices());
return ResponseEntity.status(HttpStatus.CREATED).body("보기 저장 완료");
}

// 퀴즈 id 에 속한 질문별 choice 반환
@GetMapping("/api/choices/{quizId}")
public ResponseEntity<List<ChoicesWithQuestionDto>> getChoicesWithQuestionByQuiz(@PathVariable("quizId") Long quizId){
List<ChoicesWithQuestionDto> choicesWithQuestion = choiceService.getChoicesWithQuestion(quizId);
// @PostMapping("/api/choices")
// public ResponseEntity<String> addChoices(@RequestBody ChoiceBatchRequestDto choiceBatchRequestDto){
// choiceService.addChoices(choiceBatchRequestDto.getQuizId(), choiceBatchRequestDto.getChoices());
// return ResponseEntity.status(HttpStatus.CREATED).body("보기 저장 완료");
// }

@GetMapping("/api/choices/{shareKey}")
public ResponseEntity<List<ChoicesWithQuestionDto>> getChoicesWithQuestionByQuiz(@PathVariable("shareKey") String shareKey){
List<ChoicesWithQuestionDto> choicesWithQuestion = choiceService.getChoicesWithQuestion(shareKey);
return ResponseEntity.ok(choicesWithQuestion);
}

// 특정 퀴즈 속한 choice 반환(사용X)
@GetMapping("/api/choices/v1/{quizId}")
public ResponseEntity<List<ChoiceResponseDto>> getChoicesByQuiz(@PathVariable("quizId") Long quizId){
List<ChoiceResponseDto> choices = choiceService.getChoicesByQuiz(quizId);
return ResponseEntity.ok(choices);
}
// 퀴즈 id 에 속한 질문별 choice 반환
// @GetMapping("/api/choices/{quizId}")
// public ResponseEntity<List<ChoicesWithQuestionDto>> getChoicesWithQuestionByQuiz(@PathVariable("quizId") Long quizId){
// List<ChoicesWithQuestionDto> choicesWithQuestion = choiceService.getChoicesWithQuestion(quizId);
// return ResponseEntity.ok(choicesWithQuestion);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Getter
public class ChoiceBatchRequestDto {

private Long quizId;
//private Long quizId;
private String shareKey;
private List<ChoiceRequestDto> choices;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import site.examready2025.quiz.domain.choice.entity.Choice;
import site.examready2025.quiz.domain.quiz.entity.Quiz;

import java.util.List;
import java.util.Optional;
Expand All @@ -15,4 +16,9 @@ public interface ChoiceRepository extends JpaRepository<Choice, Long> {
List<Choice> findByQuizId(Long quizId);

Optional<Choice> findFirstByQuestionIdAndIsCorrectTrue(Long questionId);

// shareKey 기반 조회
Optional<Choice> findByQuizShareKeyAndQuestionIdAndId(String shareKey, Long questionId, Long choiceId);
List<Choice> findByQuizShareKey(String shareKey);

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,69 +29,43 @@ public class ChoiceService {
private final ChoiceRepository choiceRepository;
private final QuizRepository quizRepository;

public List<ChoiceResponseDto> getChoicesByQuiz(Long quizId){
List<Choice> choices = choiceRepository.findByQuizId(quizId);
return choices.stream().map(choice -> new ChoiceResponseDto(choice.getId(), choice.getQuestion().getId(), choice.getAnswer(), choice.isCorrect())).toList();
// 특정 퀴즈 보기 반환
@Transactional(readOnly = true)
public List<ChoiceResponseDto> getChoicesByQuiz(String shareKey){
List<Choice> choices = choiceRepository.findByQuizShareKey(shareKey);
return choices.stream().map(choice-> new ChoiceResponseDto(choice.getId(), choice.getQuestion().getId(), choice.getAnswer(), choice.isCorrect())).toList();
}

public void addChoices(Long quizId, List<ChoiceRequestDto> choiceRequestDtos){
Quiz quiz = quizRepository.findById(quizId).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다. 퀴즈id : "+quizId));
// 퀴즈에 보기 추가
public void addChoices(String shareKey, List<ChoiceRequestDto> choiceRequestDtos){
Quiz quiz = quizRepository.findByShareKey(shareKey).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다."));

for(ChoiceRequestDto dto : choiceRequestDtos){
Question question = questionRepository.findById(dto.getQuestionId()).orElseThrow(()-> new IllegalArgumentException("해당 질문을 찾을 수 없습니다. questionId: "+dto.getQuestionId()));
Question question = questionRepository.findById(dto.getQuestionId()).orElseThrow(()-> new IllegalArgumentException("해당 질문을 찾을 수 없습니다."));

List<Choice> choices = new ArrayList<>();

choices.add(Choice.builder().question(question)
.quiz(quiz).answer(dto.getCorrectAnswer()).isCorrect(true).build());
// 정답 추가
choices.add(Choice.builder().quiz(quiz).question(question).answer(dto.getCorrectAnswer()).isCorrect(true).build());

// 오답 추가
for(String wrongAnswer : dto.getWrongAnswers()){
choices.add(Choice.builder().quiz(quiz).question(question).answer(wrongAnswer).isCorrect(false).build());
}

Collections.shuffle(choices);

for(Choice choice : choices){
choiceRepository.save(choice);
}
choices.forEach(choiceRepository::save);
}
}

// 선택지 저장 및 DB 저장
// public void addChoices(Long quizId, List<ChoiceRequestDto> choiceRequestDtos){
//
// Quiz quiz = quizRepository.findById(quizId).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다. 퀴즈 id : "+quizId));
// for(ChoiceRequestDto dto : choiceRequestDtos){
// //Quiz quiz = quizRepository.findById(dto.getQuizId()).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다. 퀴즈 id: "+dto.getQuizId()));
//
// Question question = questionRepository.findById(dto.getQuestionId())
// .orElseThrow(()-> new IllegalArgumentException("해당 질문을 찾을 수 없습니다. 질문 Id : "+dto.getQuestionId()));
//
// // 정답
// Choice correct = Choice.builder()
// .question(question)
// .quiz(quiz)
// .answer(dto.getCorrectAnswer())
// .isCorrect(true)
// .build();
// choiceRepository.save(correct);
//
// // 오답
// for(String wrongAnswers : dto.getWrongAnswers()){
// Choice wrong = Choice.builder()
// .quiz(quiz)
// .question(question)
// .answer(wrongAnswers)
// .isCorrect(false)
// .build();
// choiceRepository.save(wrong);
// }
// }
// }
// share Key 기반 보기 데이터 반환
@Transactional(readOnly = true)
public List<ChoicesWithQuestionDto> getChoicesWithQuestion(String shareKey){
Quiz quiz = quizRepository.findByShareKey(shareKey).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다."));

List<Choice> choices = choiceRepository.findByQuizId(quiz.getId());

// 보기 데이터 반환
public List<ChoicesWithQuestionDto> getChoicesWithQuestion(Long quizId){
List<Choice> choices = choiceRepository.findByQuizId(quizId);
Map<Long, List<ChoiceDto>> groupedChoices = choices.stream()
.collect(Collectors.groupingBy(
choice -> choice.getQuestion().getId(),
Expand All @@ -100,8 +74,53 @@ public List<ChoicesWithQuestionDto> getChoicesWithQuestion(Long quizId){
Collectors.toList()
)
));
return groupedChoices.entrySet().stream()
.map(entry -> new ChoicesWithQuestionDto(entry.getKey(), entry.getValue()))
.toList();

return groupedChoices.entrySet().stream().map(entry-> new ChoicesWithQuestionDto(entry.getKey(), entry.getValue())).toList();
}

// public List<ChoiceResponseDto> getChoicesByQuiz(Long quizId){
// List<Choice> choices = choiceRepository.findByQuizId(quizId);
// return choices.stream().map(choice -> new ChoiceResponseDto(choice.getId(), choice.getQuestion().getId(), choice.getAnswer(), choice.isCorrect())).toList();
// }
//
// public void addChoices(Long quizId, List<ChoiceRequestDto> choiceRequestDtos){
// Quiz quiz = quizRepository.findById(quizId).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다. 퀴즈id : "+quizId));
//
// //Quiz quiz = quizRepository.findByShareKey(shareKey).orElseThrow(()-> new IllegalArgumentException("해당 퀴즈를 찾을 수 없습니다. 퀴즈 shareKey: "+shareKey));
//
// for(ChoiceRequestDto dto : choiceRequestDtos){
// Question question = questionRepository.findById(dto.getQuestionId()).orElseThrow(()-> new IllegalArgumentException("해당 질문을 찾을 수 없습니다. questionId: "+dto.getQuestionId()));
//
// List<Choice> choices = new ArrayList<>();
//
// choices.add(Choice.builder().question(question)
// .quiz(quiz).answer(dto.getCorrectAnswer()).isCorrect(true).build());
//
// for(String wrongAnswer : dto.getWrongAnswers()){
// choices.add(Choice.builder().quiz(quiz).question(question).answer(wrongAnswer).isCorrect(false).build());
// }
//
// Collections.shuffle(choices);
//
// for(Choice choice : choices){
// choiceRepository.save(choice);
// }
// }
// }
//
// // 보기 데이터 반환
// public List<ChoicesWithQuestionDto> getChoicesWithQuestion(Long quizId){
// List<Choice> choices = choiceRepository.findByQuizId(quizId);
// Map<Long, List<ChoiceDto>> groupedChoices = choices.stream()
// .collect(Collectors.groupingBy(
// choice -> choice.getQuestion().getId(),
// Collectors.mapping(
// choice -> new ChoiceDto(choice.getId(), choice.getAnswer(), choice.isCorrect()), // DTO 변환
// Collectors.toList()
// )
// ));
// return groupedChoices.entrySet().stream()
// .map(entry -> new ChoicesWithQuestionDto(entry.getKey(), entry.getValue()))
// .toList();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,37 @@
public class QuizController {
private final QuizService quizService;

@PostMapping("/api/quizzes")
public ResponseEntity<QuizResponseDto> createQuiz(@RequestBody QuizRequestDto requestDto) {
// @PostMapping("/api/quizzes")
// public ResponseEntity<QuizResponseDto> createQuiz(@RequestBody QuizRequestDto requestDto) {
//
// QuizResponseDto responseDto = quizService.createQuiz(requestDto);
//
// return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
// }
//
// // 퀴즈 생성자 이름 조회
// @GetMapping("/api/quizzes/{quizId}/creator")
// public String getQuizCreatorName(@PathVariable("quizId") Long quizId){
// return quizService.getQuizCreatorName(quizId);
// }

// 퀴즈 생성
@PostMapping("/api/quizzes")
public ResponseEntity<QuizResponseDto> createQuiz(@RequestBody QuizRequestDto requestDto){
QuizResponseDto responseDto = quizService.createQuiz(requestDto);

return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}

// 퀴즈 생성자 이름 조회
@GetMapping("/api/quizzes/{quizId}/creator")
public String getQuizCreatorName(@PathVariable("quizId") Long quizId){
return quizService.getQuizCreatorName(quizId);
@GetMapping("/api/quizzes/{shareKey}/creator")
public ResponseEntity<String> getQuizCreatorName(@PathVariable("shareKey") String shareKey){
String creatorName = quizService.getQuizCreatorNameByShareKey(shareKey);
return ResponseEntity.ok(creatorName);
}

@GetMapping("/api/quizzes/{shareKey}")
public ResponseEntity<QuizResponseDto> getQuiz(@PathVariable String shareKey){
QuizResponseDto responseDto = quizService.getQuizByShareKey(shareKey);
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
@Getter
@Builder
public class QuizResponseDto {
private Long quizId;
// private Long quizId;
private Long creatorUserId;
private String title;
private LocalDateTime createdAt;
private String shareKey;
}
Loading

0 comments on commit 4009390

Please sign in to comment.