From 89c9da90ba13425cb59fe7453edaa561ff69811f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=98=9C=EB=A0=B9?= Date: Fri, 2 Aug 2024 18:57:40 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EA=B3=B5=EC=9C=A0=EA=B7=B8?= =?UTF-8?q?=EB=A3=B9=20=EC=82=AD=EC=A0=9C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ShareGroupController.java | 13 ++++++++++ .../converter/ShareGroupConverter.java | 11 +++++++-- .../shareGroup/dto/ShareGroupResponse.java | 2 ++ .../shareGroup/service/ShareGroupService.java | 1 + .../service/ShareGroupServiceImpl.java | 24 +++++++++++++++++++ .../error/code/ShareGroupErrorCode.java | 1 + .../result/code/ShareGroupResultCode.java | 3 ++- 7 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java b/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java index b2e25c8..9637836 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java @@ -20,6 +20,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -95,4 +96,16 @@ public ResultResponse joinShareGroup(@Valid @Re shareGroupConverter.toJoinShareGroupInfo(shareGroup)); } + @DeleteMapping("/{shareGroupId}") + @Operation(summary = "공유그룹 삭제 API", description = "공유그룹을 삭제하는 API입니다. 공유그룹 생성자만 삭제할 수 있습니다.") + @Parameters(value = { + @Parameter(name = "shareGroupId", description = "삭제할 공유그룹 id를 입력해 주세요.") + }) + public ResultResponse deleteShareGroup(@PathVariable Long shareGroupId, + @LoginMember Member member) { + shareGroupService.deleteShareGroup(shareGroupId, member); + return ResultResponse.of(ShareGroupResultCode.DELETE_SHARE_GROUP, + shareGroupConverter.toDeleteShareGroupInfo(shareGroupId)); + } + } diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java b/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java index 83f2bdf..bb46fe4 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java @@ -11,7 +11,6 @@ import org.springframework.stereotype.Component; import java.util.List; -import java.util.stream.Collectors; @Component public class ShareGroupConverter { @@ -36,7 +35,14 @@ public ShareGroupInfo toShareGroupInfo(ShareGroup shareGroup) { .build(); } - // 그룹 참여 시 반환하는 DTO + // 그룹 삭제 시 반환하는 정보 DTO + public ShareGroupResponse.ShareGroupId toDeleteShareGroupInfo(Long shareGroupId) { + return ShareGroupResponse.ShareGroupId.builder() + .shareGroupId(shareGroupId) + .build(); + } + + // 그룹 Id만 반환 (그룹 참여 시 반환하는 DTO) public ShareGroupResponse.ShareGroupId toJoinShareGroupInfo(ShareGroup shareGroup) { return ShareGroupResponse.ShareGroupId.builder() .shareGroupId(shareGroup.getId()) @@ -70,6 +76,7 @@ public ShareGroupResponse.ProfileInfo toProfileInfo(Profile profile) { .build(); } + // 공유 그룹 목록 반환 DTO public PagedShareGroupInfo toPagedShareGroupInfo(Page shareGroupList) { // 각 공유 그룹에 대한 상세 정보를 가져오기 (DetailInfo response 재사용) List shareGroupInfoList = shareGroupList diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java b/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java index b32b94e..5c5ae8a 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java @@ -55,6 +55,8 @@ public static class ShareGroupId { private Long shareGroupId; } + + @Getter @Builder @NoArgsConstructor diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java index c33959c..451a8d2 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java @@ -18,4 +18,5 @@ public interface ShareGroupService { Profile findProfile(Long profileId); Profile findProfile(Long shareGroupId, Long memberID); Page getMyShareGroupList(Member member, Pageable pageable); + void deleteShareGroup(Long shareGroupId, Member member); } diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java index 0cb5129..cf981e0 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java @@ -99,6 +99,30 @@ public Page getMyShareGroupList(Member member, Pageable pageable) { return shareGroupRepository.findByIdIn(shareGroupIdList, pageable); } + @Transactional + @Override + public void deleteShareGroup(Long shareGroupId, Member member) { + ShareGroup shareGroup = findShareGroup(shareGroupId); //해당 공유그룹 + // 공유그룹 id와 멤버 id로 공유그룹 내 프로필 찾기 + Profile creatorProfile = findProfile(shareGroupId, member.getId()); + + // 프로필 내 역할이 creator가 아니면 에러 + if (creatorProfile.getRole() != Role.CREATOR) { + throw new BusinessException(ShareGroupErrorCode.UNAUTHORIZED_DELETE); + } + + // 공유그룹 삭제 처리 + shareGroup.delete(); + shareGroupRepository.save(shareGroup); + + // 해당 공유그룹의 모든 프로필 삭제 처리 + List profiles = findProfileList(shareGroupId); + for (Profile profile : profiles) { + profile.delete(); + profileRepository.save(profile); + } + } + @Override public ShareGroup findShareGroup(Long shareGroupId) { return shareGroupRepository.findById(shareGroupId) diff --git a/src/main/java/com/umc/naoman/global/error/code/ShareGroupErrorCode.java b/src/main/java/com/umc/naoman/global/error/code/ShareGroupErrorCode.java index a67406a..3dbd476 100644 --- a/src/main/java/com/umc/naoman/global/error/code/ShareGroupErrorCode.java +++ b/src/main/java/com/umc/naoman/global/error/code/ShareGroupErrorCode.java @@ -17,6 +17,7 @@ public enum ShareGroupErrorCode implements ErrorCode { PROFILE_NOT_FOUND(404, "EG006", "프로필을 찾을 수 없습니다."), INVALID_PROFILE_FOR_GROUP(400, "EG007", "해당 프로필은 이 공유 그룹에 속하지 않습니다."), + UNAUTHORIZED_DELETE(403, "EG008", "공유 그룹을 삭제할 권한이 없습니다."), ; private final int status; diff --git a/src/main/java/com/umc/naoman/global/result/code/ShareGroupResultCode.java b/src/main/java/com/umc/naoman/global/result/code/ShareGroupResultCode.java index d51edba..6463a70 100644 --- a/src/main/java/com/umc/naoman/global/result/code/ShareGroupResultCode.java +++ b/src/main/java/com/umc/naoman/global/result/code/ShareGroupResultCode.java @@ -10,7 +10,8 @@ public enum ShareGroupResultCode implements ResultCode { CREATE_SHARE_GROUP(200, "SG001", "성공적으로 공유 그룹을 생성하였습니다."), SHARE_GROUP_INFO(200, "SG002", "공유 그룹을 성공적으로 조회했습니다."), JOIN_SHARE_GROUP(200, "SG003", "성공적으로 공유 그룹에 참여했습니다." ), - SHARE_GROUP_INFO_LIST(200, "SG004", "내가 참여한 공유 그룹 목록을 성공적으로 조회하였습니다.") + SHARE_GROUP_INFO_LIST(200, "SG004", "내가 참여한 공유 그룹 목록을 성공적으로 조회하였습니다."), + DELETE_SHARE_GROUP(200, "SG005", "성공적으로 공유 그룹을 삭제했습니다.") ; private final int status; private final String code; From b40e07a835635cb69d95cac9828690cb27053950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=98=9C=EB=A0=B9?= Date: Fri, 2 Aug 2024 23:18:07 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EB=A6=AC=EB=B7=B0=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shareGroup/controller/ShareGroupController.java | 6 +++--- .../shareGroup/converter/ShareGroupConverter.java | 11 ++--------- .../domain/shareGroup/dto/ShareGroupResponse.java | 2 -- .../naoman/domain/shareGroup/entity/ShareGroup.java | 11 +++++++++++ .../domain/shareGroup/service/ShareGroupService.java | 2 +- .../shareGroup/service/ShareGroupServiceImpl.java | 9 ++------- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java b/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java index 9637836..3ab65d2 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/controller/ShareGroupController.java @@ -93,7 +93,7 @@ public ResultResponse joinShareGroup(@Valid @Re @LoginMember Member member) { ShareGroup shareGroup = shareGroupService.joinShareGroup(request.getShareGroupId(), request.getProfileId(), member); return ResultResponse.of(ShareGroupResultCode.JOIN_SHARE_GROUP, - shareGroupConverter.toJoinShareGroupInfo(shareGroup)); + shareGroupConverter.toShareGroupId(shareGroup)); } @DeleteMapping("/{shareGroupId}") @@ -103,9 +103,9 @@ public ResultResponse joinShareGroup(@Valid @Re }) public ResultResponse deleteShareGroup(@PathVariable Long shareGroupId, @LoginMember Member member) { - shareGroupService.deleteShareGroup(shareGroupId, member); + ShareGroup deletedShareGroup = shareGroupService.deleteShareGroup(shareGroupId, member); return ResultResponse.of(ShareGroupResultCode.DELETE_SHARE_GROUP, - shareGroupConverter.toDeleteShareGroupInfo(shareGroupId)); + shareGroupConverter.toShareGroupId(deletedShareGroup)); } } diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java b/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java index bb46fe4..e8e29e5 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/converter/ShareGroupConverter.java @@ -35,15 +35,8 @@ public ShareGroupInfo toShareGroupInfo(ShareGroup shareGroup) { .build(); } - // 그룹 삭제 시 반환하는 정보 DTO - public ShareGroupResponse.ShareGroupId toDeleteShareGroupInfo(Long shareGroupId) { - return ShareGroupResponse.ShareGroupId.builder() - .shareGroupId(shareGroupId) - .build(); - } - - // 그룹 Id만 반환 (그룹 참여 시 반환하는 DTO) - public ShareGroupResponse.ShareGroupId toJoinShareGroupInfo(ShareGroup shareGroup) { + // 그룹 Id만 반환 (그룹 참여 시, 그룹 삭제 시 반환하는 DTO) + public ShareGroupResponse.ShareGroupId toShareGroupId(ShareGroup shareGroup) { return ShareGroupResponse.ShareGroupId.builder() .shareGroupId(shareGroup.getId()) .build(); diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java b/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java index 5c5ae8a..b32b94e 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupResponse.java @@ -55,8 +55,6 @@ public static class ShareGroupId { private Long shareGroupId; } - - @Getter @Builder @NoArgsConstructor diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java b/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java index f3336ca..d98fbfe 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java @@ -5,6 +5,8 @@ import lombok.*; import org.hibernate.annotations.SQLRestriction; +import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; @Entity @@ -26,4 +28,13 @@ public class ShareGroup extends BaseTimeEntity { private int memberCount; @Column(name = "invite_code", nullable = false) private String inviteCode; + @OneToMany(mappedBy = "shareGroup") + private List profiles = new ArrayList<>(); + + public void delete() { + super.delete(); + for (Profile profile : profiles) { + profile.delete(); + } + } } diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java index 451a8d2..fcadd52 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupService.java @@ -18,5 +18,5 @@ public interface ShareGroupService { Profile findProfile(Long profileId); Profile findProfile(Long shareGroupId, Long memberID); Page getMyShareGroupList(Member member, Pageable pageable); - void deleteShareGroup(Long shareGroupId, Member member); + ShareGroup deleteShareGroup(Long shareGroupId, Member member); } diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java index cf981e0..c0bf076 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java @@ -101,7 +101,7 @@ public Page getMyShareGroupList(Member member, Pageable pageable) { @Transactional @Override - public void deleteShareGroup(Long shareGroupId, Member member) { + public ShareGroup deleteShareGroup(Long shareGroupId, Member member) { ShareGroup shareGroup = findShareGroup(shareGroupId); //해당 공유그룹 // 공유그룹 id와 멤버 id로 공유그룹 내 프로필 찾기 Profile creatorProfile = findProfile(shareGroupId, member.getId()); @@ -115,12 +115,7 @@ public void deleteShareGroup(Long shareGroupId, Member member) { shareGroup.delete(); shareGroupRepository.save(shareGroup); - // 해당 공유그룹의 모든 프로필 삭제 처리 - List profiles = findProfileList(shareGroupId); - for (Profile profile : profiles) { - profile.delete(); - profileRepository.save(profile); - } + return shareGroup; } @Override From a6aa18865cdffdfc129e035fd333438630e7a830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=98=9C=EB=A0=B9?= Date: Fri, 2 Aug 2024 23:39:22 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20@Bulder.Default=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/umc/naoman/domain/shareGroup/entity/ShareGroup.java | 1 + .../naoman/domain/shareGroup/service/ShareGroupServiceImpl.java | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java b/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java index d98fbfe..6e9464e 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java @@ -29,6 +29,7 @@ public class ShareGroup extends BaseTimeEntity { @Column(name = "invite_code", nullable = false) private String inviteCode; @OneToMany(mappedBy = "shareGroup") + @Builder.Default private List profiles = new ArrayList<>(); public void delete() { diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java index c0bf076..223dd3a 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/service/ShareGroupServiceImpl.java @@ -113,8 +113,6 @@ public ShareGroup deleteShareGroup(Long shareGroupId, Member member) { // 공유그룹 삭제 처리 shareGroup.delete(); - shareGroupRepository.save(shareGroup); - return shareGroup; }