Skip to content

Commit

Permalink
BugFix(#11) : 문제 생성 관련 오류 수정, questionId key 생성값 수정(roomId + roundNum)
Browse files Browse the repository at this point in the history
  • Loading branch information
oo-ni committed Jan 21, 2025
1 parent 66470d0 commit 5bb4754
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RoundSocketController {
@MessageMapping("/round/question")
public void giveQuestion(QuestionReq request) {
QuestionRes response = roundService.getQuestion(request);
messagingTemplate.convertAndSend("game-service/sub/" + response.roomId(), response);
messagingTemplate.convertAndSend("/game-service/sub/" + response.roomId(), response);
}

// 플레이어 정답 제출
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public GameCreateRes createGame(GameCreateReq request) {

// Question 생성
for (int roundNum = 1; roundNum <= room.getTotalRounds(); roundNum++) {
Question question = roundService.createQuestion(game.getRoomId());
Question question = roundService.createQuestion(game.getRoomId(), roundNum);
game.getQuestionIds().add(question.getQuestionId());
}

Expand Down Expand Up @@ -109,8 +109,17 @@ public Game updateGameStatusById(String gameId, Status status) {
@Transactional(readOnly = true)
@Override
public Game findGameById(String gameId) {
return gameRepository.findById(gameId)
Game game = gameRepository.findById(gameId)
.orElseThrow(() -> new GameException(ExceptionMessage.GAME_NOT_FOUND));

if (game.getRoundScores() == null) {
game.setRoundScores(new HashMap<>());
}
if (game.getTotalScores() == null) {
game.setTotalScores(new HashMap<>());
}

return game;
}

// 라운드 점수 업뎃
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class Question {
private List<String> contents;

@Builder
public Question(String questionId, String roomId, int roundNum, double latitude, double longitude, String hint, List<String> contents) {
this.questionId = questionId;
public Question(String roomId, int roundNum, double latitude, double longitude, String hint, List<String> contents) {
this.questionId = roomId + ":" + roundNum;
this.roomId = roomId;
this.roundNum = roundNum;
this.latitude = latitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Repository
public interface QuestionRepository extends CrudRepository<Question, String> {
Optional<Question> findByRoomId(String roomId);
Optional<Question> findByRoomIdAndRoundNum(String roomId, int roundNum);

List<Question> findAllByRoomId(String roomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public interface RoundService {
// 문제 생성
Question createQuestion(String roomId);
Question createQuestion(String roomId, int roundNum);

// 문제 출제
QuestionRes getQuestion(QuestionReq request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RoundServiceImpl implements RoundService {

// 문제 생성
@Override
public Question createQuestion(String roomId) {
public Question createQuestion(String roomId, int roundNum) {
Room room = roomService.findRoomById(roomId);

// 해당 게임의 기존 문제 조회
Expand Down Expand Up @@ -72,7 +72,8 @@ public Question createQuestion(String roomId) {
.collect(Collectors.toList());

newQuestion = Question.builder()
.questionId(UUID.randomUUID().toString())
.roomId(roomId)
.roundNum(roundNum)
.latitude(targetLatitude)
.longitude(targetLongitude)
.hint(firstContent.hint())
Expand All @@ -86,7 +87,7 @@ public Question createQuestion(String roomId) {
// 문제 출제
@Override
public QuestionRes getQuestion(QuestionReq request) {
Question question = findQuestionByRoomId(request.roomId());
Question question = findQuestionByRoomIdAndRoundNum(request.roomId(), request.roundNum());
return QuestionRes.from(question);
}

Expand Down Expand Up @@ -120,8 +121,9 @@ public List<Answer> findAnswersByQuestionId(String questionId) {
}

// roomId로 문제 정보 조회
private Question findQuestionByRoomId(String roomId) {
return questionRepository.findByRoomId(roomId)
private Question findQuestionByRoomIdAndRoundNum(String roomId, int roundNum) {
String questionId = roomId + ":" + roundNum;
return questionRepository.findById(questionId)
.orElseThrow(() -> new QuestionException(ExceptionMessage.QUESTION_NOT_FOUND));
}

Expand Down

0 comments on commit 5bb4754

Please sign in to comment.