Skip to content

Commit

Permalink
[REFACTOR] 내 팀 조회 & 전술 적용 리팩토링 (#157) (#158)
Browse files Browse the repository at this point in the history
* ♻️ refactor: 개별 전술 조회 주석처리 (#157)

* ♻️ refactor: 내팀 목록 조회, 개별 팀 정보 조회 시 response로 전술 가져오게 (#157)

* ♻️ refactor: 프로필 생성 시 프로필 유무 부터 확인 (#157)

* ♻️ refactor: 팀 생성 시 팀이름이랑 적용전술 받아서 매핑 (#157)

* ♻️ refactor: 팀 생성 시 팀이름이랑 적용전술 받아서 매핑 (#157)

* ♻️ refactor: 전술 변경 시 팀 멤버 포지션 할당 초기화 (#157)

* ♻️ refactor: 전술 변경 시 팀 멤버 포지션 할당 초기화 (#157)
  • Loading branch information
gomin0 authored Jul 22, 2024
1 parent 11aec2f commit c8639d7
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public ProfileResponseDto createProfile(CreateProfileRequestDto requestDto, User

uploadProfile(user, profileImage, profile);

if (profileJpaRepository.existsByNickname(requestDto.nickname())) {
throw new ProfileExceptionHandler(ErrorCode.NICKNAME_ALREADY_EXIST);
}

// 프로필 이미 존재
if (profileJpaRepository.existsByUser(user)) {
throw new ProfileExceptionHandler(ErrorCode.PROFILE_ALREADY_EXIST);
}

if (profileJpaRepository.existsByNickname(requestDto.nickname())) {
throw new ProfileExceptionHandler(ErrorCode.NICKNAME_ALREADY_EXIST);
}

return ProfileResponseDto.from(profileJpaRepository.save(profile), user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ApiResponse<Object> deleteMyTeam(@PathVariable(name = "teamId") Long team
public ApiResponse<CreateTeamTacticResponseDto> addTacticOnTeam(@PathVariable(name = "teamId") Long teamId,
@PathVariable(name = "tacticId") Long tacticId,
@LoginUser User user) {
CreateTeamTacticResponseDto tacticAddedTeam = teamTacticService.addTactic(teamId, tacticId, user);
CreateTeamTacticResponseDto tacticAddedTeam = teamTacticService.changeTactic(teamId, tacticId, user);
return ApiResponse.onSuccess(tacticAddedTeam);
}

Expand All @@ -89,12 +89,12 @@ public ApiResponse<List<CreateTeamTacticResponseDto.MyTacticList>> getMyTactic(@
return ApiResponse.noContent();
}

@Operation(summary = "개별 전술 조회", description = "11개의 버튼을 눌러 각각의 개별 전술을 조회")
@GetMapping("/{teamId}/{positionDetailId}")
public ApiResponse<GetTeamResponseDto.getIndividualDetail> getIndividualDetail(@PathVariable(name = "teamId") Long teamId,
@PathVariable(name = "positionDetailId") Long positionDetailId) {
GetTeamResponseDto.getIndividualDetail positionDetail = teamQueryService.getIndividualDetail(teamId, positionDetailId);
return ApiResponse.onSuccess(positionDetail);
}
// @Operation(summary = "개별 전술 조회", description = "11개의 버튼을 눌러 각각의 개별 전술을 조회")
// @GetMapping("/{teamId}/{positionDetailId}")
// public ApiResponse<GetTeamResponseDto.getIndividualDetail> getIndividualDetail(@PathVariable(name = "teamId") Long teamId,
// @PathVariable(name = "positionDetailId") Long positionDetailId) {
// GetTeamResponseDto.getIndividualDetail positionDetail = teamQueryService.getIndividualDetail(teamId, positionDetailId);
// return ApiResponse.onSuccess(positionDetail);
// }

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.capstone.BnagFer.domain.myteam.dto.request;
import com.capstone.BnagFer.domain.myteam.entity.Team;
import com.capstone.BnagFer.domain.tactic.entity.Tactic;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record CUTeamRequestDto (
@NotBlank(message = "팀 이름 입력은 필수입니다.")
@Schema(description = "teamName", example = "맨체스터 유나이티드")
String teamName
String teamName,
@NotNull(message = "전술 ID 입력은 필수입니다.")
Long tacticId
) {
public Team toEntity() {
public Team toEntity(Tactic tactic) {
return Team.builder()
.teamName(teamName)
.tactic(tactic)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.capstone.BnagFer.domain.myteam.entity.Team;
import com.capstone.BnagFer.domain.myteam.entity.TeamMember;
import com.capstone.BnagFer.domain.tactic.entity.Position;
import com.capstone.BnagFer.domain.tactic.entity.Tactic;
import com.capstone.BnagFer.domain.tactic.entity.TacticPositionDetail;
import com.google.firebase.database.annotations.Nullable;
import lombok.*;
Expand All @@ -18,10 +19,32 @@ public record GetTeamResponseDto (
Long leaderId,
String leaderNickName,
String teamName,
TacticDetails tactic,

List<TeamTacticDetailResponse> tacticDto,
List<TeamMembersList> teamMembers,
LocalDateTime createdAt
) {

@Builder
public record TacticDetails(
Long tacticId,
String mainFormation,
String tacticName,
String tacticDetails,
String subTactic
) {
public static TacticDetails from(Tactic tactic) {
return TacticDetails.builder()
.tacticId(tactic.getTacticId())
.mainFormation(tactic.getMainFormation())
.tacticName(tactic.getTacticName())
.tacticDetails(tactic.getTacticDetails())
.subTactic(tactic.getSubTactic())
.build();
}
}

@Builder
public record TeamTacticDetailResponse(
Long detailId,
Expand All @@ -47,6 +70,7 @@ public static GetTeamResponseDto from(Team team) {
.leaderNickName(team.getLeader().getProfile().getNickname())
.teamName(team.getTeamName())
.teamMembers(TeamMembersList.from(team.getTeamMembers()))
.tactic(TacticDetails.from(team.getTactic()))
.tacticDto(TeamTacticDetailResponse.from(team.getTactic().getTacticPositionDetails()))
.createdAt(team.getCreatedAt())
.build();
Expand Down Expand Up @@ -78,13 +102,19 @@ public static List<TeamMembersList> from(List<TeamMember> teamMembers) {
@Builder
public record TeamList(
Long teamId,
Long tacticId,
String mainFormation,
String tacticName,
String teamName,
Long teamMemberId,
String leaderNickName
) {
public static TeamList from(Team team, TeamMember teamMember) {
return TeamList.builder()
.teamId(team.getId())
.tacticId(team.getTactic().getTacticId())
.mainFormation(team.getTactic().getMainFormation())
.tacticName(team.getTactic().getTacticName())
.teamName(team.getTeamName())
.teamMemberId(teamMember.getId())
.leaderNickName(team.getLeader().getProfile().getNickname())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.capstone.BnagFer.domain.myteam.exception.TeamExceptionHandler;
import com.capstone.BnagFer.domain.myteam.repository.TeamMembersRepository;
import com.capstone.BnagFer.domain.myteam.repository.TeamRepository;
import com.capstone.BnagFer.domain.tactic.entity.TacticPositionDetail;
import com.capstone.BnagFer.domain.tactic.repository.TacticPositionDetailRepository;
import com.capstone.BnagFer.global.common.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -23,7 +21,6 @@
public class TeamQueryService {

private final TeamRepository teamRepository;
private final TacticPositionDetailRepository tacticPositionDetailRepository;
private final TeamMembersRepository teamMembersRepository;

public GetTeamResponseDto getMyTeamById(Long teamId, User user) {
Expand Down Expand Up @@ -53,17 +50,17 @@ public List<GetTeamResponseDto.TeamList> getMyTeamList(User user) {
return teamLists;
}

public GetTeamResponseDto.getIndividualDetail getIndividualDetail(Long teamId, Long tacticPositionDetailId) {

Team team = teamRepository.findById(teamId).orElseThrow(() -> new TeamExceptionHandler(ErrorCode.TEAM_NOT_FOUND));
TacticPositionDetail tacticPositionDetail = tacticPositionDetailRepository.findById(tacticPositionDetailId).orElseThrow(() -> new TeamExceptionHandler(ErrorCode.DETAIL_NOT_FOUND));

List<TacticPositionDetail> tacticPositionDetails = team.getTactic().getTacticPositionDetails();
TeamMember byTeamAndPosition = teamMembersRepository.findByTeamAndPosition(team, tacticPositionDetail.getPosition());

if (tacticPositionDetails.contains(tacticPositionDetail)) {
return GetTeamResponseDto.getIndividualDetail.from(team, tacticPositionDetail, byTeamAndPosition);
} else
return GetTeamResponseDto.getIndividualDetail.from(team, tacticPositionDetail, null);
}
// public GetTeamResponseDto.getIndividualDetail getIndividualDetail(Long teamId, Long tacticPositionDetailId) {
//
// Team team = teamRepository.findById(teamId).orElseThrow(() -> new TeamExceptionHandler(ErrorCode.TEAM_NOT_FOUND));
// TacticPositionDetail tacticPositionDetail = tacticPositionDetailRepository.findById(tacticPositionDetailId).orElseThrow(() -> new TeamExceptionHandler(ErrorCode.DETAIL_NOT_FOUND));
//
// List<TacticPositionDetail> tacticPositionDetails = team.getTactic().getTacticPositionDetails();
// TeamMember byTeamAndPosition = teamMembersRepository.findByTeamAndPosition(team, tacticPositionDetail.getPosition());
//
// if (tacticPositionDetails.contains(tacticPositionDetail)) {
// return GetTeamResponseDto.getIndividualDetail.from(team, tacticPositionDetail, byTeamAndPosition);
// } else
// return GetTeamResponseDto.getIndividualDetail.from(team, tacticPositionDetail, null);
// }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package com.capstone.BnagFer.domain.myteam.service;

import com.capstone.BnagFer.domain.accounts.entity.User;
import com.capstone.BnagFer.domain.accounts.service.account.AccountsCommonService;
import com.capstone.BnagFer.domain.myteam.dto.request.CUTeamRequestDto;
Expand All @@ -10,6 +10,9 @@
import com.capstone.BnagFer.domain.myteam.exception.TeamExceptionHandler;
import com.capstone.BnagFer.domain.myteam.repository.TeamMembersRepository;
import com.capstone.BnagFer.domain.myteam.repository.TeamRepository;
import com.capstone.BnagFer.domain.tactic.entity.Tactic;
import com.capstone.BnagFer.domain.tactic.exception.TacticExceptionHandler;
import com.capstone.BnagFer.domain.tactic.repository.TacticRepository;
import com.capstone.BnagFer.global.common.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -23,17 +26,30 @@ public class TeamService {
private final TeamRepository teamRepository;
private final AccountsCommonService accountsCommonService;
private final TeamMembersRepository teamMembersRepository;
private final TacticRepository tacticRepository;

public CUTeamResponseDto createMyTeam(CUTeamRequestDto request, User user) {

Team team = request.toEntity();
// 프로필 존재 확인
accountsCommonService.checkUserProfile(user);

// 적용 가능 전술 유무 확인
if (user.getTactics().isEmpty()) {
throw new TeamExceptionHandler(ErrorCode.USER_TEAM_TACTIC_NOT_FOUND);
}


Tactic tactic = tacticRepository.findById(request.tacticId())
.orElseThrow(() -> new TacticExceptionHandler(ErrorCode.TACTIC_NOT_FOUND));

if (!user.getTactics().contains(tactic)) {
throw new TacticExceptionHandler(ErrorCode.TACTIC_NOT_ALLOWED);
}

Team team = request.toEntity(tactic);
team.updateLeader(user);
TeamMember teamMember = TeamMember.createTeamMember(user, Role.LEADER, team);

// 프로필 존재 확인
accountsCommonService.checkUserProfile(team.getLeader());

//teamMember.updateUserRoleAndTeam(user, team);
teamMembersRepository.save(teamMember);
teamRepository.save(team);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.capstone.BnagFer.domain.myteam.service;

import com.capstone.BnagFer.domain.accounts.entity.User;
import com.capstone.BnagFer.domain.myteam.dto.response.CreateTeamTacticResponseDto;
import com.capstone.BnagFer.domain.myteam.entity.Team;
Expand All @@ -19,15 +20,17 @@ public class TeamTacticService {
private final TeamRepository teamRepository;
private final TacticRepository tacticRepository;

public CreateTeamTacticResponseDto addTactic(Long teamId, Long tacticId, User user) {
public CreateTeamTacticResponseDto changeTactic(Long teamId, Long tacticId, User user) {

// 전술 찾기
Tactic tactic = tacticRepository.findById(tacticId).orElseThrow(() -> new TacticExceptionHandler(ErrorCode.TACTIC_NOT_FOUND));
// 팀 찾기
Team team = teamRepository.findById(teamId).orElseThrow(() -> new TeamExceptionHandler(ErrorCode.TEAM_NOT_FOUND));

// 사용자 검증 및 전술 업데이트
validateAndUpdateTeam(team, user, tactic);
// 팀 멤버들의 포지션 초기화
resetTeamMemberPositions(team);

// 팀 저장
teamRepository.save(team);

Expand Down Expand Up @@ -65,4 +68,8 @@ private void validateAndUpdateTeam(Team team, User user, Tactic tactic) {
// 팀 정보 업데이트
team.updateLeaderAndTactic(user, tactic);
}

private void resetTeamMemberPositions(Team team) {
team.getTeamMembers().forEach(member -> member.updatePosition(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public enum ErrorCode implements BaseErrorCode {
//TEAM 관련 에러
TEAM_NOT_FOUND(HttpStatus.BAD_REQUEST, "TEAM401", "팀이 없습니다."),
NO_AUTHORIZATION(HttpStatus.BAD_REQUEST,"TEAM402", "권한이 없습니다."),
USER_TEAM_TACTIC_NOT_FOUND(HttpStatus.BAD_REQUEST, "TEAM403", "팀에 적용 가능한 전술이 없습니다."),

//TEAM_MEMBER 관련 에러
CANNOT_INVITE(HttpStatus.BAD_REQUEST, "TEAMMEMBER401", "팀원을 초대할 수 없습니다."),
Expand Down

0 comments on commit c8639d7

Please sign in to comment.