Skip to content

Commit

Permalink
refactor: CreateOrUpdateTalkPickRequest를 생성 요청 DTO와 수정 요청 DTO로 분리 (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanjaemo authored Nov 14, 2024
1 parent 8d761ad commit 643331f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
23 changes: 13 additions & 10 deletions src/main/java/balancetalk/talkpick/application/TalkPickService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import static balancetalk.file.domain.FileType.TALK_PICK;
import static balancetalk.global.exception.ErrorCode.NOT_FOUND_TALK_PICK;
import static balancetalk.talkpick.dto.TalkPickDto.CreateOrUpdateTalkPickRequest;
import static balancetalk.talkpick.dto.TalkPickDto.CreateTalkPickRequest;
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickDetailResponse;
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickResponse;
import static balancetalk.talkpick.dto.TalkPickDto.UpdateTalkPickRequest;

import balancetalk.file.domain.File;
import balancetalk.file.domain.FileHandler;
Expand Down Expand Up @@ -35,19 +36,19 @@ public class TalkPickService {
private final FileHandler fileHandler;

@Transactional
public Long createTalkPick(CreateOrUpdateTalkPickRequest request, ApiMember apiMember) {
public Long createTalkPick(CreateTalkPickRequest request, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
TalkPick savedTalkPick = talkPickRepository.save(request.toEntity(member));
Long savedTalkPickId = savedTalkPick.getId();
relocateFilesIfContainsFileIds(request, savedTalkPickId);
if (request.containsFileIds()) {
relocateFiles(request.getFileIds(), savedTalkPickId);
}
return savedTalkPickId;
}

private void relocateFilesIfContainsFileIds(CreateOrUpdateTalkPickRequest request, Long talkPickId) {
if (request.containsFileIds()) {
List<File> files = fileRepository.findAllById(request.getFileIds());
fileHandler.relocateFiles(files, talkPickId, TALK_PICK);
}
private void relocateFiles(List<Long> fileIds, Long talkPickId) {
List<File> files = fileRepository.findAllById(fileIds);
fileHandler.relocateFiles(files, talkPickId, TALK_PICK);
}

@Transactional
Expand Down Expand Up @@ -83,11 +84,13 @@ public List<TalkPickResponse> findBestTalkPicks() {
}

@Transactional
public void updateTalkPick(Long talkPickId, CreateOrUpdateTalkPickRequest request, ApiMember apiMember) {
public void updateTalkPick(Long talkPickId, UpdateTalkPickRequest request, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
TalkPick talkPick = member.getTalkPickById(talkPickId);
talkPick.update(request.toEntity(member));
relocateFilesIfContainsFileIds(request, talkPick.getId());
if (request.containsFileIds()) {
relocateFiles(request.getFileIds(), talkPickId);
}
}

@Transactional
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/balancetalk/talkpick/dto/TalkPickDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

public class TalkPickDto {

@Schema(description = "톡픽 생성/수정 요청")
@Schema(description = "톡픽 생성 요청")
@Data
@AllArgsConstructor
public static class CreateOrUpdateTalkPickRequest {
public static class CreateTalkPickRequest {

private BaseTalkPickFields baseFields;

Expand All @@ -53,6 +53,33 @@ public boolean containsFileIds() {
}
}

@Schema(description = "톡픽 수정 요청")
@Data
@AllArgsConstructor
public static class UpdateTalkPickRequest {

private BaseTalkPickFields baseFields;

@Schema(description = "첨부한 이미지 파일 ID 목록", example = "[12, 41]")
private List<Long> fileIds;

public TalkPick toEntity(Member member) {
return TalkPick.builder()
.member(member)
.title(baseFields.getTitle())
.content(baseFields.getContent())
.optionA(baseFields.getOptionA())
.optionB(baseFields.getOptionB())
.sourceUrl(baseFields.getSourceUrl())
.editedAt(LocalDateTime.now())
.build();
}

public boolean containsFileIds() {
return fileIds != null;
}
}

@Schema(description = "톡픽 상세 조회 응답")
@Data
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package balancetalk.talkpick.presentation;

import static balancetalk.talkpick.dto.TalkPickDto.CreateTalkPickRequest;
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickDetailResponse;
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickResponse;
import static balancetalk.talkpick.dto.TalkPickDto.UpdateTalkPickRequest;
import static org.springframework.data.domain.Sort.Direction.DESC;

import balancetalk.global.utils.AuthPrincipal;
import balancetalk.member.dto.ApiMember;
import balancetalk.member.dto.GuestOrApiMember;
Expand All @@ -8,6 +14,7 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -21,11 +28,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static balancetalk.talkpick.dto.TalkPickDto.*;
import static org.springframework.data.domain.Sort.Direction.DESC;

@RestController
@RequiredArgsConstructor
@RequestMapping("/talks")
Expand All @@ -36,7 +38,7 @@ public class TalkPickController {

@Operation(summary = "톡픽 생성", description = "톡픽을 생성합니다.")
@PostMapping
public Long createTalkPick(@RequestBody @Valid final CreateOrUpdateTalkPickRequest request,
public Long createTalkPick(@RequestBody @Valid final CreateTalkPickRequest request,
@Parameter(hidden = true) @AuthPrincipal final ApiMember apiMember) {
return talkPickService.createTalkPick(request, apiMember);
}
Expand All @@ -59,7 +61,7 @@ public Page<TalkPickResponse> findPagedTalkPicks(
@Operation(summary = "톡픽 수정", description = "톡픽을 수정합니다.")
@PutMapping("/{talkPickId}")
public void updateTalkPick(@PathVariable final Long talkPickId,
@RequestBody @Valid final CreateOrUpdateTalkPickRequest request,
@RequestBody @Valid final UpdateTalkPickRequest request,
@Parameter(hidden = true) @AuthPrincipal final ApiMember apiMember) {
talkPickService.updateTalkPick(talkPickId, request, apiMember);
}
Expand Down

0 comments on commit 643331f

Please sign in to comment.