Skip to content

Commit 1bd3f28

Browse files
committed
♻️ refactor: saveRoom, 퀴즈 정보 조회 메서드 수정
1 parent 34b0ed2 commit 1bd3f28

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.f1.backend.domain.game.dto.response.RoomResponse;
2727
import io.f1.backend.domain.game.dto.response.RoomSettingResponse;
2828
import io.f1.backend.domain.game.dto.response.SystemNoticeResponse;
29+
import io.f1.backend.domain.game.event.RoomCreatedEvent;
2930
import io.f1.backend.domain.game.model.GameSetting;
3031
import io.f1.backend.domain.game.model.Player;
3132
import io.f1.backend.domain.game.model.Room;
@@ -67,7 +68,8 @@ public class RoomService {
6768
public RoomCreateResponse saveRoom(RoomCreateRequest request) {
6869

6970
QuizMinData quizMinData = quizService.getQuizMinData();
70-
// Quiz quiz = quizService.getQuizWithQuestionsById(quizMinId);
71+
72+
Quiz quiz = quizService.findQuizById(quizMinData.quizMinId());
7173

7274
GameSetting gameSetting = toGameSetting(quizMinData);
7375

@@ -83,7 +85,7 @@ public RoomCreateResponse saveRoom(RoomCreateRequest request) {
8385

8486
roomRepository.saveRoom(room);
8587

86-
// eventPublisher.publishEvent(new RoomCreatedEvent(room, quiz));
88+
eventPublisher.publishEvent(new RoomCreatedEvent(room, quiz));
8789

8890
return new RoomCreateResponse(newId);
8991
}
@@ -108,7 +110,7 @@ public void enterRoom(RoomValidationRequest request) {
108110
}
109111

110112
if (room.getRoomSetting().locked()
111-
&& !room.getRoomSetting().password().equals(request.password())) {
113+
&& !room.getRoomSetting().password().equals(request.password())) {
112114
throw new CustomException(RoomErrorCode.WRONG_PASSWORD);
113115
}
114116

@@ -117,7 +119,7 @@ public void enterRoom(RoomValidationRequest request) {
117119
}
118120

119121
public RoomInitialData initializeRoomSocket(
120-
Long roomId, String sessionId, UserPrincipal principal) {
122+
Long roomId, String sessionId, UserPrincipal principal) {
121123

122124
Room room = findRoom(roomId);
123125

@@ -143,15 +145,15 @@ public RoomInitialData initializeRoomSocket(
143145
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
144146

145147
GameSettingResponse gameSettingResponse =
146-
toGameSettingResponse(room.getGameSetting(), quiz);
148+
toGameSettingResponse(room.getGameSetting(), quiz);
147149

148150
PlayerListResponse playerListResponse = toPlayerListResponse(room);
149151

150152
SystemNoticeResponse systemNoticeResponse =
151-
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);
153+
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);
152154

153155
return new RoomInitialData(
154-
roomSettingResponse, gameSettingResponse, playerListResponse, systemNoticeResponse);
156+
roomSettingResponse, gameSettingResponse, playerListResponse, systemNoticeResponse);
155157
}
156158

157159
public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
@@ -177,7 +179,7 @@ public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal princi
177179
removePlayer(room, sessionId, removePlayer);
178180

179181
SystemNoticeResponse systemNoticeResponse =
180-
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
182+
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
181183

182184
PlayerListResponse playerListResponse = toPlayerListResponse(room);
183185

@@ -187,9 +189,9 @@ public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal princi
187189

