Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gameresult guest_id = null #90

Merged
merged 2 commits into from
Jun 24, 2024
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 @@ -13,6 +13,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
Expand All @@ -21,6 +22,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class GameresultService {

private final GameresultRepository gameresultRepository;
Expand Down Expand Up @@ -50,7 +52,7 @@ public void save(String chatroomId, Set<String> players ,String hostCode, String

Gameresult saveGameresult = null;

//각 멤버별 gameresult 저장
//각 멤버 gameresult에 저장
for (String player : players) {
Member member = memberRepository.findByLoginId(player)
.orElseThrow(() -> new UsernameNotFoundException("유저를 찾을 수 없습니다."));
Expand All @@ -61,8 +63,13 @@ public void save(String chatroomId, Set<String> players ,String hostCode, String
} else {
gameresult.setGuestId(member.getLoginId());
}
}

//멤버별로 gameresult에 저장
for (String player : players) {
Member member = memberRepository.findByLoginId(player)
.orElseThrow(() -> new UsernameNotFoundException("유저를 찾을 수 없습니다."));

// gameresult를 한 번만 저장
if (saveGameresult == null) {
saveGameresult = gameresultRepository.save(gameresult);
}
Expand Down Expand Up @@ -103,7 +110,11 @@ public List<GameresultsResponse> findGameresultList(String loginId) {
*/
public GameresultResponse findGameresult(String loginId, Long GameresultId) {
Gameresult GameresultByMemberIdAndGameresultId = gameresultRepository.findGameresultByMemberIdAndGameresultId(loginId, GameresultId);
return toGameresult(GameresultByMemberIdAndGameresultId);
if(GameresultByMemberIdAndGameresultId == null){
throw new IllegalStateException("게임결과가 없습니다.");
}else {
return toGameresult(GameresultByMemberIdAndGameresultId);
}
}

public List<Gameresult> findAllGameresult() {
Expand All @@ -120,8 +131,8 @@ private GameresultResponse toGameresult(Gameresult Gameresult) {
.guestId(Gameresult.getGuestId())
.hostCodeLanguage(Gameresult.getHostCodeLanguage())
.guestCodeLanguage(Gameresult.getGuestCodeLanguage())
.title(Gameresult.getAlgorithmproblemId().getTitle())
.gameOverType(Gameresult.getGameOverType())
.title(Gameresult.getAlgorithmproblemId().getTitle())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ void setup() {
sequence += 1L;
}

@AfterEach
void afterEach() {
//매 테스트마다 테이블 초기화
//H2 Database
List<String> truncateQueries = jdbcTemplate.queryForList(
"SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, ';') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class);
jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY FALSE");
truncateQueries.forEach(jdbcTemplate::execute);
jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY TRUE");
//MySQL
// jdbcTemplate.execute("SET foreign_key_checks = 0;");
// jdbcTemplate.execute("TRUNCATE TABLE member");
// jdbcTemplate.execute("SET foreign_key_checks = 1;");
}
// @AfterEach
// void afterEach() {
// //매 테스트마다 테이블 초기화
// //H2 Database
// List<String> truncateQueries = jdbcTemplate.queryForList(
// "SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, ';') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class);
// jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY FALSE");
// truncateQueries.forEach(jdbcTemplate::execute);
// jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY TRUE");
// //MySQL
//// jdbcTemplate.execute("SET foreign_key_checks = 0;");
//// jdbcTemplate.execute("TRUNCATE TABLE member");
//// jdbcTemplate.execute("SET foreign_key_checks = 1;");
// }


@Test
Expand Down Expand Up @@ -180,11 +180,10 @@ void OneToOneGameToGameResult() {
gameSessionService.savePlayerCode(user4info.getId(),gameCodeRequestUser2);


long gameNumber = ++sequence;
//유저3 게임결과
GameresultResponse gameresultByUser3 = gameresultService.findGameresult(user3info.getId(), gameNumber);
GameresultResponse gameresultByUser3 = gameresultService.findGameresult(user3info.getId(), 1L);
//유저4 게임결과
GameresultResponse gameresultByUser4 = gameresultService.findGameresult(user4info.getId(), gameNumber);
GameresultResponse gameresultByUser4 = gameresultService.findGameresult(user4info.getId(), 1L);

assertEquals(gameresultByUser3.getHostCodeContent(), gameresultByUser4.getHostCodeContent());
assertEquals(gameresultByUser3.getGuestCodeContent(), gameresultByUser4.getGuestCodeContent());
Expand Down