Skip to content

Commit

Permalink
refactor : 충돌 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
singsangssong committed Jun 6, 2024
2 parents a3088ef + dbcf426 commit 5340dd7
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
import com.khu.gitbox.domain.file.entity.FileType;
import com.khu.gitbox.domain.file.entity.Folder;
import com.khu.gitbox.domain.file.infrastructure.FileRepository;
import com.khu.gitbox.domain.file.presentation.dto.FileGetRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.FileCreateRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.FileUpdateRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.PullRequestCreateRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.*;
import com.khu.gitbox.domain.file.presentation.dto.response.FileGetResponse;
import com.khu.gitbox.domain.member.application.MemberService;
import com.khu.gitbox.domain.member.entity.Member;
import com.khu.gitbox.domain.pullRequest.entity.PullRequest;
import com.khu.gitbox.domain.pullRequest.infrastructure.PullRequestRepository;
import com.khu.gitbox.domain.workspace.application.WorkspaceService;
import com.khu.gitbox.domain.workspace.entity.Workspace;
import com.khu.gitbox.domain.workspace.entity.WorkspaceMember;
import com.khu.gitbox.domain.workspace.infrastructure.WorkspaceMemberRepository;
import com.khu.gitbox.s3.S3Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -36,13 +35,15 @@
@Transactional
@RequiredArgsConstructor
public class FileService {

private final FileRepository fileRepository;
private final PullRequestRepository pullRequestRepository;
private final MemberService memberService;
private final WorkspaceService workspaceService;
private final FolderService folderService;
private final ActionHistoryService actionHistoryService;
private final S3Service s3Service;
private final WorkspaceMemberRepository workspaceMemberRepository;

// 파일 업로드
public FileGetResponse uploadFile(FileCreateRequest request, MultipartFile multipartFile) {
Expand Down Expand Up @@ -76,6 +77,7 @@ public FileGetResponse uploadFile(FileCreateRequest request, MultipartFile multi
return FileGetResponse.of(fileRepository.save(savedFile));
}


// 새로운 버전 파일 업로드 & PR 생성
public FileGetResponse uploadNewVersionFile(
Long parentFileId,
Expand All @@ -84,7 +86,8 @@ public FileGetResponse uploadNewVersionFile(
// 부모 파일 정보 가져오기
final Member member = memberService.findMemberById(getCurrentMemberId());
final File parentFile = findFileById(parentFileId);

final List<WorkspaceMember> workspaceMembers = workspaceMemberRepository.findByWorkspaceId(
parentFile.getWorkspaceId());
// 부모 파일 업데이트 가능 여부 확인
validateParentFileForUpdate(parentFileId, parentFile);

Expand All @@ -108,6 +111,23 @@ public FileGetResponse uploadNewVersionFile(
.build();
final File savedFile = fileRepository.save(newVersionFile);

if (workspaceMembers.size() > 1) {
// PR 생성 (PR 승인 시 부모 파일을 구버전으로)
createPullRequest(request, member, savedFile);
} else {
// PR 승인
savedFile.approve(parentFile);
}

// 워크스페이스 용량 업데이트
final Workspace workspace = workspaceService.findWorkspaceById(parentFile.getWorkspaceId());
workspace.increaseUsedStorage(savedFile.getSize());

actionHistoryService.createActionHistory(workspace.getId(), member, savedFile, Action.PULL_REQUEST);
return FileGetResponse.of(savedFile);
}

private void createPullRequest(PullRequestCreateRequest request, Member member, File savedFile) {
// PR 생성
final PullRequest pullRequest = PullRequest.builder()
.title(request.pullRequestTitle())
Expand All @@ -119,13 +139,6 @@ public FileGetResponse uploadNewVersionFile(

// 파일 PR ID 설정
savedFile.updatePullRequestId(pullRequest.getId());

// 워크스페이스 용량 업데이트
final Workspace workspace = workspaceService.findWorkspaceById(parentFile.getWorkspaceId());
workspace.increaseUsedStorage(savedFile.getSize());

actionHistoryService.createActionHistory(workspace.getId(), member, savedFile, Action.PULL_REQUEST);
return FileGetResponse.of(savedFile);
}

// 파일 조회
Expand Down Expand Up @@ -198,10 +211,17 @@ private void validateParentFileForUpdate(Long parentFileId, File parentFile) {
});
}

public List<FileGetResponse> getFilesByTag(FileGetRequest request) {
public List<FileGetResponse> getFilesByTag(FileGetByTagRequest request) {
return fileRepository.findAllByTag(request.workspaceId(), request.tag())
.stream()
.map(FileGetResponse::of)
.toList();
}

public List<FileGetResponse> getFilesByKeyword(FileGetByKeywordRequest request) {
return fileRepository.findAllByKeyword(request.workspaceId(), request.keyword())
.stream()
.map(FileGetResponse::of)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.khu.gitbox.domain.file.entity.File;
import com.khu.gitbox.domain.file.entity.FileTag;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand Down Expand Up @@ -29,14 +30,17 @@ public interface FileRepository extends JpaRepository<File, Long> {
@Query("SELECT f FROM File f WHERE f.folderId = :folderId AND f.name = :name AND f.isDeleted = FALSE")
Optional<File> findByFolderIdAndName(Long folderId, String name);


//휴지통에 버린 팡리 목록 조회
//휴지통에 버린 파일 목록 조회
@Query("SELECT f FROM File f WHERE f.workspaceId = :workspaceId AND f.isDeleted = TRUE ORDER BY f.name ASC")
List<File> findAllByWorkspaceIdTrash(Long workspaceId);

@Query("SELECT f FROM File f WHERE f.workspaceId = :workspaceId AND f.name LIKE %:keyword% AND f.isDeleted = FALSE ORDER BY f.name ASC")
List<File> findAllByKeyword(Long workspaceId, @Param("keyword") String keyword);

@Query("DELETE FROM File f WHERE f.rootFileId = :id AND f.isDeleted = TRUE")
void deleteByRootFileId(Long rootFileId);

@Query("SELECT f FROM File f WHERE f.rootFileId = :rootFileId AND f.isDeleted = TRUE ORDER BY f.version DESC")
List<File> findAllByRootFileIdTrash(Long rootFileId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import com.khu.gitbox.common.response.ApiResponse;
import com.khu.gitbox.domain.file.application.FileService;
import com.khu.gitbox.domain.file.presentation.dto.FileGetRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.FileCreateRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.FileGetByKeywordRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.FileGetByTagRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.FileUpdateRequest;
import com.khu.gitbox.domain.file.presentation.dto.request.PullRequestCreateRequest;
import com.khu.gitbox.domain.file.presentation.dto.response.FileGetResponse;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -33,6 +35,7 @@
public class FileController {
private final FileService fileService;

@Operation(summary = "새 파일 업로드")
@PostMapping(value = "", consumes = "multipart/form-data")
public ApiResponse<FileGetResponse> uploadFile(
@RequestPart(value = "request") FileCreateRequest request,
Expand All @@ -41,13 +44,22 @@ public ApiResponse<FileGetResponse> uploadFile(
return ApiResponse.ok(response);
}

@GetMapping(value = "")
public ApiResponse<List<FileGetResponse>> getFileByTag(@Valid @ModelAttribute FileGetRequest request) {
log.info("{}", request);
@Operation(summary = "키워드로 파일 목록 조회")
@GetMapping(value = "/keyword")
public ApiResponse<List<FileGetResponse>> getFilesByKeyword(
@Valid @ModelAttribute FileGetByKeywordRequest request) {
final List<FileGetResponse> response = fileService.getFilesByKeyword(request);
return ApiResponse.ok(response);
}

@Operation(summary = "태그로 파일 목록 조회")
@GetMapping(value = "/tag")
public ApiResponse<List<FileGetResponse>> getFilesByTag(@Valid @ModelAttribute FileGetByTagRequest request) {
final List<FileGetResponse> response = fileService.getFilesByTag(request);
return ApiResponse.ok(response);
}

@Operation(summary = "새 버전 파일 업로드 (풀 리퀘스트 생성)")
@PostMapping(value = "/{parentFileId}", consumes = "multipart/form-data")
public ApiResponse<FileGetResponse> uploadNewVersionFile(
@PathVariable Long parentFileId,
Expand All @@ -57,18 +69,21 @@ public ApiResponse<FileGetResponse> uploadNewVersionFile(
return ApiResponse.ok(response);
}

@Operation(summary = "파일 정보 조회")
@GetMapping(value = "/{fileId}")
public ApiResponse<FileGetResponse> getFile(@PathVariable Long fileId) {
final FileGetResponse response = fileService.getFileInfo(fileId);
return ApiResponse.ok(response);
}

@Operation(summary = "파일 트리 조회")
@GetMapping(value = "/{fileId}/tree")
public ApiResponse<List<FileGetResponse>> getFileTree(@PathVariable Long fileId) {
final List<FileGetResponse> fileTree = fileService.getFileTree(fileId);
return ApiResponse.ok(fileTree);
}

@Operation(summary = "파일 정보 수정")
@PatchMapping(value = "/{fileId}")
public ApiResponse<Void> getFileTree(
@PathVariable Long fileId,
Expand All @@ -77,13 +92,15 @@ public ApiResponse<Void> getFileTree(
return ApiResponse.ok();
}

@Operation(summary = "파일 삭제")
@DeleteMapping(value = "/{fileId}")
public ApiResponse<Void> deleteFile(@PathVariable Long fileId) {
fileService.deleteFile(fileId);
return ApiResponse.ok();
}

@DeleteMapping(value = "/files/{fileId}/tree")
@Operation(summary = "파일 트리 삭제")
@DeleteMapping(value = "/{fileId}/tree")
public ApiResponse<Void> deleteFileTree(@PathVariable Long fileId) {
fileService.deleteFileTree(fileId);
return ApiResponse.ok();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.khu.gitbox.domain.file.presentation.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record FileGetByKeywordRequest(
@NotNull(message = "workspaceId는 필수입니다.")
Long workspaceId,

@NotBlank(message = "keyword는 필수입니다.")
String keyword
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.khu.gitbox.domain.file.presentation.dto.request;

import com.khu.gitbox.domain.file.entity.FileTag;

import jakarta.validation.constraints.NotNull;

public record FileGetByTagRequest(
@NotNull(message = "workspaceId는 필수입니다.")
Long workspaceId,
FileTag tag
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class TrashController {
private final TrashService trashService;

@GetMapping("")
@Operation(summary = "휴지통 파일 목록 조회")
public ApiResponse<List<TrashFileResponse>> showTrashFiles(
@PathVariable("workspaceId") Long workspaceId) {
List<TrashFileResponse> fileList = trashService.showTrashFiles(workspaceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ public Long createWorkspace(CreateWorkspace request, Long ownerId) {
.build();
Long workspaceId = workspaceRepository.save(workspace).getId();

// 멤버 이메일 리스트를 순회하며 멤버 엔티티 생성 및 추가
saveWorkspaceMembers(workspaceId, request.getMemberEmails());

// 워크스페이스 소유자 추가
WorkspaceMember workspaceMember = WorkspaceMember.builder()
.memberId(ownerId)
.workspaceId(workspaceId)
.build();
workspaceMemberRepository.save(workspaceMember);

// 멤버 이메일 리스트를 순회하며 멤버 엔티티 생성 및 추가
saveWorkspaceMembers(workspaceId, request.getMemberEmails());

Folder rootFolder = Folder.builder()
.name(DEFAULT_ROOT_FOLDER_NAME)
.workspaceId(workspaceId)
Expand Down Expand Up @@ -160,23 +161,21 @@ private List<Long> saveWorkspaceMembers(Long workspaceId, List<String> memberEma
for (String email : memberEmails) {
// 이메일로 멤버 찾기
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "이메일을 잘못 입력하셨습니다."));
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "존재하지 않는 멤버입니다. : " + email));

// 이미 워크스페이스 멤버인지 확인
if (workspaceMemberRepository.findByWorkspaceIdAndMemberId(workspaceId, member.getId()).isPresent()) {
throw new CustomException(HttpStatus.BAD_REQUEST, "이미 워크스페이스 멤버입니다.");
if (workspaceMemberRepository.findByWorkspaceIdAndMemberId(workspaceId, member.getId()).isEmpty()) {
// WorkspaceMember 객체 생성
WorkspaceMember workspaceMember = WorkspaceMember.builder()
.memberId(member.getId())
.workspaceId(workspaceId)
.build();
// WorkspaceMember 저장
workspaceMemberRepository.save(workspaceMember);

// 멤버 ID 리스트에 추가
returnMemberIds.add(member.getId());
}

// WorkspaceMember 객체 생성
WorkspaceMember workspaceMember = WorkspaceMember.builder()
.memberId(member.getId())
.workspaceId(workspaceId)
.build();
// WorkspaceMember 저장
workspaceMemberRepository.save(workspaceMember);

// 멤버 ID 리스트에 추가
returnMemberIds.add(member.getId());
}
return returnMemberIds;
}
Expand Down
Loading

0 comments on commit 5340dd7

Please sign in to comment.