-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Nexters/dev
Dev -> Main 08.17
- Loading branch information
Showing
14 changed files
with
251 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/nexters/moyeomoyeo/team_building/service/TeamBuildingAdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.nexters.moyeomoyeo.team_building.service; | ||
|
||
|
||
import static com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamBuildingResponse.TeamBuildingInfo.makeTeamBuildingInfo; | ||
import static com.nexters.moyeomoyeo.team_building.controller.dto.response.UserInfo.makeUserInfo; | ||
|
||
import com.nexters.moyeomoyeo.common.constant.ExceptionInfo; | ||
import com.nexters.moyeomoyeo.notification.service.NotificationService; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.request.TeamBuildingRequest; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.request.TeamRequest; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamBuildingResponse; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamInfo; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.UserInfo; | ||
import com.nexters.moyeomoyeo.team_building.domain.constant.RoundStatus; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.Team; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.TeamBuilding; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.User; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TeamBuildingAdminService { | ||
|
||
private final TeamBuildingService teamBuildingService; | ||
private final UserService userService; | ||
private final NotificationService notificationService; | ||
|
||
@Transactional | ||
public UserInfo adjustUser(String teamBuildingUuid, String userUuid, String teamUuid) { | ||
final TeamBuilding teamBuilding = teamBuildingService.findByUuid(teamBuildingUuid); | ||
|
||
if (RoundStatus.ADJUSTED_ROUND != teamBuilding.getRoundStatus()) { | ||
throw ExceptionInfo.INVALID_ADJUST_REQUEST.exception(); | ||
} | ||
|
||
final User user = userService.findByUuid(userUuid); | ||
final Team targetTeam = teamBuilding.getTeams() | ||
.stream() | ||
.filter(team -> Objects.equals(team.getUuid(), teamUuid)) | ||
.findFirst() | ||
.orElse(null); | ||
user.adjustTeam(targetTeam); | ||
UserInfo userInfo = makeUserInfo(user); | ||
|
||
notificationService.broadCast(teamBuildingUuid, "adjust-user", userInfo); | ||
return userInfo; | ||
} | ||
|
||
|
||
@Transactional | ||
public void finishTeamBuilding(String uuid) { | ||
final TeamBuilding teamBuilding = teamBuildingService.findByUuid(uuid); | ||
|
||
if (RoundStatus.ADJUSTED_ROUND != teamBuilding.getRoundStatus()) { | ||
throw ExceptionInfo.INVALID_FINISH_REQUEST.exception(); | ||
} | ||
|
||
teamBuilding.nextRound(); | ||
notificationService.broadCast(teamBuilding.getUuid(), "finish-team-building", teamBuilding.getRoundStatus()); | ||
} | ||
|
||
|
||
@Transactional | ||
public TeamBuildingResponse createTeamBuilding(TeamBuildingRequest teamBuildingRequest) { | ||
final TeamBuilding teamBuilding = TeamBuilding.builder() | ||
.name(teamBuildingRequest.getName()) | ||
.build(); | ||
|
||
final List<Team> teams = teamBuildingRequest.getTeams() | ||
.stream() | ||
.map(TeamRequest::toEntity) | ||
.toList(); | ||
|
||
for (final Team team : teams) { | ||
team.addTeamBuilding(teamBuilding); | ||
} | ||
|
||
final TeamBuilding savedTeamBuilding = teamBuildingService.save(teamBuilding); | ||
|
||
return TeamBuildingResponse.builder() | ||
.teamBuildingInfo(makeTeamBuildingInfo(savedTeamBuilding)) | ||
.teamInfoList(savedTeamBuilding.getTeams().stream().map(TeamInfo::makeTeamInfo).toList()) | ||
.build(); | ||
} | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
src/main/java/com/nexters/moyeomoyeo/team_building/service/TeamBuildingCoreService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package com.nexters.moyeomoyeo.team_building.service; | ||
|
||
import static com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamBuildingResponse.TeamBuildingInfo.makeTeamBuildingInfo; | ||
import static com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamInfo.isSelectDone; | ||
|
||
import com.nexters.moyeomoyeo.common.constant.ExceptionInfo; | ||
import com.nexters.moyeomoyeo.notification.service.NotificationService; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.request.UserPickRequest; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.PickUserResponse; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamBuildingResponse; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.TeamInfo; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.UserInfo; | ||
import com.nexters.moyeomoyeo.team_building.controller.dto.response.UserPickResponse; | ||
import com.nexters.moyeomoyeo.team_building.domain.constant.RoundStatus; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.Team; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.TeamBuilding; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.User; | ||
import com.nexters.moyeomoyeo.team_building.domain.entity.UserChoice; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TeamBuildingCoreService { | ||
|
||
private final TeamBuildingService teamBuildingService; | ||
private final UserService userService; | ||
private final NotificationService notificationService; | ||
|
||
/** | ||
* 선택된 유저가 현재 라운드에서 지망한 팀이 유저를 선택한 팀과 일치하는지 유효성 체크. | ||
* | ||
* @param team 유저를 선택한 팀 | ||
* @param pickedUsers 선택된 유저 목록 | ||
* @return 일치여부 | ||
*/ | ||
private static boolean isChosenTeam(Team team, List<User> pickedUsers) { | ||
final RoundStatus roundStatus = team.getRoundStatus(); | ||
for (final User user : pickedUsers) { | ||
final UserChoice choice = user.findChoice(roundStatus.getWeight()); | ||
if (!Objects.equals(choice.getTeamUuid(), team.getUuid())) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* 모든 팀이 선택을 완료했는지 판단. | ||
* | ||
* @param teams team building 의 team 들 | ||
* @param roundStatus 전체 round 상태 | ||
* @return 완료됐으면 true | ||
*/ | ||
private static boolean isAllTeamSelected(List<Team> teams, RoundStatus roundStatus) { | ||
for (final Team team : teams) { | ||
if (roundStatus == team.getRoundStatus()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public TeamBuildingResponse findTeamBuilding(String teamBuildingUuid) { | ||
final TeamBuilding teamBuilding = teamBuildingService.findByUuid(teamBuildingUuid); | ||
|
||
final List<User> users = userService.findByTeamBuildingId(teamBuildingUuid); | ||
|
||
return TeamBuildingResponse.builder() | ||
.teamBuildingInfo(makeTeamBuildingInfo(teamBuilding)) | ||
.teamInfoList(teamBuilding.getTeams().stream().map(TeamInfo::makeTeamInfo).toList()) | ||
.userInfoList(users.stream().map(UserInfo::makeUserInfo).toList()) | ||
.build(); | ||
} | ||
|
||
@Transactional | ||
public UserPickResponse pickUsers(String teamBuildingUuid, String teamUuid, UserPickRequest userPickRequest) { | ||
final TeamBuilding teamBuilding = teamBuildingService.findByUuid(teamBuildingUuid); | ||
final RoundStatus teamBuildingRoundStatus = teamBuilding.getRoundStatus(); | ||
|
||
if (RoundStatus.COMPLETE == teamBuildingRoundStatus) { | ||
throw ExceptionInfo.COMPLETED_TEAM_BUILDING.exception(); | ||
} | ||
|
||
final Team targetTeam = teamBuilding.getTeams() | ||
.stream() | ||
.filter(team -> teamUuid.equals(team.getUuid())) | ||
.findFirst() | ||
.orElseThrow(ExceptionInfo.INVALID_TEAM_UUID::exception); | ||
|
||
if (isSelectDone(teamBuildingRoundStatus, targetTeam.getRoundStatus())) { | ||
throw ExceptionInfo.DUPLICATED_PICK_REQUEST.exception(); | ||
} | ||
|
||
final List<String> userUuids = userPickRequest.getUserUuids(); | ||
final List<User> pickedUsers = userService.findByUuidIn(userUuids); | ||
|
||
if (!isChosenTeam(targetTeam, pickedUsers)) { | ||
throw ExceptionInfo.BAD_REQUEST_FOR_USER_PICK.exception(); | ||
} | ||
|
||
for (final User user : pickedUsers) { | ||
user.addTeam(targetTeam); | ||
} | ||
targetTeam.nextRound(); | ||
|
||
PickUserResponse userResponse = PickUserResponse.builder() | ||
.teamName(targetTeam.getName()) | ||
.teamUuid(teamUuid) | ||
.pickUserUuids(userUuids) | ||
.build(); | ||
|
||
notificationService.broadCast(teamBuilding.getUuid(), "pick-user", userResponse); | ||
|
||
if (isAllTeamSelected(teamBuilding.getTeams(), teamBuildingRoundStatus)) { | ||
final RoundStatus nextStatus = teamBuilding.nextRound(); | ||
notificationService.broadCast(teamBuilding.getUuid(), "change-round", nextStatus); | ||
} | ||
|
||
return UserPickResponse.builder() | ||
.userInfoList(targetTeam.getUsers().stream().map(UserInfo::makeUserInfo).toList()) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.