Skip to content

Commit

Permalink
Merge pull request #13 from urdego/feature/#11
Browse files Browse the repository at this point in the history
Feature#11 : redis 조회오류 수정
  • Loading branch information
oo-ni authored Jan 22, 2025
2 parents 1f6d1e0 + 6bc7bbc commit 9a6de9f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import io.urdego.urdego_game_service.domain.round.entity.Question;
import io.urdego.urdego_game_service.domain.round.service.RoundService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.*;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
Expand Down Expand Up @@ -76,6 +78,8 @@ public ScoreRes giveScores(ScoreReq request) {

gameRepository.save(game);

log.info("게임 점수 정보 : roundScores={}, totalScores={}", game.getRoundScores(), game.getTotalScores());

return ScoreRes.from(game);
}

Expand Down Expand Up @@ -119,23 +123,30 @@ public Game findGameById(String gameId) {
game.setTotalScores(new HashMap<>());
}

gameRepository.save(game);

return game;
}

// 라운드 점수 업뎃
private void updateRoundScores(Game game, int roundNum, List<Answer> answers) {
Map<Integer, Map<String, Integer>> roundScores = game.getRoundScores();

if (!roundScores.containsKey(roundNum)) {
roundScores.put(roundNum, new HashMap<>());
if (roundScores == null) {
roundScores = new HashMap<>();
}

roundScores.putIfAbsent(roundNum, new HashMap<>());

Map<String, Integer> roundScore = roundScores.get(roundNum);
for (Answer answer : answers) {
log.info("Answer: userId={}, score={}", answer.getUserId(), answer.getScore());
roundScore.put(answer.getUserId(), answer.getScore());
}

game.setRoundScores(roundScores);

log.info("{}라운드 점수가 업데이트 되었습니다. : {}", roundNum, game.getRoundScores());
}

// 전체 점수 업뎃
Expand All @@ -149,5 +160,7 @@ private void updateTotalScores(Game game) {
});

game.setTotalScores(totalScores);

log.info("전체 점수가 업데이트 되었습니다. : {}", game.getTotalScores());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

@Getter
@RedisHash(value = "answer", timeToLive = 3600)
Expand All @@ -14,7 +15,10 @@ public class Answer {

@Id
private String answerId;

@Indexed
private String questionId;

private String userId;
private double latitude;
private double longitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

@Repository
public interface AnswerRepository extends CrudRepository<Answer, String> {
List<Answer> findByQuestionId(String questionId);
List<Answer> findAllByQuestionId(String questionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public AnswerRes submitAnswer(AnswerReq request) {
@Override
@Transactional(readOnly = true)
public List<Answer> findAnswersByQuestionId(String questionId) {
return answerRepository.findByQuestionId(questionId);
return answerRepository.findAllByQuestionId(questionId);
}

// roomId로 문제 정보 조회
Expand Down

0 comments on commit 9a6de9f

Please sign in to comment.