diff --git a/src/main/java/com/gg/server/admin/tournament/service/TournamentAdminService.java b/src/main/java/com/gg/server/admin/tournament/service/TournamentAdminService.java index 331975f13..51018a1cf 100644 --- a/src/main/java/com/gg/server/admin/tournament/service/TournamentAdminService.java +++ b/src/main/java/com/gg/server/admin/tournament/service/TournamentAdminService.java @@ -91,19 +91,6 @@ public Tournament updateTournamentInfo(Long tournamentId, TournamentAdminUpdateR return tournamentRepository.save(targetTournament); } - /*** - * 토너먼트 게임 테이블 생성 Method - * @param tournament 토너먼트 게임에 매칭될 토너먼트 - * @param cnt 토너먼트 전체 라운드 수 - */ - private void createTournamentGameList(Tournament tournament, int cnt) { - TournamentRound[] rounds = TournamentRound.values(); - while (--cnt >= 0) { - TournamentGame tournamentGame = new TournamentGame(null, tournament, rounds[cnt]); - tournament.addTournamentGame(tournamentGame); - } - } - /** *

토너먼트 삭제 매서드

*

토너먼트는 BEFORE 인 경우에만 삭제 가능하다.

@@ -117,8 +104,6 @@ public void deleteTournament(Long tournamentId) { if (targetTournament.getStatus() != TournamentStatus.BEFORE) { throw new TournamentUpdateException("already started or ended", ErrorCode.TOURNAMENT_NOT_BEFORE); } - List tournamentGameList = tournamentGameRepository.findAllByTournamentId(targetTournament.getId()); - tournamentGameRepository.deleteAll(tournamentGameList); tournamentRepository.deleteById(tournamentId); } @@ -185,6 +170,19 @@ public void deleteTournamentUser(Long tournamentId, Long userId) { tournamentUserRepository.delete(targetTournamentUser); } + /*** + * 토너먼트 게임 테이블 생성 Method + * @param tournament 토너먼트 게임에 매칭될 토너먼트 + * @param cnt 토너먼트 전체 라운드 수 + */ + private void createTournamentGameList(Tournament tournament, int cnt) { + TournamentRound[] rounds = TournamentRound.values(); + while (--cnt >= 0) { + TournamentGame tournamentGame = new TournamentGame(null, tournament, rounds[cnt]); + tournament.addTournamentGame(tournamentGame); + } + } + /** * 토너먼트 시간 체크 : * [ 현재 시간 + 최소 2일 ], diff --git a/src/main/java/com/gg/server/domain/tournament/data/Tournament.java b/src/main/java/com/gg/server/domain/tournament/data/Tournament.java index 20a2ccf40..a447abd15 100644 --- a/src/main/java/com/gg/server/domain/tournament/data/Tournament.java +++ b/src/main/java/com/gg/server/domain/tournament/data/Tournament.java @@ -52,10 +52,10 @@ public class Tournament extends BaseTimeEntity { @JoinColumn(name = "winner_id") private User winner; - @OneToMany(mappedBy = "tournament", cascade = CascadeType.PERSIST) + @OneToMany(mappedBy = "tournament", cascade = CascadeType.ALL) private List tournamentGames = new ArrayList<>(); - @OneToMany(mappedBy = "tournament") + @OneToMany(mappedBy = "tournament", cascade = CascadeType.ALL) private List tournamentUsers = new ArrayList<>(); @Builder diff --git a/src/main/resources/db/migration/V3__migration_42gg_5th.sql b/src/main/resources/db/migration/V3__migration_42gg_5th.sql index 22954cbea..b81b2e51b 100644 --- a/src/main/resources/db/migration/V3__migration_42gg_5th.sql +++ b/src/main/resources/db/migration/V3__migration_42gg_5th.sql @@ -24,7 +24,9 @@ CREATE TABLE tournament_user ( created_at DATETIME NOT NULL, modified_at DATETIME NOT NULL, PRIMARY KEY (id), - FOREIGN KEY (tournament_id) REFERENCES tournament(id), + FOREIGN KEY (tournament_id) REFERENCES tournament(id) + ON UPDATE CASCADE + ON DELETE CASCADE , FOREIGN KEY (user_id) REFERENCES user(id) ); @@ -37,6 +39,8 @@ CREATE TABLE tournament_game ( created_at DATETIME NOT NULL, modified_at DATETIME NOT NULL, PRIMARY KEY (id), - FOREIGN KEY (tournament_id) REFERENCES tournament(id), + FOREIGN KEY (tournament_id) REFERENCES tournament(id) + ON UPDATE CASCADE + ON DELETE CASCADE , FOREIGN KEY (game_id) REFERENCES game(id) ); diff --git a/src/test/java/com/gg/server/admin/tournament/service/TournamentAdminServiceTest.java b/src/test/java/com/gg/server/admin/tournament/service/TournamentAdminServiceTest.java index 4cfcc152f..c51bf0756 100644 --- a/src/test/java/com/gg/server/admin/tournament/service/TournamentAdminServiceTest.java +++ b/src/test/java/com/gg/server/admin/tournament/service/TournamentAdminServiceTest.java @@ -288,7 +288,6 @@ void success() { getTargetTime(2, 1), getTargetTime(2, 3)); List tournamentGameList = createTournamentGames(1L, tournament, tournamentGameCnt); given(tournamentRepository.findById(1L)).willReturn(Optional.of(tournament)); - given(tournamentGameRepository.findAllByTournamentId(tournament.getId())).willReturn(tournamentGameList); // when, then tournamentAdminService.deleteTournament(tournament.getId()); }