Skip to content

Commit

Permalink
Merge pull request #187 from Team-Tiki/feat/#186-folder
Browse files Browse the repository at this point in the history
[FEAT] 폴더 이름 수정
  • Loading branch information
Chan531 authored Dec 2, 2024
2 parents eae4759 + b753cf2 commit 6161585
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static com.tiki.server.common.dto.SuccessResponse.*;
import static com.tiki.server.folder.message.SuccessMessage.SUCCESS_CREATE_FOLDER;
import static com.tiki.server.folder.message.SuccessMessage.SUCCESS_GET_FOLDERS;
import static com.tiki.server.folder.message.SuccessMessage.SUCCESS_UPDATE_FOLDER_NAME;

import java.security.Principal;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -20,6 +22,7 @@
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.request.FolderNameUpdateRequest;
import com.tiki.server.folder.dto.response.FolderCreateResponse;
import com.tiki.server.folder.dto.response.FoldersGetResponse;
import com.tiki.server.folder.service.FolderService;
Expand All @@ -46,7 +49,7 @@ public ResponseEntity<SuccessResponse<FoldersGetResponse>> getFolders(

@PostMapping("/teams/{teamId}/folders")
public ResponseEntity<SuccessResponse<FolderCreateResponse>> createFolder(
Principal principal,
final Principal principal,
@PathVariable final long teamId,
@RequestParam(required = false) final Long folderId,
@RequestBody final FolderCreateRequest request
Expand All @@ -58,6 +61,18 @@ public ResponseEntity<SuccessResponse<FolderCreateResponse>> createFolder(
.body(success(SUCCESS_CREATE_FOLDER.getMessage(), response));
}

@PatchMapping("/teams/{teamId}/folders/{folderId}")
public ResponseEntity<SuccessResponse<?>> updateFolderName(
final Principal principal,
@PathVariable final long teamId,
@PathVariable final long folderId,
@RequestBody final FolderNameUpdateRequest request
) {
long memberId = Long.parseLong(principal.getName());
folderService.updateFolderName(memberId, teamId, folderId, request);
return ResponseEntity.ok(success(SUCCESS_UPDATE_FOLDER_NAME.getMessage()));
}

@DeleteMapping("/teams/{teamId}/folders")
public ResponseEntity<?> delete(
final Principal principal,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tiki.server.folder.dto.request;

import lombok.NonNull;

public record FolderNameUpdateRequest(
@NonNull String name
) {
}
4 changes: 4 additions & 0 deletions src/main/java/com/tiki/server/folder/entity/Folder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public String getChildPath() {
return path + SEPARATOR + id;
}

public void updateName(final String name) {
this.name = name;
}

private String generatePath(final Folder parentFolder) {
if (parentFolder == null) {
return ROOT_PATH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public enum SuccessMessage {

SUCCESS_GET_FOLDERS("폴더 목록 조회 성공"),
SUCCESS_CREATE_FOLDER("폴더 생성 성공");
SUCCESS_CREATE_FOLDER("폴더 생성 성공"),
SUCCESS_UPDATE_FOLDER_NAME("폴더 이름 수정 성공");

private final String message;
}
17 changes: 14 additions & 3 deletions src/main/java/com/tiki/server/folder/service/FolderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.request.FolderNameUpdateRequest;
import com.tiki.server.folder.dto.response.FolderCreateResponse;
import com.tiki.server.folder.dto.response.FoldersGetResponse;
import com.tiki.server.folder.entity.Folder;
Expand Down Expand Up @@ -52,11 +53,21 @@ public FolderCreateResponse create(final long memberId, final long teamId,
memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Folder parentFolder = getFolder(teamId, folderId);
String path = getChildFolderPath(parentFolder);
validateFolderName(teamId, path, request);
validateFolderName(teamId, path, request.name());
Folder folder = folderSaver.save(new Folder(request.name(), parentFolder, teamId));
return FolderCreateResponse.from(folder.getId());
}

@Transactional
public void updateFolderName(final long memberId, final long teamId,
final long folderId, final FolderNameUpdateRequest request) {
memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Folder folder = folderFinder.findById(folderId);
folder.validateTeamId(teamId);
validateFolderName(teamId, folder.getPath(), request.name());
folder.updateName(request.name());
}

@Transactional
public void delete(final long memberId, final long teamId, final List<Long> folderIds) {
memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Expand All @@ -80,9 +91,9 @@ private String getChildFolderPath(final Folder folder) {
return folder.getChildPath();
}

private void validateFolderName(final long teamId, final String path, final FolderCreateRequest request) {
private void validateFolderName(final long teamId, final String path, final String name) {
List<Folder> folders = folderFinder.findByTeamIdAndPath(teamId, path);
if (folders.stream().anyMatch(folder -> folder.getName().equals(request.name()))) {
if (folders.stream().anyMatch(folder -> folder.getName().equals(name))) {
throw new FolderException(FOLDER_NAME_DUPLICATE);
}
}
Expand Down

0 comments on commit 6161585

Please sign in to comment.