Skip to content

Commit

Permalink
Merge pull request #154 from Team-Tiki/init-#151-folder
Browse files Browse the repository at this point in the history
[INIT] 폴더 도메인 추가 및 파일 도메인 수정
  • Loading branch information
Chan531 authored Oct 28, 2024
2 parents f960b45 + a983940 commit cc463af
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/tiki/server/document/entity/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class Document extends BaseTime {

private String fileUrl;

private Long folderId;

private double capacity;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "block_id")
private TimeBlock timeBlock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.tiki.server.document.service;

import static com.tiki.server.document.message.ErrorCode.INVALID_DOCUMENT;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,7 +10,6 @@
import com.tiki.server.document.adapter.DocumentFinder;
import com.tiki.server.document.dto.response.DocumentsGetResponse;
import com.tiki.server.document.entity.Document;
import com.tiki.server.document.exception.DocumentException;
import com.tiki.server.memberteammanager.adapter.MemberTeamManagerFinder;
import com.tiki.server.memberteammanager.entity.MemberTeamManager;

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/tiki/server/folder/adapter/FolderFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.tiki.server.folder.adapter;

import static com.tiki.server.folder.message.ErrorCode.INVALID_FOLDER;

import com.tiki.server.common.support.RepositoryAdapter;
import com.tiki.server.folder.entity.Folder;
import com.tiki.server.folder.exception.FolderException;
import com.tiki.server.folder.repository.FolderRepository;

import lombok.RequiredArgsConstructor;

@RepositoryAdapter
@RequiredArgsConstructor
public class FolderFinder {

private final FolderRepository folderRepository;

public Folder findById(long id) {
return folderRepository.findById(id)
.orElseThrow(() -> new FolderException(INVALID_FOLDER));
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/tiki/server/folder/adapter/FolderSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tiki.server.folder.adapter;

import com.tiki.server.common.support.RepositoryAdapter;
import com.tiki.server.folder.entity.Folder;
import com.tiki.server.folder.repository.FolderRepository;

import lombok.RequiredArgsConstructor;

@RepositoryAdapter
@RequiredArgsConstructor
public class FolderSaver {

private final FolderRepository folderRepository;

public Folder save(Folder folder) {
return folderRepository.save(folder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.tiki.server.folder.controller;

import static com.tiki.server.common.dto.SuccessResponse.*;
import static com.tiki.server.folder.message.SuccessMessage.SUCCESS_CREATE_FOLDER;

import java.security.Principal;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tiki.server.common.dto.SuccessResponse;
import com.tiki.server.common.support.UriGenerator;
import com.tiki.server.folder.dto.request.FolderCreateRequest;
import com.tiki.server.folder.dto.response.FolderCreateResponse;
import com.tiki.server.folder.service.FolderService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/v2/folders")
public class FolderController {

private final FolderService folderService;

@PostMapping()
public ResponseEntity<SuccessResponse<FolderCreateResponse>> createFolder(
Principal principal,
@RequestHeader("team-id") long teamId,
@RequestBody FolderCreateRequest request
) {
long memberId = Long.parseLong(principal.getName());
FolderCreateResponse response = folderService.create(memberId, teamId, request);
return ResponseEntity.created(UriGenerator.getUri("api/v2/folders/" + response.folderId()))
.body(success(SUCCESS_CREATE_FOLDER.getMessage(), response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tiki.server.folder.dto.request;

import lombok.NonNull;

public record FolderCreateRequest(
@NonNull String name,
Long parentId
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tiki.server.folder.dto.response;

import static lombok.AccessLevel.PRIVATE;

import lombok.Builder;

@Builder(access = PRIVATE)
public record FolderCreateResponse(
long folderId
) {

public static FolderCreateResponse from(long folderId) {
return FolderCreateResponse.builder()
.folderId(folderId)
.build();
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/tiki/server/folder/entity/Folder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tiki.server.folder.entity;

import static jakarta.persistence.GenerationType.IDENTITY;

import com.tiki.server.common.entity.BaseTime;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
public class Folder extends BaseTime {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String name;

private String path;

private long teamId;

public Folder(String name, Folder parentFolder, long teamId) {
this.name = name;
this.path = generatePath(parentFolder);
this.teamId = teamId;
}

private String generatePath(Folder parentFolder) {
if (parentFolder == null) {
return "";
}
return parentFolder.getPath() + "/" + parentFolder.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tiki.server.folder.exception;

import com.tiki.server.folder.message.ErrorCode;

import lombok.Getter;

@Getter
public class FolderException extends RuntimeException {

private final ErrorCode errorCode;

public FolderException(ErrorCode errorCode) {
super("[FolderException] : " + errorCode.getMessage());
this.errorCode = errorCode;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/tiki/server/folder/message/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tiki.server.folder.message;

import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ErrorCode {

/* 404 NOT_FOUND : 자원을 찾을 수 없음 */
INVALID_FOLDER(NOT_FOUND, "유효하지 않은 폴더입니다.");

private final HttpStatus httpStatus;
private final String message;
}
13 changes: 13 additions & 0 deletions src/main/java/com/tiki/server/folder/message/SuccessMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tiki.server.folder.message;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum SuccessMessage {

SUCCESS_CREATE_FOLDER("폴더 생성 성공");

private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tiki.server.folder.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.tiki.server.folder.entity.Folder;

public interface FolderRepository extends JpaRepository<Folder, Long> {
}
39 changes: 39 additions & 0 deletions src/main/java/com/tiki/server/folder/service/FolderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tiki.server.folder.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiki.server.folder.adapter.FolderFinder;
import com.tiki.server.folder.adapter.FolderSaver;
import com.tiki.server.folder.dto.request.FolderCreateRequest;
import com.tiki.server.folder.dto.response.FolderCreateResponse;
import com.tiki.server.folder.entity.Folder;
import com.tiki.server.memberteammanager.adapter.MemberTeamManagerFinder;

import lombok.RequiredArgsConstructor;

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

private final FolderFinder folderFinder;
private final FolderSaver folderSaver;
private final MemberTeamManagerFinder memberTeamManagerFinder;

@Transactional
public FolderCreateResponse create(long memberId, long teamId, FolderCreateRequest request) {
// 같은 레벨 파일명 중복 방지 로직 추가 필요
memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Folder parentFolder = getFolder(request.parentId());
Folder folder = folderSaver.save(new Folder(request.name(), parentFolder, teamId));
return FolderCreateResponse.from(folder.getId());
}

private Folder getFolder(Long folderId) {
if (folderId == null) {
return null;
}
return folderFinder.findById(folderId);
}
}

0 comments on commit cc463af

Please sign in to comment.