Skip to content

Commit

Permalink
fix: 팀 존재 여부 확인 쿼리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
EunjiShin committed Jul 17, 2024
1 parent 83855f7 commit a173f5c
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public static class BaseballTeamNotFoundException extends TeamException {
public BaseballTeamNotFoundException() {
super(TeamErrorCode.BASEBALL_TEAM_NOT_FOUND);
}

public BaseballTeamNotFoundException(Long id) {
super(TeamErrorCode.BASEBALL_TEAM_NOT_FOUND.appended(" : " + id));
}
}

public static class InvalidBaseballTeamNameException extends TeamException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.depromeet.spot.jpa.team.repository;

import java.util.List;
import java.util.Set;

import org.depromeet.spot.jpa.team.entity.BaseballTeamEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BaseballTeamJpaRepository extends JpaRepository<BaseballTeamEntity, Long> {
boolean existsByNameIn(List<String> names);

boolean existsByIdIn(Set<Long> ids);
boolean existsById(Long teamId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.depromeet.spot.jpa.team.repository;

import java.util.List;
import java.util.Set;

import org.depromeet.spot.common.exception.team.TeamException.BaseballTeamNotFoundException;
import org.depromeet.spot.domain.team.BaseballTeam;
Expand Down Expand Up @@ -44,7 +43,7 @@ public boolean existsByNameIn(List<String> names) {
}

@Override
public boolean existsByIdIn(Set<Long> ids) {
return baseballTeamJpaRepository.existsByIdIn(ids);
public boolean existsById(Long id) {
return baseballTeamJpaRepository.existsById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface ReadBaseballTeamUsecase {

List<BaseballTeam> findAll();

void checkExistsBy(Set<Long> teamIds);
void areAllTeamIdsExist(Set<Long> teamIds);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.depromeet.spot.usecase.port.out.team;

import java.util.List;
import java.util.Set;

import org.depromeet.spot.domain.team.BaseballTeam;

Expand All @@ -14,5 +13,5 @@ public interface BaseballTeamRepository {

boolean existsByNameIn(List<String> names);

boolean existsByIdIn(Set<Long> ids);
boolean existsById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CreateHomeTeamService implements CreateHomeTeamUsecase {
@Override
public void createHomeTeams(final Long stadiumId, Set<Long> teamIds) {
stadiumReadUsecase.checkIsExistsBy(stadiumId);
readBaseballTeamUsecase.checkExistsBy(teamIds);
readBaseballTeamUsecase.areAllTeamIdsExist(teamIds);
List<StadiumHomeTeam> homeTeams = new ArrayList<>();
teamIds.forEach(
teamId ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public List<BaseballTeam> findAll() {
}

@Override
public void checkExistsBy(Set<Long> teamIds) {
if (!baseballTeamRepository.existsByIdIn(teamIds)) {
throw new BaseballTeamNotFoundException();
public void areAllTeamIdsExist(Set<Long> teamIds) {
for (Long teamId : teamIds) {
if (!baseballTeamRepository.existsById(teamId)) {
throw new BaseballTeamNotFoundException(teamId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

import org.depromeet.spot.common.exception.team.TeamException.BaseballTeamNotFoundException;
Expand Down Expand Up @@ -61,7 +60,7 @@ public boolean existsByNameIn(List<String> names) {
}

@Override
public boolean existsByIdIn(Set<Long> ids) {
return data.stream().map(BaseballTeam::getId).anyMatch(ids::contains);
public boolean existsById(Long id) {
return data.stream().anyMatch(team -> team.getId().equals(id));
}
}

0 comments on commit a173f5c

Please sign in to comment.