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..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 @@ -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; @@ -92,7 +93,19 @@ 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}") + @Operation(summary = "공유그룹 삭제 API", description = "공유그룹을 삭제하는 API입니다. 공유그룹 생성자만 삭제할 수 있습니다.") + @Parameters(value = { + @Parameter(name = "shareGroupId", description = "삭제할 공유그룹 id를 입력해 주세요.") + }) + public ResultResponse deleteShareGroup(@PathVariable Long shareGroupId, + @LoginMember Member member) { + ShareGroup deletedShareGroup = shareGroupService.deleteShareGroup(shareGroupId, member); + return ResultResponse.of(ShareGroupResultCode.DELETE_SHARE_GROUP, + 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 83f2bdf..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 @@ -11,7 +11,6 @@ import org.springframework.stereotype.Component; import java.util.List; -import java.util.stream.Collectors; @Component public class ShareGroupConverter { @@ -36,8 +35,8 @@ public ShareGroupInfo toShareGroupInfo(ShareGroup shareGroup) { .build(); } - // 그룹 참여 시 반환하는 DTO - public ShareGroupResponse.ShareGroupId toJoinShareGroupInfo(ShareGroup shareGroup) { + // 그룹 Id만 반환 (그룹 참여 시, 그룹 삭제 시 반환하는 DTO) + public ShareGroupResponse.ShareGroupId toShareGroupId(ShareGroup shareGroup) { return ShareGroupResponse.ShareGroupId.builder() .shareGroupId(shareGroup.getId()) .build(); @@ -70,6 +69,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/entity/ShareGroup.java b/src/main/java/com/umc/naoman/domain/shareGroup/entity/ShareGroup.java index f3336ca..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 @@ -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,14 @@ public class ShareGroup extends BaseTimeEntity { private int memberCount; @Column(name = "invite_code", nullable = false) private String inviteCode; + @OneToMany(mappedBy = "shareGroup") + @Builder.Default + 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 c33959c..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,4 +18,5 @@ public interface ShareGroupService { Profile findProfile(Long profileId); Profile findProfile(Long shareGroupId, Long memberID); Page getMyShareGroupList(Member member, Pageable pageable); + 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 0cb5129..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 @@ -99,6 +99,23 @@ public Page getMyShareGroupList(Member member, Pageable pageable) { return shareGroupRepository.findByIdIn(shareGroupIdList, pageable); } + @Transactional + @Override + public ShareGroup 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(); + return shareGroup; + } + @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;