Skip to content

Commit

Permalink
๐Ÿ”จ [Refactor] Cascade ์ถ”๊ฐ€ (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
SONGS4RI authored Dec 6, 2023
1 parent 76010a6 commit 0a992e4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
* <p>ํ† ๋„ˆ๋จผํŠธ ์‚ญ์ œ ๋งค์„œ๋“œ</p>
* <p>ํ† ๋„ˆ๋จผํŠธ๋Š” BEFORE ์ธ ๊ฒฝ์šฐ์—๋งŒ ์‚ญ์ œ ๊ฐ€๋Šฅํ•˜๋‹ค.</p>
Expand All @@ -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<TournamentGame> tournamentGameList = tournamentGameRepository.findAllByTournamentId(targetTournament.getId());
tournamentGameRepository.deleteAll(tournamentGameList);
tournamentRepository.deleteById(tournamentId);
}

Expand Down Expand Up @@ -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์ผ ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TournamentGame> tournamentGames = new ArrayList<>();

@OneToMany(mappedBy = "tournament")
@OneToMany(mappedBy = "tournament", cascade = CascadeType.ALL)
private List<TournamentUser> tournamentUsers = new ArrayList<>();

@Builder
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/db/migration/V3__migration_42gg_5th.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand All @@ -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)
);
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ void success() {
getTargetTime(2, 1), getTargetTime(2, 3));
List<TournamentGame> 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());
}
Expand Down

0 comments on commit 0a992e4

Please sign in to comment.