Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: FileManager Class 추가 #21

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mergedoc.backend.filemanage.dto;

import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateDIRForm {
@NotNull
private Long dirId;
@NotNull
private String name;

private Long memberId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mergedoc.backend.filemanage.dto;

import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreatePageForm {
@NotNull
private Long dirId;
@NotNull
private String name;

private Long memberId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mergedoc.backend.filemanage.dto;

import com.mergedoc.backend.dir.entity.FileType;
import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class FindFilesInDIRForm {
@NotNull
private Long dirId;

private FileType type;
private Long memberId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mergedoc.backend.filemanage.dto;

import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class FindPageForm {
@NotNull
private Long pageId;
@NotNull
private Long memberId;
}
160 changes: 160 additions & 0 deletions src/main/java/com/mergedoc/backend/filemanage/service/FileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.mergedoc.backend.filemanage.service;

import com.mergedoc.backend.dir.entity.DIR;
import com.mergedoc.backend.dir.entity.InDIR;
import com.mergedoc.backend.dir.entity.PageInDIR;
import com.mergedoc.backend.dir.repository.DIRRepository;
import com.mergedoc.backend.filemanage.dto.CreateDIRForm;
import com.mergedoc.backend.filemanage.dto.CreatePageForm;
import com.mergedoc.backend.filemanage.dto.FindFilesInDIRForm;
import com.mergedoc.backend.filemanage.dto.FindPageForm;
import com.mergedoc.backend.member.entity.Member;
import com.mergedoc.backend.member.repository.MemberRepository;
import com.mergedoc.backend.page.entity.Page;
import com.mergedoc.backend.page.repository.PageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class FileManager {

private final String ROOT_PATH = "/";
private final String ROOT_NAME = "root";

private final PageRepository pageRepository;
private final DIRRepository dirRepository;
private final MemberRepository memberRepository;

@Transactional
public Long createRootDIR(Long memberId) {
Member findMember = memberRepository.findById(memberId);

DIR dir = DIR.builder()
.path(ROOT_PATH)
.name(ROOT_NAME)
.member(findMember)
.build();

dirRepository.saveDIR(dir);
return dir.getId();
}

// dir 내부에 있는 file 을 찾는데 찾으려는 파일들의 생성 유저가 요청 유저 정보랑 다르면 예외
public List<DIR> findDIRsInDIR(FindFilesInDIRForm form) {
List<DIR> dirs = dirRepository.findDIRsInDIR(form.getDirId());

dirs.stream().findFirst().ifPresent(d -> validateDirOwner(form.getMemberId(), d));

return dirs;
}
public List<PageInDIR> findPagesInDIR(FindFilesInDIRForm form) {
List<PageInDIR> pages = dirRepository.findPagesInDIR(form.getDirId());

pages.stream().findFirst().ifPresent(p -> validatePageOwner(form.getMemberId(), p));

return pages;
}

/* Member 가입 시 생성되는 Root DIR*/

public List<InDIR> findAllFiles(FindFilesInDIRForm form) {
List<InDIR> result = new ArrayList<>();
result.addAll(findDIRsInDIR(form));
result.addAll(findPagesInDIR(form));

return result;
// 각 파일들에 대해서 type 을 포함해서 데이터로 돌려줄 수 있어야함
}

@Transactional
public Long createDIR(CreateDIRForm form) {
DIR parentDir = dirRepository.findDIRById(form.getDirId());

// 생성하려는 dir 의 소유자가 요청 유저와 일치해야한다
validateDirOwner(form.getMemberId(), parentDir);
validateDirDuplicated(form.getDirId(), form.getName());

// member id 만 저장하면 되는데 select 한 개가 더 나간다,, 이 부분은 개선이 필요한듯, JpaRepository getOne 혹은 직접 쿼리문 작성
Member member = memberRepository.findById(form.getMemberId());

DIR dir = DIR.builder()
.parent(parentDir)
.path(producePathFromParentDIR(parentDir.getPath(), parentDir.getName()))
.name(form.getName())
.member(member)
.build();

dirRepository.saveDIR(dir);

return dir.getId();
}

@Transactional
public Long createPage(CreatePageForm form) {
DIR dir = dirRepository.findDIRById(form.getDirId());

validateDirOwner(form.getMemberId(), dir);
validatePageDuplicated(dir.getId(), form.getName());

Member member = memberRepository.findById(form.getMemberId());

Page page = Page.builder()
.name(form.getName())
.build();
pageRepository.savePage(page);

PageInDIR pageInDIR = PageInDIR.builder()
.page(page)
.dir(dir)
.member(member)
.path(producePathFromParentDIR(dir.getPath(), dir.getName()))
.name(form.getName())
.build();
dirRepository.savePageInDIR(pageInDIR);

return pageInDIR.getId();
}

public Page findPage(FindPageForm form) {
PageInDIR findPage = dirRepository.findPageById(form.getPageId());

validatePageOwner(form.getMemberId(), findPage);

return pageRepository.findPageById(findPage.getPage().getId());
}



private String producePathFromParentDIR(String path, String name) {
return path + name + "/";
}

private void validateDirOwner(Long memberId, DIR dir) {
if(!dir.getMember().getId().equals(memberId)) {
throw new IllegalStateException("올바르지 않은 접근입니다.");
}
}

private void validatePageOwner(Long memberId, PageInDIR page) {
if(!page.getMember().getId().equals(memberId)) {
throw new IllegalStateException("올바르지 않은 접근입니다.");
}
}

private void validateDirDuplicated(Long dirId, String name) {
if (dirRepository.dirExistsInDirByName(dirId, name)) {
throw new IllegalStateException("중복된 디렉토리가 존재합니다.");
}
}
private void validatePageDuplicated(Long dirId, String name) {
if (dirRepository.pageExistsInDirByName(dirId, name)) {
throw new IllegalStateException("중복된 페이지가 존재합니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mergedoc.backend.share.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateSharedPageForm {
private Long pageId;
private Long memberId;
}
79 changes: 79 additions & 0 deletions src/test/java/com/mergedoc/backend/service/FileManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.mergedoc.backend.service;

import com.mergedoc.backend.dir.entity.DIR;
import com.mergedoc.backend.dir.entity.PageInDIR;
import com.mergedoc.backend.filemanage.dto.CreateDIRForm;
import com.mergedoc.backend.filemanage.dto.CreatePageForm;
import com.mergedoc.backend.filemanage.dto.FindFilesInDIRForm;
import com.mergedoc.backend.filemanage.dto.FindPageForm;
import com.mergedoc.backend.filemanage.service.FileManager;
import com.mergedoc.backend.member.entity.Member;
import com.mergedoc.backend.member.repository.MemberRepository;
import com.mergedoc.backend.page.entity.Page;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SpringBootTest
@Transactional
public class FileManagerTest {

@Autowired private FileManager fileManager;
@Autowired private MemberRepository memberRepository;


private final String ROOT_PATH = "/";
private final String ROOT_NAME = "root";

Member member;
Long rootId;

@BeforeEach
public void createRootDIR() {
member = Member.builder().build();
memberRepository.save(member);

rootId = fileManager.createRootDIR(member.getId());
}

@Test
public void createDIR() {
CreateDIRForm form = new CreateDIRForm(rootId, "sampleDIR", member.getId());
fileManager.createDIR(form);

FindFilesInDIRForm findForm = new FindFilesInDIRForm(rootId, null, member.getId());
List<DIR> findDIRs = fileManager.findDIRsInDIR(findForm);

for (DIR dir : findDIRs) {
System.out.println(dir.getName());
}
}

@Test
public void createPage() {
CreatePageForm form = new CreatePageForm(rootId, "samplePage", member.getId());
fileManager.createPage(form);
FindFilesInDIRForm findForm = new FindFilesInDIRForm(rootId, null, member.getId());
List<PageInDIR> findPages = fileManager.findPagesInDIR(findForm);

for (PageInDIR page : findPages) {
System.out.println(page.getName());
}
}

@Test
public void findPage() {
CreatePageForm form = new CreatePageForm(rootId, "samplePage", member.getId());
Long pageId = fileManager.createPage(form);

FindPageForm form2 = new FindPageForm(pageId, member.getId());
Page page = fileManager.findPage(form2);

System.out.println(page.getName());
}

}