188190
public PlayerListResponse handlePlayerReady(Long roomId, String sessionId) {
189191
Player player =
190-
roomRepository
191-
.findPlayerInRoomBySessionId(roomId, sessionId)
192-
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
192+
roomRepository
193+
.findPlayerInRoomBySessionId(roomId, sessionId)
194+
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
193195

194196
player.toggleReady();
195197

@@ -201,15 +203,15 @@ public PlayerListResponse handlePlayerReady(Long roomId, String sessionId) {
201203
public RoomListResponse getAllRooms() {
202204
List<Room> rooms = roomRepository.findAll();
203205
List<RoomResponse> roomResponses =
204-
rooms.stream()
205-
.map(
206-
room -> {
207-
Long quizId = room.getGameSetting().getQuizId();
208-
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
209-
210-
return toRoomResponse(room, quiz);
211-
})
212-
.toList();
206+
rooms.stream()
207+
.map(
208+
room -> {
209+
Long quizId = room.getGameSetting().getQuizId();
210+
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
211+
212+
return toRoomResponse(room, quiz);
213+
})
214+
.toList();
213215
return new RoomListResponse(roomResponses);
214216
}
215217

@@ -232,12 +234,12 @@ public RoundResult chat(Long roomId, String sessionId, ChatMessage chatMessage)
232234
room.increasePlayerCorrectCount(sessionId);
233235

234236
return RoundResult.builder()
235-
.questionResult(
236-
toQuestionResultResponse(currentQuestion.getId(), chatMessage, answer))
237-
.rankUpdate(toRankUpdateResponse(room))
238-
.systemNotice(ofPlayerEvent(chatMessage.nickname(), RoomEventType.ENTER))
239-
.chat(chatMessage)
240-
.build();
237+
.questionResult(
238+
toQuestionResultResponse(currentQuestion.getId(), chatMessage, answer))
239+
.rankUpdate(toRankUpdateResponse(room))
240+
.systemNotice(ofPlayerEvent(chatMessage.nickname(), RoomEventType.ENTER))
241+
.chat(chatMessage)
242+
.build();
241243
}
242244

243245
private Player getRemovePlayer(Room room, String sessionId, UserPrincipal principal) {
@@ -259,8 +261,8 @@ private Player createPlayer() {
259261

260262
private Room findRoom(Long roomId) {
261263
return roomRepository
262-
.findRoom(roomId)
263-
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
264+
.findRoom(roomId)
265+
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
264266
}
265267

266268
private boolean isLastPlayer(Room room, String sessionId) {
@@ -280,14 +282,14 @@ private void changeHost(Room room, String hostSessionId) {
280282
Map<String, Player> playerSessionMap = room.getPlayerSessionMap();
281283

282284
Optional<String> nextHostSessionId =
283-
playerSessionMap.keySet().stream()
284-
.filter(key -> !key.equals(hostSessionId))
285-
.findFirst();
285+
playerSessionMap.keySet().stream()
286+
.filter(key -> !key.equals(hostSessionId))
287+
.findFirst();
286288

287289
Player nextHost =
288-
playerSessionMap.get(
289-
nextHostSessionId.orElseThrow(
290-
() -> new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND)));
290+
playerSessionMap.get(
291+
nextHostSessionId.orElseThrow(
292+
() -> new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND)));
291293

292294
room.updateHost(nextHost);
293295
log.info("user_id:{} 방장 변경 완료 ", nextHost.getId());
@@ -301,4 +303,4 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
301303
private RoundResult buildResultOnlyChat(ChatMessage chatMessage) {
302304
return RoundResult.builder().chat(chatMessage).build();
303305
}
304-
}
306+
}

backend/src/main/java/io/f1/backend/domain/quiz/app/QuizService.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ private String getExtension(String filename) {
122122
public void deleteQuiz(Long quizId) {
123123

124124
Quiz quiz =
125-
quizRepository
126-
.findById(quizId)
127-
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
125+
quizRepository
126+
.findById(quizId)
127+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
128128

129129
// TODO : util 메서드에서 사용자 ID 꺼내쓰는 식으로 수정하기
130130
if (1L != quiz.getCreator().getId()) {
@@ -138,9 +138,9 @@ public void deleteQuiz(Long quizId) {
138138
@Transactional
139139
public void updateQuizTitle(Long quizId, String title) {
140140
Quiz quiz =
141-
quizRepository
142-
.findById(quizId)
143-
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
141+
quizRepository
142+
.findById(quizId)
143+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
144144

145145
validateTitle(title);
146146
quiz.changeTitle(title);
@@ -150,9 +150,9 @@ public void updateQuizTitle(Long quizId, String title) {
150150
public void updateQuizDesc(Long quizId, String description) {
151151

152152
Quiz quiz =
153-
quizRepository
154-
.findById(quizId)
155-
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
153+
quizRepository
154+
.findById(quizId)
155+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
156156

157157
validateDesc(description);
158158
quiz.changeDescription(description);
@@ -162,9 +162,9 @@ public void updateQuizDesc(Long quizId, String description) {
162162
public void updateThumbnail(Long quizId, MultipartFile thumbnailFile) {
163163

164164
Quiz quiz =
165-
quizRepository
166-
.findById(quizId)
167-
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
165+
quizRepository
166+
.findById(quizId)
167+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
168168

169169
validateImageFile(thumbnailFile);
170170
String newThumbnailPath = convertToThumbnailPath(thumbnailFile);
@@ -230,9 +230,9 @@ public QuizListPageResponse getQuizzes(String title, String creator, Pageable pa
230230
@Transactional(readOnly = true)
231231
public Quiz getQuizWithQuestionsById(Long quizId) {
232232
Quiz quiz =
233-
quizRepository
234-
.findQuizWithQuestionsById(quizId)
235-
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
233+
quizRepository
234+
.findQuizWithQuestionsById(quizId)
235+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
236236
return quiz;
237237
}
238238

@@ -244,21 +244,27 @@ public QuizMinData getQuizMinData() {
244244
@Transactional(readOnly = true)
245245
public QuizQuestionListResponse getQuizWithQuestions(Long quizId) {
246246
Quiz quiz =
247-
quizRepository
248-
.findById(quizId)
249-
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
247+
quizRepository
248+
.findById(quizId)
249+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
250250

251251
return quizToQuizQuestionListResponse(quiz);
252252
}
253253

254254
@Transactional(readOnly = true)
255255
public List<Question> getRandomQuestionsWithoutAnswer(Long quizId, Integer round) {
256256
quizRepository
257-
.findById(quizId)
258-
.orElseThrow(() -> new NoSuchElementException("존재하지 않는 퀴즈입니다."));
257+
.findById(quizId)
258+
.orElseThrow(() -> new NoSuchElementException("존재하지 않는 퀴즈입니다."));
259259

260260
List<Question> randomQuestions = quizRepository.findRandQuestionsByQuizId(quizId, round);
261261

262262
return randomQuestions;
263263
}
264+
265+
@Transactional(readOnly = true)
266+
public Quiz findQuizById(Long quizId) {
267+
return quizRepository.findById(quizId)
268+
.orElseThrow(() -> new CustomException(QuizErrorCode.QUIZ_NOT_FOUND));
269+
}
264270
}

0 commit comments

Comments
 (0)