Skip to content

Commit

Permalink
Core server: PR#90
Browse files Browse the repository at this point in the history
  • Loading branch information
Berygna committed Jun 24, 2024
1 parent 4977f2a commit 4e36c6c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
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

0 comments on commit 4e36c6c

Please sign in to comment.