Skip to content

Commit

Permalink
♻️ refactor: 전술 복사 시, TacticPositionDetail 관한 부분 깊은 복사 처리 (#202) (#203)
Browse files Browse the repository at this point in the history
[REFACTOR] 전술 복사 API 요청 시, response 내용 수정
  • Loading branch information
jiiiiiw authored Sep 14, 2024
1 parent b78a467 commit fa4aa56
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public ApiResponse<TacticResponse> updateTactic(@PathVariable(name = "tacticId")

@Operation(summary = "전술 복사후 가져오기", description = "다른 사람의 전술을 복사해서 작성자를 자신으로 하여 저장.")
@PostMapping("/{tacticId}")
public ApiResponse<TacticResponse> copyTactic(@PathVariable(name = "tacticId") Long tacticId, @LoginUser User user) {
TacticResponse tacticDetail = tacticService.copyTactic(tacticId, user);
public ApiResponse<CopyTacticResponse> copyTactic(@PathVariable(name = "tacticId") Long tacticId, @LoginUser User user) {
CopyTacticResponse tacticDetail = tacticService.copyTactic(tacticId, user);
return ApiResponse.onSuccess(tacticDetail);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<TacticDetailResponse.DetailList> 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<TacticDetailResponse.DetailList> from(List<TacticPositionDetail> positions){
return positions.stream().map(TacticDetailResponse.DetailList::from).toList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ public void updateTactic(TacticUpdateRequest request){
public static Tactic createTactic() {
return new Tactic();
}

public void setTacticPositionDetails(List<TacticPositionDetail> tacticPositionDetails) {
this.tacticPositionDetails = tacticPositionDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -90,7 +91,8 @@ private void createPositionDetails(List<DetailCreateRequest> 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));

Expand All @@ -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){
Expand Down

0 comments on commit fa4aa56

Please sign in to comment.