Skip to content

Commit

Permalink
refactor: 비동기 작업을 수행하는 TalkPickFileHandler를 추가하여 파일 관련 로직을 TalkPickSe…
Browse files Browse the repository at this point in the history
…rvice에서 분리
  • Loading branch information
Hanjaemo committed Dec 18, 2024
1 parent 05bde1c commit d09b48d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 52 deletions.
11 changes: 0 additions & 11 deletions src/main/java/balancetalk/file/domain/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
Expand All @@ -27,15 +24,11 @@ public class FileHandler {
@Value("${picko.aws.s3.endpoint}")
private String s3EndPoint;

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void relocateFile(File file, Long resourceId, FileType fileType) {
String newDirectoryPath = relocateWithinS3(file, resourceId, fileType);
saveOrMapToResource(file, newDirectoryPath, resourceId, fileType);
}

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void relocateFiles(List<File> files, Long resourceId, FileType fileType) {
for (File file : files) {
String newDirectoryPath = relocateWithinS3(file, resourceId, fileType);
Expand Down Expand Up @@ -94,17 +87,13 @@ private String getImgUrl(File file, String directoryPath) {
return String.format("%s%s%s", s3EndPoint, directoryPath, file.getStoredName());
}

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void deleteFiles(List<File> files) {
for (File file : files) {
s3Operations.deleteObject(bucket, file.getS3Key());
}
fileRepository.deleteAll(files);
}

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void deleteFile(File file) {
s3Operations.deleteObject(bucket, file.getS3Key());
fileRepository.delete(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface FileRepository extends JpaRepository<File, Long>, FileRepositor
void deleteByResourceIdAndFileType(Long tempTalkPickId, FileType fileType);

void deleteByResourceIdInAndFileType(List<Long> ids, FileType fileType);

boolean existsByResourceIdAndFileType(Long talkPickId, FileType fileType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickResponse;
import static balancetalk.talkpick.dto.TalkPickDto.UpdateTalkPickRequest;

import balancetalk.file.domain.File;
import balancetalk.file.domain.FileHandler;
import balancetalk.file.domain.repository.FileRepository;
import balancetalk.global.exception.BalanceTalkException;
import balancetalk.member.domain.Member;
import balancetalk.member.domain.MemberRepository;
import balancetalk.member.dto.ApiMember;
import balancetalk.member.dto.GuestOrApiMember;
import balancetalk.talkpick.domain.TalkPick;
import balancetalk.talkpick.domain.TalkPickFileHandler;
import balancetalk.talkpick.domain.repository.TalkPickRepository;
import balancetalk.vote.domain.TalkPickVote;
import java.util.List;
Expand All @@ -33,24 +32,19 @@ public class TalkPickService {
private final MemberRepository memberRepository;
private final TalkPickRepository talkPickRepository;
private final FileRepository fileRepository;
private final FileHandler fileHandler;
private final TalkPickFileHandler talkPickFileHandler;

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

private void relocateFiles(List<Long> fileIds, Long talkPickId) {
List<File> files = fileRepository.findAllById(fileIds);
fileHandler.relocateFiles(files, talkPickId, TALK_PICK);
}

@Transactional
public TalkPickDetailResponse findById(Long talkPickId, GuestOrApiMember guestOrApiMember) {
TalkPick talkPick = talkPickRepository.findById(talkPickId)
Expand Down Expand Up @@ -88,44 +82,15 @@ public void updateTalkPick(Long talkPickId, UpdateTalkPickRequest request, ApiMe
Member member = apiMember.toMember(memberRepository);
TalkPick talkPick = member.getTalkPickById(talkPickId);
talkPick.update(request.toEntity(member));

if (request.notContainsAnyFileIds()) {
return;
}

List<Long> deletedFileIds = deleteRequestedFiles(request);

if (request.containsNewFileIds()) {
List<Long> newFileIds = request.getNewFileIds();
newFileIds.removeIf((deletedFileIds::contains));
relocateFiles(newFileIds, talkPickId);
}
}

private List<Long> deleteRequestedFiles(UpdateTalkPickRequest request) {
if (request.containsDeleteFileIds()) {
List<Long> deleteFileIds = request.getDeleteFileIds();
List<File> files = fileRepository.findAllById(deleteFileIds);
fileHandler.deleteFiles(files);
return deleteFileIds;
} else {
return List.of();
}
talkPickFileHandler
.handleFilesOnTalkPickUpdate(request.getNewFileIds(), request.getDeleteFileIds(), talkPickId);
}

@Transactional
public void deleteTalkPick(Long talkPickId, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
TalkPick talkPick = member.getTalkPickById(talkPickId);
talkPickRepository.delete(talkPick);
deleteAllAssociatedFiles(talkPickId);
}

private void deleteAllAssociatedFiles(Long talkPickId) {
List<File> files = fileRepository.findAllByResourceIdAndFileType(talkPickId, TALK_PICK);
if (files.isEmpty()) {
return;
}
fileHandler.deleteFiles(files);
talkPickFileHandler.handleFilesOnTalkPickDelete(talkPickId);
}
}
64 changes: 64 additions & 0 deletions src/main/java/balancetalk/talkpick/domain/TalkPickFileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package balancetalk.talkpick.domain;

import static balancetalk.file.domain.FileType.TALK_PICK;

import balancetalk.file.domain.File;
import balancetalk.file.domain.FileHandler;
import balancetalk.file.domain.repository.FileRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
@Transactional
public class TalkPickFileHandler {

private final FileRepository fileRepository;
private final FileHandler fileHandler;

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void handleFilesOnTalkPickCreate(List<Long> fileIds, Long talkPickId) {
relocateFiles(fileIds, talkPickId);
}

private void relocateFiles(List<Long> fileIds, Long talkPickId) {
List<File> files = fileRepository.findAllById(fileIds);
fileHandler.relocateFiles(files, talkPickId, TALK_PICK);
}

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void handleFilesOnTalkPickUpdate(List<Long> newFileIds, List<Long> deleteFileIds, Long talkPickId) {
deleteFiles(deleteFileIds);
newFileIds.removeIf((deleteFileIds::contains));
relocateFiles(newFileIds, talkPickId);
}

private void deleteFiles(List<Long> deleteFileIds) {
if (deleteFileIds.isEmpty()) {
return;
}
List<File> files = fileRepository.findAllById(deleteFileIds);
fileHandler.deleteFiles(files);
}

@Async
@Retryable(backoff = @Backoff(delay = 1000))
public void handleFilesOnTalkPickDelete(Long talkPickId) {
if (notExistsFilesBy(talkPickId)) {
return;
}
List<File> files = fileRepository.findAllByResourceIdAndFileType(talkPickId, TALK_PICK);
fileHandler.deleteFiles(files);
}

private boolean notExistsFilesBy(Long talkPickId) {
return !fileRepository.existsByResourceIdAndFileType(talkPickId, TALK_PICK);
}
}

0 comments on commit d09b48d

Please sign in to comment.