Skip to content

Commit

Permalink
[REFACTOR] 중복된 코드 제거 및 PermissionValidator 를 사용하도록 수정 #218
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin409 committed Sep 22, 2024
1 parent 747f12c commit d1a27e0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sports.server.command.game.application;

import com.sports.server.auth.exception.AuthorizationErrorMessages;
import com.sports.server.command.game.domain.Game;
import com.sports.server.command.game.domain.GameRepository;
import com.sports.server.command.game.domain.GameTeam;
Expand All @@ -11,8 +10,8 @@
import com.sports.server.command.sport.domain.Sport;
import com.sports.server.command.sport.domain.SportRepository;
import com.sports.server.common.application.EntityUtils;
import com.sports.server.common.application.PermissionValidator;
import com.sports.server.common.exception.NotFoundException;
import com.sports.server.common.exception.UnauthorizedException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -24,6 +23,7 @@ public class GameService {
private final EntityUtils entityUtils;
private final GameRepository gameRepository;
private final SportRepository sportRepository;
private final PermissionValidator permissionValidator;
private static final String NAME_OF_SPORT = "축구";

@Transactional
Expand All @@ -45,28 +45,18 @@ private void saveGameTeams(Game game, GameRequestDto.Register requestDto) {
game.addTeam(gameTeam1);
game.addTeam(gameTeam2);

leagueTeam1.getLeagueTeamPlayers().stream()
leagueTeam1.getLeagueTeamPlayers()
.forEach(gameTeam1::registerLineup);
leagueTeam2.getLeagueTeamPlayers().stream()
leagueTeam2.getLeagueTeamPlayers()
.forEach(gameTeam2::registerLineup);
}

private Game saveGame(Long leagueId, Member manager, GameRequestDto.Register requestDto) {
Sport sport = sportRepository.findByName(NAME_OF_SPORT)
.orElseThrow(() -> new NotFoundException("해당 이름을 가진 스포츠가 존재하지 않습니다."));
League league = getLeagueAndCheckPermission(leagueId, manager);
League league = permissionValidator.checkPermissionAndGet(leagueId, manager, League.class);
Game game = requestDto.toEntity(sport, manager, league);
gameRepository.save(game);
return game;
}

private League getLeagueAndCheckPermission(final Long leagueId, final Member manager) {
League league = entityUtils.getEntity(leagueId, League.class);

if (!league.isManagedBy(manager)) {
throw new UnauthorizedException(AuthorizationErrorMessages.PERMISSION_DENIED);
}

return league;
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/sports/server/command/game/domain/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sports.server.command.member.domain.Member;
import com.sports.server.command.sport.domain.Sport;
import com.sports.server.common.domain.BaseEntity;
import com.sports.server.common.domain.ManagedEntity;
import com.sports.server.common.exception.CustomException;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
Expand All @@ -29,7 +30,7 @@
@Getter
@Table(name = "games")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Game extends BaseEntity<Game> {
public class Game extends BaseEntity<Game> implements ManagedEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sport_id")
Expand Down Expand Up @@ -101,10 +102,6 @@ public void score(LineupPlayer scorer) {
scoredTeam.score();
}

public boolean isMangedBy(Member member) {
return manager.equals(member);
}

public void cancelScore(LineupPlayer scorer) {
GameTeam scoredTeam = teams.stream()
.filter(scorer::isInTeam)
Expand Down Expand Up @@ -142,4 +139,9 @@ private void validateGameTeam(final GameTeam gameTeam) {
throw new CustomException(HttpStatus.BAD_REQUEST, "해당 게임팀은 이 게임에 포함되지 않습니다.");
}
}

@Override
public boolean isManagedBy(Member manager) {
return manager.equals(manager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sports.server.command.member.domain.Member;
import com.sports.server.command.organization.domain.Organization;
import com.sports.server.common.domain.BaseEntity;
import com.sports.server.common.domain.ManagedEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -28,7 +29,7 @@
@SQLDelete(sql = "UPDATE leagues SET is_deleted = 1 WHERE id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class League extends BaseEntity<League> {
public class League extends BaseEntity<League> implements ManagedEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "manager_id")
Expand Down Expand Up @@ -86,6 +87,7 @@ public void updateInfo(String name, LocalDateTime startAt, LocalDateTime endAt,
this.maxRound = maxRound;
}

@Override
public boolean isManagedBy(Member manager) {
return this.manager.equals(manager);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sports.server.command.leagueteam.application;

import com.sports.server.auth.exception.AuthorizationErrorMessages;
import com.sports.server.command.league.domain.League;
import com.sports.server.command.leagueteam.domain.LeagueTeam;
import com.sports.server.command.leagueteam.domain.LeagueTeamPlayer;
Expand All @@ -10,8 +9,8 @@
import com.sports.server.command.leagueteam.dto.LeagueTeamRequest;
import com.sports.server.command.member.domain.Member;
import com.sports.server.common.application.EntityUtils;
import com.sports.server.common.application.PermissionValidator;
import com.sports.server.common.exception.NotFoundException;
import com.sports.server.common.exception.UnauthorizedException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -31,9 +30,10 @@ public class LeagueTeamService {
private final LeagueTeamRepository leagueTeamRepository;
private final LeagueTeamPlayerRepository leagueTeamPlayerRepository;
private final EntityUtils entityUtils;
private final PermissionValidator permissionValidator;

public void register(final Long leagueId, final Member manager, final LeagueTeamRequest.Register request) {
League league = getLeagueAndCheckPermission(leagueId, manager);
League league = permissionValidator.checkPermissionAndGet(leagueId, manager, League.class);

String imgUrl = changeLogoImageUrlToBeSaved(request.logoImageUrl());
LeagueTeam leagueTeam = request.toEntity(manager, league, imgUrl);
Expand All @@ -46,7 +46,7 @@ public void register(final Long leagueId, final Member manager, final LeagueTeam
}

public void update(Long leagueId, LeagueTeamRequest.Update request, Member manager, Long teamId) {
getLeagueAndCheckPermission(leagueId, manager);
permissionValidator.checkPermission(leagueId, manager, League.class);
LeagueTeam leagueTeam = getLeagueTeam(teamId);

leagueTeam.updateInfo(request.name(), changeLogoImageUrlToBeSaved(request.logoImageUrl()));
Expand All @@ -57,7 +57,7 @@ public void update(Long leagueId, LeagueTeamRequest.Update request, Member manag
}

public void delete(Long leagueId, Member manager, Long teamId) {
League league = getLeagueAndCheckPermission(leagueId, manager);
League league = permissionValidator.checkPermissionAndGet(leagueId, manager, League.class);
LeagueTeam leagueTeam = entityUtils.getEntity(teamId, LeagueTeam.class);
leagueTeam.isParticipate(league);

Expand Down Expand Up @@ -102,7 +102,7 @@ private void deletePlayers(LeagueTeamRequest.Update request, LeagueTeam leagueTe
}

public void deleteLogoImage(Long leagueId, Member manager, Long teamId) {
getLeagueAndCheckPermission(leagueId, manager);
permissionValidator.checkPermission(leagueId, manager, League.class);

LeagueTeam leagueTeam = entityUtils.getEntity(teamId, LeagueTeam.class);
leagueTeam.deleteLogoImageUrl();
Expand All @@ -115,13 +115,4 @@ private String changeLogoImageUrlToBeSaved(String logoImageUrl) {
return logoImageUrl.replace(originPrefix, replacePrefix);
}

private League getLeagueAndCheckPermission(final Long leagueId, final Member manager) {
League league = entityUtils.getEntity(leagueId, League.class);

if (!league.isManagedBy(manager)) {
throw new UnauthorizedException(AuthorizationErrorMessages.PERMISSION_DENIED);
}

return league;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import com.sports.server.command.timeline.domain.TimelineRepository;
import com.sports.server.command.timeline.dto.TimelineRequest;
import com.sports.server.command.timeline.mapper.TimelineMapper;
import com.sports.server.common.application.EntityUtils;
import com.sports.server.common.application.PermissionValidator;
import com.sports.server.common.exception.CustomException;
import com.sports.server.common.exception.UnauthorizedException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -19,11 +18,11 @@
@RequiredArgsConstructor
public class TimelineService {
private final TimelineRepository timelineRepository;
private final EntityUtils entityUtils;
private final PermissionValidator permissionValidator;
private final TimelineMapper timelineMapper;

public void register(Member member, Long gameId, TimelineRequest request) {
Game game = checkPermissionAndGet(gameId, member);
Game game = permissionValidator.checkPermissionAndGet(gameId, member, Game.class);

Timeline timeline = timelineMapper.toEntity(game, request);
timeline.apply();
Expand All @@ -32,24 +31,14 @@ public void register(Member member, Long gameId, TimelineRequest request) {
}

public void deleteTimeline(Member member, Long gameId, Long timelineId) {
Game game = checkPermissionAndGet(gameId, member);
Game game = permissionValidator.checkPermissionAndGet(gameId, member, Game.class);

Timeline timeline = getLastTimeline(timelineId, game);
timeline.rollback();

timelineRepository.delete(timeline);
}

private Game checkPermissionAndGet(Long gameId, Member member) {
Game game = entityUtils.getEntity(gameId, Game.class);

if (!game.isMangedBy(member)) {
throw new UnauthorizedException("타임라인 조작 권한이 없습니다.");
}

return game;
}

private Timeline getLastTimeline(Long timelineId, Game game) {
return timelineRepository.findFirstByGameOrderByIdDesc(game)
.filter(t -> t.getId().equals(timelineId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.sports.server.query.application;

import com.sports.server.auth.exception.AuthorizationErrorMessages;
import com.sports.server.command.cheertalk.domain.CheerTalk;
import com.sports.server.command.league.domain.League;
import com.sports.server.command.member.domain.Member;
import com.sports.server.common.application.EntityUtils;
import com.sports.server.common.application.PermissionValidator;
import com.sports.server.common.dto.PageRequestDto;
import com.sports.server.common.exception.UnauthorizedException;
import com.sports.server.query.dto.response.CheerTalkResponse;
import com.sports.server.query.dto.response.CheerTalkResponse.ForManager;
import com.sports.server.query.repository.CheerTalkDynamicRepository;
Expand All @@ -27,7 +25,7 @@ public class CheerTalkQueryService {

private final GameQueryRepository gameQueryRepository;

private final EntityUtils entityUtils;
private final PermissionValidator permissionValidator;

public List<CheerTalkResponse.ForSpectator> getCheerTalksByGameId(final Long gameId,
final PageRequestDto pageRequest) {
Expand All @@ -46,7 +44,7 @@ public List<CheerTalkResponse.ForSpectator> getCheerTalksByGameId(final Long gam
public List<CheerTalkResponse.ForManager> getReportedCheerTalksByLeagueId(final Long leagueId,
final PageRequestDto pageRequest,
final Member manager) {
checkPermission(leagueId, manager);
permissionValidator.checkPermission(leagueId, manager, League.class);

List<CheerTalk> reportedCheerTalks = cheerTalkDynamicRepository.findReportedCheerTalksByLeagueId(
leagueId, pageRequest.cursor(), pageRequest.size()
Expand All @@ -60,7 +58,7 @@ public List<CheerTalkResponse.ForManager> getReportedCheerTalksByLeagueId(final
public List<CheerTalkResponse.ForManager> getUnblockedCheerTalksByLeagueId(Long leagueId,
PageRequestDto pageRequest,
Member manager) {
checkPermission(leagueId, manager);
permissionValidator.checkPermission(leagueId, manager, League.class);

List<CheerTalk> cheerTalks = cheerTalkDynamicRepository.findUnblockedCheerTalksByLeagueId(
leagueId, pageRequest.cursor(), pageRequest.size()
Expand All @@ -71,13 +69,5 @@ public List<CheerTalkResponse.ForManager> getUnblockedCheerTalksByLeagueId(Long
gameQueryRepository.findByGameTeamIdWithLeague(cheerTalk.getGameTeamId()))).toList();
}

private void checkPermission(final Long leagueId, final Member manager) {
League league = entityUtils.getEntity(leagueId, League.class);

if (!league.isManagedBy(manager)) {
throw new UnauthorizedException(AuthorizationErrorMessages.PERMISSION_DENIED);
}
}


}

0 comments on commit d1a27e0

Please sign in to comment.