From 4f429fb519dd562089b0054424d934cb13e89304 Mon Sep 17 00:00:00 2001 From: Taeho Date: Wed, 4 Oct 2023 15:54:23 +0900 Subject: [PATCH 1/8] =?UTF-8?q?#69=20Feat:=20=EC=9B=8C=ED=81=AC=EC=8A=A4?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=83=9D=EC=84=B1=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WorkspaceController.java | 48 +++++++++++++++++++ .../dto/WorkspaceCreateRequestDto.java | 8 ++++ 2 files changed, 56 insertions(+) create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceCreateRequestDto.java diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java new file mode 100644 index 00000000..629a80af --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java @@ -0,0 +1,48 @@ +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.WorkspaceSaveService; +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 WorkspaceSaveService workspaceSaveService; + + @GetMapping("") + public ResponseEntity getWorkspaceList(@AuthenticationPrincipal CustomUserDetails userDetails) { + // 워크스페이스 리스트 조회 서비스 코드 + return ResponseEntity.ok(ApiUtils.success(null)); + } + + @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) { + // 워크스페이스 수정 서비스 코드 + return ResponseEntity.ok(ApiUtils.success(null)); + } + + @PostMapping("/delete/{workspace_id}") + public ResponseEntity deleteWorkspace(@PathVariable("workspace_id") Long workspaceId, + @AuthenticationPrincipal CustomUserDetails userDetails) { + // 워크스페이스 삭제 서비스 코드 + return ResponseEntity.ok(ApiUtils.success(null)); + } +} diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceCreateRequestDto.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceCreateRequestDto.java new file mode 100644 index 00000000..e9618049 --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceCreateRequestDto.java @@ -0,0 +1,8 @@ +package com.kakao.linknamu.workspace.dto; + +import jakarta.validation.constraints.NotBlank; + +public record WorkspaceCreateRequestDto( + @NotBlank String workspaceName +) { +} From 82648f301d68ed92dad504de2b2b74b7cd9d310e Mon Sep 17 00:00:00 2001 From: Taeho Date: Wed, 4 Oct 2023 19:22:09 +0900 Subject: [PATCH 2/8] =?UTF-8?q?#69=20Feat:=20=EC=9B=8C=ED=81=AC=EC=8A=A4?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=88=98=EC=A0=95=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workspace/WorkspaceExceptionStatus.java | 1 + .../controller/WorkspaceController.java | 3 ++ .../dto/WorkspaceUpdateRequestDto.java | 8 ++++++ .../linknamu/workspace/entity/Workspace.java | 6 ++++ .../service/WorkspaceUpdateService.java | 28 +++++++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceUpdateRequestDto.java create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java index a570ed2c..54ed766c 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java @@ -7,6 +7,7 @@ @RequiredArgsConstructor @Getter public enum WorkspaceExceptionStatus implements BaseExceptionStatus { + WORKSPACE_NOT_FOUND("존재하지 않는 워크스페이스 입니다.", 404), WORKSPACE_DUPLICATED("같은 이름의 워크스페이스가 존재합니다.", 400); private final String message; diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java index 629a80af..d2eb872d 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java @@ -5,6 +5,7 @@ import com.kakao.linknamu.workspace.dto.WorkspaceCreateRequestDto; import com.kakao.linknamu.workspace.dto.WorkspaceUpdateRequestDto; 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; @@ -16,6 +17,7 @@ @RequestMapping("/api/workspace") public class WorkspaceController { private final WorkspaceSaveService workspaceSaveService; + private final WorkspaceUpdateService workspaceUpdateService; @GetMapping("") public ResponseEntity getWorkspaceList(@AuthenticationPrincipal CustomUserDetails userDetails) { @@ -36,6 +38,7 @@ public ResponseEntity updateWorkspace(@PathVariable("workspace_id") Long work @RequestBody @Valid WorkspaceUpdateRequestDto requestDto, @AuthenticationPrincipal CustomUserDetails userDetails) { // 워크스페이스 수정 서비스 코드 + workspaceUpdateService.updateWorkspace(workspaceId, requestDto, userDetails.getUser()); return ResponseEntity.ok(ApiUtils.success(null)); } diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceUpdateRequestDto.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceUpdateRequestDto.java new file mode 100644 index 00000000..dcfaefc1 --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceUpdateRequestDto.java @@ -0,0 +1,8 @@ +package com.kakao.linknamu.workspace.dto; + +import jakarta.validation.constraints.NotBlank; + +public record WorkspaceUpdateRequestDto( + @NotBlank String workspaceName +) { +} diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java index 3a428544..1b54222f 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java @@ -3,6 +3,7 @@ import com.kakao.linknamu.user.entity.User; import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; @@ -12,6 +13,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity @ToString(exclude = {"user"}) +@DynamicUpdate @Table( name = "workspace_tb", uniqueConstraints = { @@ -44,6 +46,10 @@ public Workspace(Long id, User user, String workspaceName) { this.workspaceName = workspaceName; } + public void renameWorkspace(String workspaceName) { + this.workspaceName = workspaceName; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java new file mode 100644 index 00000000..09c6bf96 --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java @@ -0,0 +1,28 @@ +package com.kakao.linknamu.workspace.service; + +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 WorkspaceJPARepository workspaceJPARepository; + + public void updateWorkspace(Long workspaceId, WorkspaceUpdateRequestDto requestDto, User user) { + Workspace workspace = workspaceJPARepository.findById(workspaceId).orElseThrow( + () -> new Exception404(WorkspaceExceptionStatus.WORKSPACE_NOT_FOUND)); + + // 만약 수정하고자하는 이름이 같다면 DB에 Update할 이유가 없다. + if (requestDto.workspaceName().equals(workspace.getWorkspaceName())) return; + + workspace.renameWorkspace(requestDto.workspaceName()); + } +} From c63d2229c40ef8c55b29924d84427aec8de0bbc1 Mon Sep 17 00:00:00 2001 From: Taeho Date: Wed, 4 Oct 2023 21:00:25 +0900 Subject: [PATCH 3/8] =?UTF-8?q?#70=20Fix:=20=EC=9A=94=EC=B2=AD=EC=9E=90=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=20=EB=B0=8F=20=EC=9D=98=EC=A1=B4=EC=84=B1=20?= =?UTF-8?q?=EC=A3=BC=EC=9E=85=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../linknamu/workspace/WorkspaceExceptionStatus.java | 1 + .../workspace/service/WorkspaceUpdateService.java | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java index 54ed766c..1c491c8a 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/WorkspaceExceptionStatus.java @@ -8,6 +8,7 @@ @Getter public enum WorkspaceExceptionStatus implements BaseExceptionStatus { WORKSPACE_NOT_FOUND("존재하지 않는 워크스페이스 입니다.", 404), + WORKSPACE_FORBIDDEN("접근 권한이 없는 사용자 입니다.", 403), WORKSPACE_DUPLICATED("같은 이름의 워크스페이스가 존재합니다.", 400); private final String message; diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java index 09c6bf96..cab162fc 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceUpdateService.java @@ -1,5 +1,6 @@ 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; @@ -14,15 +15,21 @@ @RequiredArgsConstructor @Transactional public class WorkspaceUpdateService { - private WorkspaceJPARepository workspaceJPARepository; + 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); + } } From e30edcf893acd702284e5c2af693fd4f8ff420a0 Mon Sep 17 00:00:00 2001 From: Taeho Date: Wed, 4 Oct 2023 21:04:24 +0900 Subject: [PATCH 4/8] =?UTF-8?q?#71=20Feat:=20=EC=9B=8C=ED=81=AC=EC=8A=A4?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=82=AD=EC=A0=9C=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WorkspaceController.java | 3 ++ .../service/WorkspaceDeleteService.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceDeleteService.java diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java index d2eb872d..2ba657e9 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java @@ -4,6 +4,7 @@ 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.WorkspaceSaveService; import com.kakao.linknamu.workspace.service.WorkspaceUpdateService; import jakarta.validation.Valid; @@ -18,6 +19,7 @@ public class WorkspaceController { private final WorkspaceSaveService workspaceSaveService; private final WorkspaceUpdateService workspaceUpdateService; + private final WorkspaceDeleteService workspaceDeleteService; @GetMapping("") public ResponseEntity getWorkspaceList(@AuthenticationPrincipal CustomUserDetails userDetails) { @@ -46,6 +48,7 @@ public ResponseEntity updateWorkspace(@PathVariable("workspace_id") Long work public ResponseEntity deleteWorkspace(@PathVariable("workspace_id") Long workspaceId, @AuthenticationPrincipal CustomUserDetails userDetails) { // 워크스페이스 삭제 서비스 코드 + workspaceDeleteService.deleteWorkspace(workspaceId, userDetails.getUser()); return ResponseEntity.ok(ApiUtils.success(null)); } } diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceDeleteService.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceDeleteService.java new file mode 100644 index 00000000..a1f60fdd --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceDeleteService.java @@ -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); + } +} From b85fd45b1650630c41cc8f6196c0c9dc74e2c840 Mon Sep 17 00:00:00 2001 From: Taeho Date: Wed, 4 Oct 2023 22:55:43 +0900 Subject: [PATCH 5/8] =?UTF-8?q?#78=20Feat:=20=EC=9B=8C=ED=81=AC=EC=8A=A4?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WorkspaceController.java | 6 ++- .../dto/WorkspaceGetResponseDto.java | 37 +++++++++++++++++++ .../linknamu/workspace/entity/Workspace.java | 9 ++++- .../repository/WorkspaceJPARepository.java | 3 ++ .../service/WorkspaceReadService.java | 34 +++++++++++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java create mode 100644 linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java index 2ba657e9..12806ddc 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/controller/WorkspaceController.java @@ -5,6 +5,7 @@ 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; @@ -17,14 +18,15 @@ @RequiredArgsConstructor @RequestMapping("/api/workspace") public class WorkspaceController { + private final WorkspaceReadService workspaceReadService; private final WorkspaceSaveService workspaceSaveService; private final WorkspaceUpdateService workspaceUpdateService; private final WorkspaceDeleteService workspaceDeleteService; - @GetMapping("") + @GetMapping("/list") public ResponseEntity getWorkspaceList(@AuthenticationPrincipal CustomUserDetails userDetails) { // 워크스페이스 리스트 조회 서비스 코드 - return ResponseEntity.ok(ApiUtils.success(null)); + return ResponseEntity.ok(ApiUtils.success(workspaceReadService.getWorkspaceList(userDetails.getUser()))); } @PostMapping("/create") diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java new file mode 100644 index 00000000..ac8579f0 --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java @@ -0,0 +1,37 @@ +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 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.getCategoryList().stream() + .map((c) -> new CategoryResponseDto(c.getCategoryId(), c.getCategoryName())) + .toList()) + .build(); + } +} diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java index 1b54222f..03201705 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java @@ -1,5 +1,7 @@ package com.kakao.linknamu.workspace.entity; +import com.kakao.linknamu._core.util.AuditingEntity; +import com.kakao.linknamu.category.entity.Category; import com.kakao.linknamu.user.entity.User; import jakarta.persistence.*; import lombok.*; @@ -7,6 +9,8 @@ import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; @Getter @@ -26,7 +30,7 @@ ) } ) -public class Workspace { +public class Workspace extends AuditingEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="workspace_id") private Long id; @@ -39,6 +43,9 @@ public class Workspace { @Column(length = 50, name = "workspace_name", nullable = false) private String workspaceName; + @OneToMany(mappedBy = "workspace") + private List categoryList = new ArrayList<>(); + @Builder public Workspace(Long id, User user, String workspaceName) { this.id = id; diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java index c244026e..025efeda 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java @@ -12,4 +12,7 @@ public interface WorkspaceJPARepository extends JpaRepository { @Query(value = "select w from Workspace w where w.user.userId=:userId and w.workspaceName =:workspaceName") Optional findByUserIdAndWorkspaceName(@Param("userId") Long userId, @Param("workspaceName") String workspaceName); + + @Query(value = "select w from Workspace w where w.user.userId =:userId") + List findAllByUserId(@Param("userId") Long userId); } diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java new file mode 100644 index 00000000..5e191b89 --- /dev/null +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java @@ -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 getWorkspaceList(User user) { + List workspaceList = workspaceJPARepository.findAllByUserId(user.getUserId()); + if (workspaceList.isEmpty()) return List.of(); + + return workspaceList.stream() + .map(WorkspaceGetResponseDto::of) + .toList(); + } +} From 4c42b53f5dea3c6789d97643862e2d75693c22c9 Mon Sep 17 00:00:00 2001 From: Taeho Date: Thu, 5 Oct 2023 01:29:34 +0900 Subject: [PATCH 6/8] =?UTF-8?q?#78=20Mod:=20=EC=9B=8C=ED=81=AC=EC=8A=A4?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=97=94=ED=8B=B0=ED=8B=B0=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ToString에서 callSuper 추가 및 user, categoryList 제거 - List자료형인 categoryList를 Set으로 변경 --- .../com/kakao/linknamu/workspace/entity/Workspace.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java index 03201705..021b456a 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/entity/Workspace.java @@ -9,14 +9,12 @@ import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.util.*; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -@ToString(exclude = {"user"}) +@ToString(callSuper = true, exclude = {"user", "categoryList"}) @DynamicUpdate @Table( name = "workspace_tb", @@ -44,7 +42,7 @@ public class Workspace extends AuditingEntity { private String workspaceName; @OneToMany(mappedBy = "workspace") - private List categoryList = new ArrayList<>(); + private Set categorySet = new HashSet<>(); @Builder public Workspace(Long id, User user, String workspaceName) { From 7a8d620c42dc66f4e5eb535aa97ccc3ba026282a Mon Sep 17 00:00:00 2001 From: Taeho Date: Thu, 5 Oct 2023 01:30:04 +0900 Subject: [PATCH 7/8] =?UTF-8?q?#78=20Mod:=20OneToMany=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=EB=A5=BC=20=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20fetch=20join=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workspace/repository/WorkspaceJPARepository.java | 6 ++++-- .../linknamu/workspace/service/WorkspaceReadService.java | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java index 025efeda..39a57c30 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/repository/WorkspaceJPARepository.java @@ -13,6 +13,8 @@ public interface WorkspaceJPARepository extends JpaRepository { @Query(value = "select w from Workspace w where w.user.userId=:userId and w.workspaceName =:workspaceName") Optional findByUserIdAndWorkspaceName(@Param("userId") Long userId, @Param("workspaceName") String workspaceName); - @Query(value = "select w from Workspace w where w.user.userId =:userId") - List findAllByUserId(@Param("userId") Long userId); + @Query(value = "select distinct w from Workspace w " + + "join fetch w.categorySet c " + + "where w.user.userId =:userId") + List findAllByUserIdFetchJoinCategory(@Param("userId") Long userId); } diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java index 5e191b89..9ba02def 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/service/WorkspaceReadService.java @@ -24,7 +24,7 @@ public class WorkspaceReadService { private final WorkspaceJPARepository workspaceJPARepository; public List getWorkspaceList(User user) { - List workspaceList = workspaceJPARepository.findAllByUserId(user.getUserId()); + List workspaceList = workspaceJPARepository.findAllByUserIdFetchJoinCategory(user.getUserId()); if (workspaceList.isEmpty()) return List.of(); return workspaceList.stream() From 92f7941e360e752d52f5ca7e093be2af514addc8 Mon Sep 17 00:00:00 2001 From: Taeho Date: Thu, 5 Oct 2023 01:33:55 +0900 Subject: [PATCH 8/8] =?UTF-8?q?#78=20Fix:=20categorySet=EC=88=98=EC=A0=95?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EC=BB=B4=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=97=90=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java index ac8579f0..ed8b4f03 100644 --- a/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java +++ b/linknamu/src/main/java/com/kakao/linknamu/workspace/dto/WorkspaceGetResponseDto.java @@ -29,7 +29,8 @@ public static WorkspaceGetResponseDto of(Workspace workspace) { return WorkspaceGetResponseDto.builder() .workspaceId(workspace.getId()) .workspaceName(workspace.getWorkspaceName()) - .categoryList(workspace.getCategoryList().stream() + .categoryList(workspace.getCategorySet().stream() + .sorted(Comparator.comparing(Category::getCategoryId)) .map((c) -> new CategoryResponseDto(c.getCategoryId(), c.getCategoryName())) .toList()) .build();