Member와 Team을 만들었던 것과 비슷하므로 추가적인 개념이 있을 경우만 설명하겠습니다.
LeagueHost == League의 요구사항과 같다고 볼 수 있습니다.
- LeagueHost는 League를 개최할 수 있습니다.
- LeagueHost는 League 참가를 희망한 팀의 리스트를 확인할 수 있으며 수락이 가능합니다.
1. LeagueService, LeagueServiceImpl 추가
| public interface LeagueService { |
| Long createLeague(LeagueDto leagueDto, Long userId); |
| void acceptLeague(Long requestLeagueId); |
| List<RequestLeagueDto> findRequestList(Long leagueId); |
| List<RestLeagueDto> findRestLeagueSeat(); |
| RestLeagueDto findLeagueByName(String leagueName); |
| } |
| package com.example.league.service; |
| |
| |
| import com.example.league.domain.League; |
| import com.example.league.domain.ROLE; |
| import com.example.league.domain.Team; |
| import com.example.league.domain.User; |
| import com.example.league.domain.request.RequestLeague; |
| import com.example.league.dto.LeagueDto; |
| import com.example.league.dto.RequestLeagueDto; |
| import com.example.league.dto.RestLeagueDto; |
| import com.example.league.exception.*; |
| import com.example.league.repository.LeagueRepository; |
| import com.example.league.repository.RequestLeagueRepository; |
| import com.example.league.repository.UserRepository; |
| import lombok.RequiredArgsConstructor; |
| import org.springframework.data.domain.Sort; |
| import org.springframework.stereotype.Service; |
| import org.springframework.transaction.annotation.Transactional; |
| |
| import java.util.List; |
| import java.util.stream.Collectors; |
| |
| @Service |
| @Transactional |
| @RequiredArgsConstructor |
| public class LeagueServiceImpl implements LeagueService{ |
| |
| private final LeagueRepository leagueRepository; |
| private final RequestLeagueRepository requestLeagueRepository; |
| private final UserRepository userRepository; |
| @Override |
| public Long createLeague(LeagueDto leagueDto, Long userId) { |
| User user = userRepository.findById(userId).get(); |
| checkDuplicateTeamName(leagueDto); |
| League league = League.createLeague(leagueDto.getSize(), leagueDto.getName(),user); |
| return leagueRepository.save(league).getId(); |
| } |
| |
| @Override |
| public void acceptLeague(Long requestLeagueId) { |
| RequestLeague requestLeague = requestLeagueRepository.findWithFetchJoinUserById(requestLeagueId).get(); |
| Team team = requestLeague.getTeam(); |
| |
| if(requestLeague.getRequest()){ |
| throw new AlreadyAcceptTeamException("이미 승인이 완료된 신청입니다."); |
| } |
| |
| if(team.getLeague()!=null){ |
| throw new AlreadyLeagueException("이미 리그에 참가하고 있습니다."); |
| } |
| |
| requestLeague.acceptRequest(); |
| requestLeague.increaseLeagueSize(); |
| } |
| |
| @Override |
| public List<RequestLeagueDto> findRequestList(Long leagueId) { |
| return requestLeagueRepository.findByLeagueId(leagueId, Sort.by(Sort.Direction.DESC)) |
| .stream().map(a -> new RequestLeagueDto(a.getRequest(),a.getLeague().getLeagueName())) |
| .collect(Collectors.toList()); |
| } |
| |
| @Override |
| public List<RestLeagueDto> findRestLeagueSeat() { |
| return leagueRepository.findByRestSeat().stream().map(a -> new RestLeagueDto(a.getLeagueName(), |
| a.getScaleSize(),a.getNowSize())).collect(Collectors.toList()); |
| } |
| |
| @Override |
| public RestLeagueDto findLeagueByName(String leagueName) { |
| League league = leagueRepository.findByName(leagueName).orElseThrow( |
| () -> { |
| throw new NotExistLeagueNameException("해당 이름의 리그는 없습니다."); |
| } |
| ); |
| |
| return new RestLeagueDto(league.getLeagueName(),league.getScaleSize(),league.getNowSize()); |
| |
| } |
| private void checkDuplicateTeamName(LeagueDto leagueDto) { |
| leagueRepository.findByName(leagueDto.getName()).ifPresent( |
| a -> { |
| throw new DuplicateLeagueNameException("이미 같은 이름의 리그가 있습니다."); |
| } |
| ); |
| } |
| } |
2. 예외 클래스 추가
| public class AlreadyLeagueException extends RuntimeException{ |
| public AlreadyLeagueException() { |
| super(); |
| } |
| |
| public AlreadyLeagueException(String message) { |
| super(message); |
| } |
| |
| public AlreadyLeagueException(String message, Throwable cause) { |
| super(message, cause); |
| } |
| |
| public AlreadyLeagueException(Throwable cause) { |
| super(cause); |
| } |
| |
| protected AlreadyLeagueException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { |
| super(message, cause, enableSuppression, writableStackTrace); |
| } |
| } |
| public class AlreadyAcceptTeamException extends RuntimeException{ |
| public AlreadyAcceptTeamException() { |
| super(); |
| } |
| |
| public AlreadyAcceptTeamException(String message) { |
| super(message); |
| } |
| |
| public AlreadyAcceptTeamException(String message, Throwable cause) { |
| super(message, cause); |
| } |
| |
| public AlreadyAcceptTeamException(Throwable cause) { |
| super(cause); |
| } |
| |
| protected AlreadyAcceptTeamException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { |
| super(message, cause, enableSuppression, writableStackTrace); |
| } |
| } |
| public class DuplicateLeagueNameException extends RuntimeException { |
| public DuplicateLeagueNameException() { |
| super(); |
| } |
| |
| public DuplicateLeagueNameException(String message) { |
| super(message); |
| } |
| |
| public DuplicateLeagueNameException(String message, Throwable cause) { |
| super(message, cause); |
| } |
| |
| public DuplicateLeagueNameException(Throwable cause) { |
| super(cause); |
| } |
| |
| protected DuplicateLeagueNameException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { |
| super(message, cause, enableSuppression, writableStackTrace); |
| } |
| } |
| public class NotExistLeagueNameException extends RuntimeException{ |
| public NotExistLeagueNameException() { |
| super(); |
| } |
| |
| public NotExistLeagueNameException(String message) { |
| super(message); |
| } |
| |
| public NotExistLeagueNameException(String message, Throwable cause) { |
| super(message, cause); |
| } |
| |
| public NotExistLeagueNameException(Throwable cause) { |
| super(cause); |
| } |
| |
| protected NotExistLeagueNameException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { |
| super(message, cause, enableSuppression, writableStackTrace); |
| } |
| } |
| public class NotHaveAuthorityMakeLeagueException extends RuntimeException{ |
| public NotHaveAuthorityMakeLeagueException() { |
| super(); |
| } |
| |
| public NotHaveAuthorityMakeLeagueException(String message) { |
| super(message); |
| } |
| |
| public NotHaveAuthorityMakeLeagueException(String message, Throwable cause) { |
| super(message, cause); |
| } |
| |
| public NotHaveAuthorityMakeLeagueException(Throwable cause) { |
| super(cause); |
| } |
| |
| protected NotHaveAuthorityMakeLeagueException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { |
| super(message, cause, enableSuppression, writableStackTrace); |
| } |
| } |
| public class OverMaxLeagueSizeException extends RuntimeException{ |
| protected OverMaxLeagueSizeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { |
| super(message, cause, enableSuppression, writableStackTrace); |
| } |
| |
| public OverMaxLeagueSizeException() { |
| super(); |
| } |
| |
| public OverMaxLeagueSizeException(String message) { |
| super(message); |
| } |
| |
| public OverMaxLeagueSizeException(String message, Throwable cause) { |
| super(message, cause); |
| } |
| |
| public OverMaxLeagueSizeException(Throwable cause) { |
| super(cause); |
| } |
| } |
3. LeagueRepository 추가
| @Query("select l from League l where l.leagueName= :leagueName") |
| Optional<League> findByName(@Param("leagueName") String leagueName); |
| |
| @Query("select l from League l where l.scaleSize>l.nowSize") |
| List<League> findByRestSeat(); |
4. LeagueRequestRepository 추가
| @Query("select re from RequestLeague re join fetch re.team where re.id= :requestId") |
| Optional<RequestLeague> findWithFetchJoinUserById(@Param("requestId")Long requestId); |
| |
| @Query("select re from RequestLeague re join fetch re.league join fetch re.team where re.league.id= :leagueId") |
| List<RequestLeague> findByLeagueId(@Param("leagueId") Long leagueId); |
5. Test 코드
| @SpringBootTest |
| @Transactional |
| class LeagueServiceImplTest { |
| |
| |
| @Autowired |
| LeagueRepository leagueRepository; |
| |
| @Autowired |
| LeagueServiceImpl leagueService; |
| |
| @Autowired |
| RequestLeagueRepository requestLeagueRepository; |
| |
| @Autowired |
| TeamRepository teamRepository; |
| |
| @Autowired |
| UserRepository userRepository; |
| |
| @Autowired |
| UserService userService; |
| |
| @Autowired |
| TeamService teamService; |
| |
| |
| |
| @Test |
| void createLeagueSuccess(){ |
| Address address = getAddress(); |
| User host = getHost(address); |
| Long hostId = userService.signup(new UserDto(host.getEmail(), host.getPassword(), host.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long leagueId = leagueService.createLeague(new LeagueDto("전국대회", 10), hostId); |
| League league = leagueRepository.findById(leagueId).get(); |
| |
| assertThat(league.getLeagueName()).isEqualTo("전국대회"); |
| assertThat(league.getEmail()).isEqualTo(host.getEmail()); |
| assertThat(league.getScaleSize()).isEqualTo(10); |
| } |
| |
| @Test |
| void createLeagueFailDuplicateName(){ |
| Address address = getAddress(); |
| User host = getHost(address); |
| Long hostId = userService.signup(new UserDto(host.getEmail(), host.getPassword(), host.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long leagueId = leagueService.createLeague(new LeagueDto("전국대회", 10), hostId); |
| League league = leagueRepository.findById(leagueId).get(); |
| |
| assertThatThrownBy(() -> leagueService.createLeague(new LeagueDto("전국대회",10),hostId)) |
| .isInstanceOf(DuplicateLeagueNameException.class); |
| } |
| @Test |
| void acceptRequestSuccess(){ |
| Address address = getAddress(); |
| User founder = getFounder(address); |
| User host = getHost(address); |
| Long founderId = userService.signup(new UserDto(founder.getEmail(), founder.getPassword(), founder.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long hostId = userService.signup(new UserDto(host.getEmail(), host.getPassword(), host.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long team1Id = teamService.createTeam(new TeamDto("상명", 10), founderId); |
| Long leagueId = leagueService.createLeague(new LeagueDto("전국대회", 10), hostId); |
| Long requestId = teamService.requestLeague(leagueId, team1Id); |
| leagueService.acceptLeague(requestId); |
| |
| RequestLeague requestLeague = requestLeagueRepository.findById(requestId).get(); |
| |
| Team team = requestLeague.getTeam(); |
| League league = requestLeague.getLeague(); |
| |
| assertThat(team.getEmail()).isEqualTo(founder.getEmail()); |
| assertThat(league.getEmail()).isEqualTo(host.getEmail()); |
| assertThat(league.getNowSize()).isEqualTo(1); |
| } |
| |
| @Test |
| void acceptRequestFailAlreadyJoinLeague() { |
| Address address = getAddress(); |
| User founder = getFounder(address); |
| User host = getHost(address); |
| Long founderId = userService.signup(new UserDto(founder.getEmail(), founder.getPassword(), founder.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long hostId = userService.signup(new UserDto(host.getEmail(), host.getPassword(), host.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long team1Id = teamService.createTeam(new TeamDto("상명", 10), founderId); |
| Long leagueId = leagueService.createLeague(new LeagueDto("전국대회", 10), hostId); |
| Long requestId = teamService.requestLeague(leagueId, team1Id); |
| leagueService.acceptLeague(requestId); |
| |
| RequestLeague requestLeague = requestLeagueRepository.findById(requestId).get(); |
| |
| assertThatThrownBy(() -> leagueService.acceptLeague(requestId)) |
| .isInstanceOf(AlreadyAcceptTeamException.class); |
| } |
| |
| @Test |
| void findRequestList(){ |
| Address address = getAddress(); |
| User founder = getFounder(address); |
| User host = getHost(address); |
| Long founderId = userService.signup(new UserDto(founder.getEmail(), founder.getPassword(), founder.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long hostId = userService.signup(new UserDto(host.getEmail(), host.getPassword(), host.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long team1Id = teamService.createTeam(new TeamDto("상명", 10), founderId); |
| Long leagueId = leagueService.createLeague(new LeagueDto("전국대회", 10), hostId); |
| Long requestId = teamService.requestLeague(leagueId, team1Id); |
| List<RequestLeagueDto> requestList = leagueService.findRequestList(leagueId); |
| assertThat(requestList.size()).isEqualTo(1); |
| assertThat(requestList.get(0).getTeamName()).isEqualTo("상명"); |
| } |
| |
| @Test |
| void findRestLeagueList(){ |
| Address address = getAddress(); |
| User founder = getFounder(address); |
| User host = getHost(address); |
| Long founderId = userService.signup(new UserDto(founder.getEmail(), founder.getPassword(), founder.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long hostId = userService.signup(new UserDto(host.getEmail(), host.getPassword(), host.getName() |
| , "user", address.getCity(), address.getStreet())); |
| Long team1Id = teamService.createTeam(new TeamDto("상명", 10), founderId); |
| Long leagueId1 = leagueService.createLeague(new LeagueDto("전국대회1", 10), hostId); |
| Long leagueId2= leagueService.createLeague(new LeagueDto("전국대회2", 1), hostId); |
| |
| Long requestId = teamService.requestLeague(leagueId2, team1Id); |
| |
| leagueService.acceptLeague(requestId); |
| |
| List<RestLeagueDto> restLeagueSeat = leagueService.findRestLeagueSeat(); |
| |
| assertThat(restLeagueSeat.size()).isEqualTo(1); |
| assertThat(restLeagueSeat.get(0).getLeagueName()).isEqualTo("전국대회1"); |
| } |
| |
| |
| |
| |
| |
| private Address getAddress() { |
| return Address.builder() |
| .street("s") |
| .city("a") |
| .build(); |
| } |
| |
| private User getHost(Address address) { |
| return User.createUser("rkdlem48", "asdf", "kim", address, ROLE.LEAGUE_HOST); |
| } |
| private User getFounder(Address address){ |
| return User.createUser("rkdlem2", "asdf", "kim", address, ROLE.TEAM_FOUNDER); |
| |
| } |
League는 Team, Member와 매우 유사하므로 이미 설명한 부분들에 대해선 언급을 하지 않았습니다. 다음으로는 LeagueController, Interceptor, controllerAdvice를 만들어보겠습니다. 감사합니다.
모든 코드는 아래 링크에서 확인 가능합니다.
https://github.com/rlaehdals/blogProject
GitHub - rlaehdals/blogProject
Contribute to rlaehdals/blogProject development by creating an account on GitHub.
github.com
댓글을 사용할 수 없습니다.