-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from Team-Tiki/init-#151-folder
[INIT] 폴더 도메인 추가 및 파일 도메인 수정
- Loading branch information
Showing
13 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/tiki/server/folder/adapter/FolderFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/com/tiki/server/folder/adapter/FolderSaver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/tiki/server/folder/controller/FolderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/tiki/server/folder/dto/request/FolderCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/tiki/server/folder/dto/response/FolderCreateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/tiki/server/folder/exception/FolderException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/com/tiki/server/folder/message/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/com/tiki/server/folder/message/SuccessMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/tiki/server/folder/repository/FolderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/main/java/com/tiki/server/folder/service/FolderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |