-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 비동기 작업을 수행하는 TalkPickFileHandler를 추가하여 파일 관련 로직을 TalkPickSe…
…rvice에서 분리
- Loading branch information
Showing
4 changed files
with
72 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/balancetalk/talkpick/domain/TalkPickFileHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |