-
Notifications
You must be signed in to change notification settings - Fork 5
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 #79 from Train0303/feature/workspace
워크스페이스 API 구현
- Loading branch information
Showing
10 changed files
with
231 additions
and
3 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
56 changes: 56 additions & 0 deletions
56
linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.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,56 @@ | ||
package com.kakao.linknamu.workspace.controller; | ||
|
||
import com.kakao.linknamu._core.security.CustomUserDetails; | ||
import com.kakao.linknamu._core.util.ApiUtils; | ||
import com.kakao.linknamu.workspace.dto.WorkspaceCreateRequestDto; | ||
import com.kakao.linknamu.workspace.dto.WorkspaceUpdateRequestDto; | ||
import com.kakao.linknamu.workspace.service.WorkspaceDeleteService; | ||
import com.kakao.linknamu.workspace.service.WorkspaceReadService; | ||
import com.kakao.linknamu.workspace.service.WorkspaceSaveService; | ||
import com.kakao.linknamu.workspace.service.WorkspaceUpdateService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/workspace") | ||
public class WorkspaceController { | ||
private final WorkspaceReadService workspaceReadService; | ||
private final WorkspaceSaveService workspaceSaveService; | ||
private final WorkspaceUpdateService workspaceUpdateService; | ||
private final WorkspaceDeleteService workspaceDeleteService; | ||
|
||
@GetMapping("/list") | ||
public ResponseEntity<?> getWorkspaceList(@AuthenticationPrincipal CustomUserDetails userDetails) { | ||
// 워크스페이스 리스트 조회 서비스 코드 | ||
return ResponseEntity.ok(ApiUtils.success(workspaceReadService.getWorkspaceList(userDetails.getUser()))); | ||
} | ||
|
||
@PostMapping("/create") | ||
public ResponseEntity<?> createWorkspace(@RequestBody @Valid WorkspaceCreateRequestDto requestDto, | ||
@AuthenticationPrincipal CustomUserDetails userDetails) { | ||
// 워크스페이스 생성 서비스 코드 | ||
workspaceSaveService.createWorkspace(requestDto.workspaceName(), userDetails.getUser()); | ||
return ResponseEntity.ok(ApiUtils.success(null)); | ||
} | ||
|
||
@PostMapping("/update/{workspace_id}") | ||
public ResponseEntity<?> updateWorkspace(@PathVariable("workspace_id") Long workspaceId, | ||
@RequestBody @Valid WorkspaceUpdateRequestDto requestDto, | ||
@AuthenticationPrincipal CustomUserDetails userDetails) { | ||
// 워크스페이스 수정 서비스 코드 | ||
workspaceUpdateService.updateWorkspace(workspaceId, requestDto, userDetails.getUser()); | ||
return ResponseEntity.ok(ApiUtils.success(null)); | ||
} | ||
|
||
@PostMapping("/delete/{workspace_id}") | ||
public ResponseEntity<?> deleteWorkspace(@PathVariable("workspace_id") Long workspaceId, | ||
@AuthenticationPrincipal CustomUserDetails userDetails) { | ||
// 워크스페이스 삭제 서비스 코드 | ||
workspaceDeleteService.deleteWorkspace(workspaceId, userDetails.getUser()); | ||
return ResponseEntity.ok(ApiUtils.success(null)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceCreateRequestDto.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.kakao.linknamu.workspace.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record WorkspaceCreateRequestDto( | ||
@NotBlank String workspaceName | ||
) { | ||
} |
38 changes: 38 additions & 0 deletions
38
linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.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,38 @@ | ||
package com.kakao.linknamu.workspace.dto; | ||
|
||
import com.kakao.linknamu.category.entity.Category; | ||
import com.kakao.linknamu.workspace.entity.Workspace; | ||
import lombok.Builder; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public record WorkspaceGetResponseDto( | ||
Long workspaceId, | ||
String workspaceName, | ||
List<CategoryResponseDto> categoryList | ||
) { | ||
record CategoryResponseDto( | ||
Long categoryId, | ||
String categoryName | ||
){} | ||
|
||
|
||
@Builder | ||
public WorkspaceGetResponseDto { | ||
} | ||
|
||
|
||
public static WorkspaceGetResponseDto of(Workspace workspace) { | ||
return WorkspaceGetResponseDto.builder() | ||
.workspaceId(workspace.getId()) | ||
.workspaceName(workspace.getWorkspaceName()) | ||
.categoryList(workspace.getCategorySet().stream() | ||
.sorted(Comparator.comparing(Category::getCategoryId)) | ||
.map((c) -> new CategoryResponseDto(c.getCategoryId(), c.getCategoryName())) | ||
.toList()) | ||
.build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceUpdateRequestDto.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.kakao.linknamu.workspace.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record WorkspaceUpdateRequestDto( | ||
@NotBlank String workspaceName | ||
) { | ||
} |
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
31 changes: 31 additions & 0 deletions
31
linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceDeleteService.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,31 @@ | ||
package com.kakao.linknamu.workspace.service; | ||
|
||
import com.kakao.linknamu._core.exception.Exception403; | ||
import com.kakao.linknamu._core.exception.Exception404; | ||
import com.kakao.linknamu.user.entity.User; | ||
import com.kakao.linknamu.workspace.WorkspaceExceptionStatus; | ||
import com.kakao.linknamu.workspace.entity.Workspace; | ||
import com.kakao.linknamu.workspace.repository.WorkspaceJPARepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class WorkspaceDeleteService { | ||
private final WorkspaceJPARepository workspaceJPARepository; | ||
|
||
public void deleteWorkspace(Long workspaceId, User user) { | ||
Workspace workspace = workspaceJPARepository.findById(workspaceId).orElseThrow( | ||
() -> new Exception404(WorkspaceExceptionStatus.WORKSPACE_NOT_FOUND)); | ||
|
||
validationCheck(workspace.getUser().getUserId(), user.getUserId()); | ||
|
||
workspaceJPARepository.delete(workspace); | ||
} | ||
|
||
private void validationCheck(Long writerId, Long requesterId) { | ||
if (!writerId.equals(requesterId)) throw new Exception403(WorkspaceExceptionStatus.WORKSPACE_FORBIDDEN); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.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,34 @@ | ||
package com.kakao.linknamu.workspace.service; | ||
|
||
import com.kakao.linknamu.category.entity.Category; | ||
import com.kakao.linknamu.category.service.CategoryService; | ||
import com.kakao.linknamu.user.entity.User; | ||
import com.kakao.linknamu.workspace.dto.WorkspaceGetResponseDto; | ||
import com.kakao.linknamu.workspace.entity.Workspace; | ||
import com.kakao.linknamu.workspace.repository.WorkspaceJPARepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.stream.Collectors.groupingBy; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class WorkspaceReadService { | ||
private final WorkspaceJPARepository workspaceJPARepository; | ||
|
||
public List<WorkspaceGetResponseDto> getWorkspaceList(User user) { | ||
List<Workspace> workspaceList = workspaceJPARepository.findAllByUserIdFetchJoinCategory(user.getUserId()); | ||
if (workspaceList.isEmpty()) return List.of(); | ||
|
||
return workspaceList.stream() | ||
.map(WorkspaceGetResponseDto::of) | ||
.toList(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.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,35 @@ | ||
package com.kakao.linknamu.workspace.service; | ||
|
||
import com.kakao.linknamu._core.exception.Exception403; | ||
import com.kakao.linknamu._core.exception.Exception404; | ||
import com.kakao.linknamu.user.entity.User; | ||
import com.kakao.linknamu.workspace.WorkspaceExceptionStatus; | ||
import com.kakao.linknamu.workspace.dto.WorkspaceUpdateRequestDto; | ||
import com.kakao.linknamu.workspace.entity.Workspace; | ||
import com.kakao.linknamu.workspace.repository.WorkspaceJPARepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class WorkspaceUpdateService { | ||
private final WorkspaceJPARepository workspaceJPARepository; | ||
|
||
public void updateWorkspace(Long workspaceId, WorkspaceUpdateRequestDto requestDto, User user) { | ||
Workspace workspace = workspaceJPARepository.findById(workspaceId).orElseThrow( | ||
() -> new Exception404(WorkspaceExceptionStatus.WORKSPACE_NOT_FOUND)); | ||
|
||
validationCheck(workspace.getUser().getUserId(), user.getUserId()); | ||
|
||
// 만약 수정하고자하는 이름이 같다면 DB에 Update할 이유가 없다. | ||
if (requestDto.workspaceName().equals(workspace.getWorkspaceName())) return; | ||
|
||
workspace.renameWorkspace(requestDto.workspaceName()); | ||
} | ||
|
||
private void validationCheck(Long writerId, Long requesterId) { | ||
if (!writerId.equals(requesterId)) throw new Exception403(WorkspaceExceptionStatus.WORKSPACE_FORBIDDEN); | ||
} | ||
} |