Skip to content

Commit

Permalink
feat: PR 기능 수
Browse files Browse the repository at this point in the history
  • Loading branch information
kimday0326 committed May 31, 2024
1 parent e8f0fbf commit 0351250
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public FileGetResponse uploadFile(FileCreateRequest request, MultipartFile multi
return FileGetResponse.of(fileRepository.save(savedFile));
}

// 새로운 버전 파일 업로드 (+ PR 생성)
// 새로운 버전 파일 업로드 & PR 생성
public FileGetResponse uploadNewVersionFile(
Long parentFileId,
PullRequestCreateRequest request,
Expand Down Expand Up @@ -119,6 +119,9 @@ public FileGetResponse uploadNewVersionFile(
.build();
pullRequestRepository.save(pullRequest);

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

// 워크스페이스 용량 업데이트
final Workspace workspace = workspaceService.findWorkspaceById(parentFile.getWorkspaceId());
workspace.increaseUsedStorage(savedFile.getSize());
Expand Down Expand Up @@ -166,7 +169,7 @@ public void deleteFileTree(Long fileId) {
});
}

private File findFileById(Long fileId) {
public File findFileById(Long fileId) {
return fileRepository.findById(fileId)
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "파일을 찾을 수 없습니다."));
}
Expand Down
252 changes: 142 additions & 110 deletions src/main/java/com/khu/gitbox/domain/file/entity/File.java
Original file line number Diff line number Diff line change
@@ -1,125 +1,157 @@
package com.khu.gitbox.domain.file.entity;

import org.springframework.http.HttpStatus;

import com.khu.gitbox.common.BaseEntity;
import com.khu.gitbox.common.exception.CustomException;
import jakarta.persistence.*;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "file")
public class File extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "size", nullable = false)
private Long size;

@Column(name = "url", nullable = false)
private String url;

@Enumerated(EnumType.STRING)
@Column(name = "tag")
private FileTag tag;

@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private FileType type;

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
private FileStatus status;

@Column(name = "version", nullable = false)
private Long version;

@Column(name = "is_latest", nullable = false)
private boolean isLatest;

@Column(name = "is_deleted", nullable = false)
private boolean isDeleted;

@Column(name = "writer_id", nullable = false)
private Long writerId;

@Column(name = "workspace_id", nullable = false)
private Long workspaceId;

@Column(name = "folder_id", nullable = false)
private Long folderId;

@Column(name = "parent_file_id")
private Long parentFileId;

@Column(name = "root_file_id")
private Long rootFileId;

@Builder
File(
String name,
Long size,
String url,
FileType type,
FileStatus status,
Long version,
boolean isLatest,
Long writerId,
Long workspaceId,
Long folderId,
Long parentFileId,
Long rootFileId
) {
this.name = name;
this.size = size;
this.url = url;
this.type = type;
this.status = status;
this.version = version;
this.isLatest = isLatest;
this.writerId = writerId;
this.workspaceId = workspaceId;
this.folderId = folderId;
this.parentFileId = parentFileId;
this.rootFileId = rootFileId;
}

public void updateRootFileId(Long id) {
this.rootFileId = id;
}

public void updateFile(String name, FileTag tag) {
this.name = name;
this.tag = tag;
}

public void updateStatus(FileStatus status) {
this.status = status;
}

public void updateTag(FileTag tag) {
this.tag = tag;
}

public void delete() {
if (this.isDeleted) {
throw new CustomException(HttpStatus.BAD_REQUEST, "이미 삭제된 파일입니다.");
}
this.isDeleted = true;
}

public void restore() {
if (!this.isDeleted) {
throw new CustomException(HttpStatus.BAD_REQUEST, "이미 복원된 파일입니다.");
}
this.isDeleted = false;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "size", nullable = false)
private Long size;

@Column(name = "url", nullable = false)
private String url;

@Enumerated(EnumType.STRING)
@Column(name = "tag")
private FileTag tag;

@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private FileType type;

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
private FileStatus status;

@Column(name = "version", nullable = false)
private Long version;

@Column(name = "is_latest", nullable = false)
private boolean isLatest;

@Column(name = "is_deleted", nullable = false)
private boolean isDeleted;

@Column(name = "writer_id", nullable = false)
private Long writerId;

@Column(name = "workspace_id", nullable = false)
private Long workspaceId;

@Column(name = "folder_id", nullable = false)
private Long folderId;

@Column(name = "parent_file_id")
private Long parentFileId;

@Column(name = "root_file_id")
private Long rootFileId;

@Column(name = "pull_request_id")
private Long pullRequestId;

@Builder
File(
String name,
Long size,
String url,
FileType type,
FileStatus status,
Long version,
boolean isLatest,
Long writerId,
Long workspaceId,
Long folderId,
Long parentFileId,
Long rootFileId
) {
this.name = name;
this.size = size;
this.url = url;
this.type = type;
this.status = status;
this.version = version;
this.isLatest = isLatest;
this.writerId = writerId;
this.workspaceId = workspaceId;
this.folderId = folderId;
this.parentFileId = parentFileId;
this.rootFileId = rootFileId;
}

public void updateRootFileId(Long id) {
this.rootFileId = id;
}

public void updateFile(String name, FileTag tag) {
this.name = name;
this.tag = tag;
}

public void approve(File parentFile) {
if (this.status != FileStatus.PENDING) {
throw new CustomException(HttpStatus.BAD_REQUEST, "승인할 수 없는 파일입니다.");
}
parentFile.isLatest = false;
this.isLatest = true;
this.status = FileStatus.APPROVED;
}

public void reject(File parentFile) {
if (this.status != FileStatus.PENDING) {
throw new CustomException(HttpStatus.BAD_REQUEST, "반려할 수 없는 파일입니다.");
}
parentFile.initPullRequestId();
this.status = FileStatus.REJECTED;
}

public void initPullRequestId() {
this.pullRequestId = null;
}

public void updatePullRequestId(Long pullRequestId) {
if (this.pullRequestId != null) {
throw new CustomException(HttpStatus.BAD_REQUEST, "이미 PR이 존재하는 파일입니다.");
}
this.pullRequestId = pullRequestId;
}

public void delete() {
if (this.isDeleted) {
throw new CustomException(HttpStatus.BAD_REQUEST, "이미 삭제된 파일입니다.");
}
this.isDeleted = true;
}

public void restore() {
if (!this.isDeleted) {
throw new CustomException(HttpStatus.BAD_REQUEST, "이미 복원된 파일입니다.");
}
this.isDeleted = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public record FileGetResponse(
Long folderId,
Long parentFileId,
Long rootFileId,
Long pullRequestId,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
Expand All @@ -41,6 +42,7 @@ public static FileGetResponse of(File file) {
file.getFolderId(),
file.getParentFileId(),
file.getRootFileId(),
file.getPullRequestId(),
file.getCreatedAt(),
file.getUpdatedAt()
);
Expand Down
Loading

0 comments on commit 0351250

Please sign in to comment.