From fa4aa565f1e0f207683f2786313ec105527c8ef1 Mon Sep 17 00:00:00 2001 From: jiiiiiw <98935125+jiiiiiw@users.noreply.github.com> Date: Sat, 14 Sep 2024 22:59:25 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20=EC=A0=84?= =?UTF-8?q?=EC=88=A0=20=EB=B3=B5=EC=82=AC=20=EC=8B=9C,=20TacticPositionDet?= =?UTF-8?q?ail=20=EA=B4=80=ED=95=9C=20=EB=B6=80=EB=B6=84=20=EA=B9=8A?= =?UTF-8?q?=EC=9D=80=20=EB=B3=B5=EC=82=AC=20=EC=B2=98=EB=A6=AC=20(#202)=20?= =?UTF-8?q?(#203)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [REFACTOR] 전술 복사 API 요청 시, response 내용 수정 --- .../tactic/controller/TacticController.java | 4 +- .../domain/tactic/dto/CopyTacticResponse.java | 59 +++++++++++++++++++ .../BnagFer/domain/tactic/entity/Tactic.java | 4 ++ .../tactic/entity/TacticPositionDetail.java | 6 ++ .../domain/tactic/service/TacticService.java | 15 ++++- 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/capstone/BnagFer/domain/tactic/dto/CopyTacticResponse.java diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java b/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java index 90b09641..c9ea09fb 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java @@ -92,8 +92,8 @@ public ApiResponse updateTactic(@PathVariable(name = "tacticId") @Operation(summary = "전술 복사후 가져오기", description = "다른 사람의 전술을 복사해서 작성자를 자신으로 하여 저장.") @PostMapping("/{tacticId}") - public ApiResponse copyTactic(@PathVariable(name = "tacticId") Long tacticId, @LoginUser User user) { - TacticResponse tacticDetail = tacticService.copyTactic(tacticId, user); + public ApiResponse copyTactic(@PathVariable(name = "tacticId") Long tacticId, @LoginUser User user) { + CopyTacticResponse tacticDetail = tacticService.copyTactic(tacticId, user); return ApiResponse.onSuccess(tacticDetail); } diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/dto/CopyTacticResponse.java b/src/main/java/com/capstone/BnagFer/domain/tactic/dto/CopyTacticResponse.java new file mode 100644 index 00000000..76d8a26a --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/dto/CopyTacticResponse.java @@ -0,0 +1,59 @@ +package com.capstone.BnagFer.domain.tactic.dto; + +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 lombok.Builder; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; + +@Builder +public record CopyTacticResponse( + Long tacticId, + Long userId, + String nickname, + String tacticName, + Boolean anonymous, + String mainFormation, + List positionDetail, + String tacticDetails, + String subTactic, + LocalDateTime createdAt, + LocalDateTime updatedAt) { + public static CopyTacticResponse from(Tactic tactic) { + + return CopyTacticResponse.builder() + .tacticId(tactic.getTacticId()) + .userId(tactic.getUser().getId()) + .nickname(tactic.getUser().getProfile().getNickname()) + .tacticName(tactic.getTacticName()) + .anonymous(tactic.isAnonymous()) + .mainFormation(tactic.getMainFormation()) + .positionDetail(CopyTacticResponse.DetailList.from(tactic.getTacticPositionDetails())) + .tacticDetails(tactic.getTacticDetails()) + .subTactic(tactic.getSubTactic()) + .createdAt(tactic.getCreatedAt()) + .updatedAt(tactic.getUpdatedAt()) + .build(); + } + + @Builder + public record DetailList( + Long detailId, + Position position, + String positionDescription + ){ + public static TacticDetailResponse.DetailList from(TacticPositionDetail tacticPositionDetail){ + return TacticDetailResponse.DetailList.builder() + .detailId(tacticPositionDetail.getDetailId()) + .position(tacticPositionDetail.getPosition()) + .positionDescription(tacticPositionDetail.getPositionDescription()) + .build(); + } + public static List from(List positions){ + return positions.stream().map(TacticDetailResponse.DetailList::from).toList(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/entity/Tactic.java b/src/main/java/com/capstone/BnagFer/domain/tactic/entity/Tactic.java index acdd514e..6bb2fe6d 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/entity/Tactic.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/entity/Tactic.java @@ -85,4 +85,8 @@ public void updateTactic(TacticUpdateRequest request){ public static Tactic createTactic() { return new Tactic(); } + + public void setTacticPositionDetails(List tacticPositionDetails) { + this.tacticPositionDetails = tacticPositionDetails; + } } diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/entity/TacticPositionDetail.java b/src/main/java/com/capstone/BnagFer/domain/tactic/entity/TacticPositionDetail.java index a40a9d3a..d00a7197 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/entity/TacticPositionDetail.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/entity/TacticPositionDetail.java @@ -32,4 +32,10 @@ public void updateDetail(DetailUpdateRequest request){ position = request.position(); positionDescription = request.positionDescription(); } + + public void setDetail(Tactic tactic, TacticPositionDetail request){ + this.tactic = tactic; + position = request.getPosition(); + positionDescription = request.getPositionDescription(); + } } diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java index 43e66350..48138422 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticService.java @@ -20,6 +20,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -90,7 +91,8 @@ private void createPositionDetails(List positionDetails, Ta } } - public TacticResponse copyTactic(Long tacticId, User user) { + @Transactional + public CopyTacticResponse copyTactic(Long tacticId, User user) { Tactic tactic = tacticRepository.findById(tacticId).orElseThrow(() -> new TacticExceptionHandler(ErrorCode.TACTIC_NOT_FOUND)); @@ -100,11 +102,20 @@ public TacticResponse copyTactic(Long tacticId, User user) { Tactic copyTactic = Tactic.createTactic(); copyTactic.setCopyDetail(user, tactic); + // 깊은 복사 수행 + copyTactic.setTacticPositionDetails(new ArrayList<>()); + for (TacticPositionDetail detail : tactic.getTacticPositionDetails()) { + TacticPositionDetail copyDetail = new TacticPositionDetail(); + // TacticPositionDetail의 모든 필드를 복사 + copyDetail.setDetail(copyTactic, detail); + copyTactic.getTacticPositionDetails().add(copyDetail); + } + // 프로필 존재 확인 accountsCommonService.checkUserProfile(user); tacticRepository.save(copyTactic); - return TacticResponse.from(copyTactic); + return CopyTacticResponse.from(copyTactic); } public CommentResponse createComment(Long tacticId, CommentCreateRequest request, User user, Long parentCommentId){