Skip to content

Commit

Permalink
refactor : 파일/폴더 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
kochungcheon committed Feb 6, 2024
1 parent 07a7225 commit de1b059
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,24 @@ public class StorageFacadeService {
private final FolderService folderService;
private final FileService fileService;

public CursorMetaRes getFolderContents(Long parentId, Long cursorId, Long userId, Integer size,
boolean cursorFlag) {
public CursorMetaRes getFolderContents(Long parentId, Long cursorId, Long userId, Integer size, boolean cursorFlag) {
Integer contentsSize = PagingUtil.calculateSize(size);

return cursorFlag
? handleFolderFirstStrategy(parentId, cursorId, userId, contentsSize)
: handleFileFirstStrategy(parentId, cursorId, userId, contentsSize);
return cursorFlag ? handleFolderFirstStrategy(parentId, cursorId, userId, contentsSize) : handleFileFirstStrategy(parentId, cursorId, userId, contentsSize);
}

private CursorMetaRes handleFolderFirstStrategy(Long parentId, Long cursorId, Long userId,
Integer contentsSize) {
private CursorMetaRes handleFolderFirstStrategy(Long parentId, Long cursorId, Long userId, Integer contentsSize) {
Pageable page = PagingUtil.createPageable(contentsSize);
CursorFolderResponse cursorFolderResponse = folderService.getFolders(parentId, cursorId, userId, page);

if (Boolean.FALSE.equals(cursorFolderResponse.folderHasNext())) {
Pageable remainPage = PagingUtil.createPageable(
contentsSize - cursorFolderResponse.folderMetadata().size());
Pageable remainPage = PagingUtil.createPageable(contentsSize - cursorFolderResponse.folderMetadata().size());
CursorFileResponse cursorFileResponse = fileService.getFiles(parentId, null, userId, remainPage);
return CursorMetaRes.of(cursorFolderResponse, cursorFileResponse);
}
return CursorMetaRes.of(cursorFolderResponse, new CursorFileResponse(null, false));
}

private CursorMetaRes handleFileFirstStrategy(Long parentId, Long cursorId, Long userId,
Integer contentsSize) {
private CursorMetaRes handleFileFirstStrategy(Long parentId, Long cursorId, Long userId, Integer contentsSize) {
Pageable page = PagingUtil.createPageable(contentsSize);
CursorFileResponse cursorFileResponse = fileService.getFiles(parentId, cursorId, userId, page);
return CursorMetaRes.of(CursorFolderResponse.of(null, false), cursorFileResponse);
Expand All @@ -61,20 +54,17 @@ public void deleteFolderContents(long folderId, long userId) {
deleteFolderContents(folderMetadata);
}

// softDelete
// hardDelete
private void deleteFolderContents(FolderMetadata folderMetadata) {
// 삭제 할 폴더리스트 조회
List<FolderMetadata> folderMetadataList = folderService.findAllBy(folderMetadata.getId());
// 삭제 할 파일리스트 조회
List<FileMetadata> fileMetadataList;
fileMetadataList = fileService.findAllBy(folderMetadata.getId());

// 파일 일괄 삭제 softDelete
fileService.deleteAll(fileMetadataList);
// 재귀적으로 폴더 삭제
folderMetadataList.forEach(this::deleteFolderContents);


folderService.deleteFolder(folderMetadata);
}
}
20 changes: 5 additions & 15 deletions src/test/java/com/c4cometrue/mystorage/TestConstants.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.c4cometrue.mystorage;

import java.nio.file.Path;
import java.nio.file.Paths;

import com.c4cometrue.mystorage.file.FileMetadata;
import com.c4cometrue.mystorage.folder.FolderMetadata;
import com.c4cometrue.mystorage.folder.dto.FolderNameChangeRequest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import com.c4cometrue.mystorage.common.MetadataType;
import com.c4cometrue.mystorage.deletedmetadata.DeletedMetadata;
import com.c4cometrue.mystorage.folder.dto.FolderNameChangeRequest;
import com.c4cometrue.mystorage.file.FileMetadata;
import com.c4cometrue.mystorage.folder.FolderMetadata;
import java.nio.file.Path;
import java.nio.file.Paths;

public class TestConstants {
public static final Long USER_ID = 1L;
Expand Down Expand Up @@ -48,11 +45,4 @@ public class TestConstants {
public static final Long FOLDER_ID = 1L;

public static final FolderNameChangeRequest FOLDER_NAME_CHANGE_REQUEST = FolderNameChangeRequest.of(USER_ID, FOLDER_ID, USER_FOLDER_NAME);

public static final DeletedMetadata DELETE_METADATA = DeletedMetadata.builder()
.type(MetadataType.FILE)
.filePath(PARENT_PATH)
.parentId(PARENT_ID)
.userId(USER_ID)
.build();
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package com.c4cometrue.mystorage.storage;

import static com.c4cometrue.mystorage.TestConstants.*;
import static org.mockito.BDDMockito.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.c4cometrue.mystorage.deletedmetadata.DeleteMetadataService;
import com.c4cometrue.mystorage.file.FileMetadata;
import com.c4cometrue.mystorage.file.FileService;
import com.c4cometrue.mystorage.file.dto.CursorFileResponse;
import com.c4cometrue.mystorage.file.dto.FileContent;
import com.c4cometrue.mystorage.folder.FolderMetadata;
import com.c4cometrue.mystorage.folder.FolderService;
import com.c4cometrue.mystorage.folder.dto.CursorFolderResponse;
import com.c4cometrue.mystorage.folder.dto.FolderContent;
import com.c4cometrue.mystorage.util.PagingUtil;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static com.c4cometrue.mystorage.TestConstants.*;
import static org.mockito.BDDMockito.*;

@ExtendWith(MockitoExtension.class)
@DisplayName("스토리지 파사드 서비스 테스트")
Expand All @@ -34,8 +28,7 @@ class StorageFacadeServiceTest {
private FolderService folderService;
@Mock
private FileService fileService;
@Mock
private DeleteMetadataService deleteMetadataService;

@Test
@DisplayName("폴더조회테스트 flag가 true 일때")
void getFolderContentsTrueTest() {
Expand Down

0 comments on commit de1b059

Please sign in to comment.