Skip to content

Commit

Permalink
Merge pull request #46 from Na-o-man/feature/#45/delete-shareGroup
Browse files Browse the repository at this point in the history
[FEAT] 공유그룹 삭제 API 구현
  • Loading branch information
h-ye-ryoung authored Aug 2, 2024
2 parents 37d75a8 + a6aa188 commit a83fc20
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,7 +93,19 @@ public ResultResponse<ShareGroupResponse.ShareGroupId> 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<ShareGroupResponse.ShareGroupId> deleteShareGroup(@PathVariable Long shareGroupId,
@LoginMember Member member) {
ShareGroup deletedShareGroup = shareGroupService.deleteShareGroup(shareGroupId, member);
return ResultResponse.of(ShareGroupResultCode.DELETE_SHARE_GROUP,
shareGroupConverter.toShareGroupId(deletedShareGroup));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class ShareGroupConverter {
Expand All @@ -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();
Expand Down Expand Up @@ -70,6 +69,7 @@ public ShareGroupResponse.ProfileInfo toProfileInfo(Profile profile) {
.build();
}

// 공유 그룹 목록 반환 DTO
public PagedShareGroupInfo toPagedShareGroupInfo(Page<ShareGroup> shareGroupList) {
// 각 공유 그룹에 대한 상세 정보를 가져오기 (DetailInfo response 재사용)
List<ShareGroupInfo> shareGroupInfoList = shareGroupList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.*;
import org.hibernate.annotations.SQLRestriction;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
Expand All @@ -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<Profile> profiles = new ArrayList<>();

public void delete() {
super.delete();
for (Profile profile : profiles) {
profile.delete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public interface ShareGroupService {
Profile findProfile(Long profileId);
Profile findProfile(Long shareGroupId, Long memberID);
Page<ShareGroup> getMyShareGroupList(Member member, Pageable pageable);
ShareGroup deleteShareGroup(Long shareGroupId, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ public Page<ShareGroup> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a83fc20

Please sign in to comment.