From 1966d2634d0e962438563c9e5b027b8060ca44dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=98=9C=EB=A0=B9?= Date: Thu, 1 Aug 2024 19:11:18 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20memberCount=20=ED=95=84=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shareGroup/controller/ShareGroupController.java | 2 +- .../shareGroup/converter/ShareGroupConverter.java | 7 ++++--- .../domain/shareGroup/dto/ShareGroupRequest.java | 2 -- .../domain/shareGroup/dto/ShareGroupResponse.java | 11 +++++++++++ 4 files changed, 16 insertions(+), 6 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 7477dbc..2a4431c 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 @@ -46,7 +46,7 @@ public class ShareGroupController { @Parameters(value = { @Parameter(name = "shareGroupId", description = "특정 공유그룹 id를 입력해 주세요.") }) - public ResultResponse getShareGroupInfo(@PathVariable(name = "shareGroupId") Long shareGroupId) { + public ResultResponse getShareGroupInfo(@PathVariable(name = "shareGroupId") Long shareGroupId) { ShareGroup shareGroup = shareGroupService.findShareGroup(shareGroupId); List profileList = shareGroupService.findProfileList(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 055a952..56f5509 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 @@ -14,6 +14,7 @@ public class ShareGroupConverter { public static ShareGroup toEntity(ShareGroupRequest.createShareGroupRequest request) { return ShareGroup.builder() .memberCount(request.getMemberNameList().size()) // 변경 가능성 있음. memberCount 대신 nameList의 size 사용 + .name("임시 공유 그룹 이름") //임시 공유 그룹 이름 .build(); } @@ -23,7 +24,7 @@ public static ShareGroup toEntity(ShareGroupRequest.createShareGroupRequest requ public static ShareGroupResponse.ShareGroupInfo toShareGroupInfoDTO(ShareGroup shareGroup) { return ShareGroupResponse.ShareGroupInfo.builder() .shareGroupId(shareGroup.getId()) - .name("임시 공유 그룹 이름") //임시 공유 그룹 이름 (고유 코드 출력) + .name(shareGroup.getName()) .inviteUrl(baseUrl + shareGroup.getInviteCode()) .createdAt(shareGroup.getCreatedAt()) .build(); @@ -37,12 +38,12 @@ public static ShareGroupResponse.ShareGroupId toJoinShareGroupInfoDTO(ShareGroup } // 조회 시, 디테일한 그룹 정보를 반환하는 DTO - public static ShareGroupResponse.ShareGroupInfo toShareGroupDetailInfoDTO(ShareGroup shareGroup, List profiles) { + public static ShareGroupResponse.ShareGroupDetailInfo toShareGroupDetailInfoDTO(ShareGroup shareGroup, List profiles) { List profileInfoList = profiles.stream() .map(ShareGroupConverter::toProfileInfo) .toList(); - return ShareGroupResponse.ShareGroupInfo.builder() + return ShareGroupResponse.ShareGroupDetailInfo.builder() .shareGroupId(shareGroup.getId()) .name("임시 공유 그룹 이름") //임시 공유 그룹 이름 (고유 코드 출력) .image(shareGroup.getImage()) diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupRequest.java b/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupRequest.java index 29a29eb..7869d4c 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupRequest.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/dto/ShareGroupRequest.java @@ -17,8 +17,6 @@ public abstract class ShareGroupRequest { @NoArgsConstructor @AllArgsConstructor public static class createShareGroupRequest { - @NotNull(message = "멤버 수를 입력해야 합니다.") - private Integer memberCount; @NotEmpty(message = "멤버 이름 리스트는 비어 있을 수 없습니다.") private List memberNameList; @NotEmpty(message = "모임의 성격을 하나 이상 선택해야 합니다.") 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 242b1a8..7acee6d 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 @@ -19,6 +19,17 @@ public static class ShareGroupInfo { private Long shareGroupId; private String name; //공유그룹 이름 반환 private String inviteUrl; //공유그룹 초대 코드 반환 + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") + private LocalDateTime createdAt; + } + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ShareGroupDetailInfo { + private Long shareGroupId; + private String name; //공유그룹 이름 반환 private String image; //공유그룹 대표 이미지 반환 private int memberCount; private List profileInfoList; From 3c1b09392df34cc8dcb5bb0d273e8b069fee4d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=98=9C=EB=A0=B9?= Date: Thu, 1 Aug 2024 19:33:09 +0900 Subject: [PATCH 2/4] =?UTF-8?q?featrue:=20=EC=B4=88=EB=8C=80=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=20=EA=B7=B8=EB=A3=B9=EC=A1=B0=ED=9A=8C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ShareGroupController.java | 15 +++++++++++++++ .../repository/ShareGroupRepository.java | 3 +++ .../shareGroup/service/ShareGroupService.java | 1 + .../shareGroup/service/ShareGroupServiceImpl.java | 6 ++++++ 4 files changed, 25 insertions(+) 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 2a4431c..0581d0a 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.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -55,6 +56,20 @@ public ResultResponse getShareGroupInfo ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profileList)); } + @GetMapping + @Operation(summary = "초대 코드로 공유그룹 조회 API", description = "inviteCode로 특정 공유그룹을 조회하는 API입니다.") + @Parameters(value = { + @Parameter(name = "inviteCode", description = "참여하려는 공유그룹의 초대 코드") + }) + public ResultResponse getShareGroupByInviteCode(@RequestParam String inviteCode) { + + ShareGroup shareGroup = shareGroupService.findShareGroupByInviteCode(inviteCode); + List profileList = shareGroupService.findProfileList(shareGroup.getId()); + + return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO, + ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profileList)); + } + @PostMapping("/join") @Operation(summary = "공유그룹 참여 API", description = "특정 공유그룹에 참여하는 API입니다.") public ResultResponse joinShareGroup(@Valid @RequestBody ShareGroupRequest.JoinShareGroupRequest request, diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java index 1715e9f..2f03ced 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java @@ -4,6 +4,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface ShareGroupRepository extends JpaRepository { + Optional findByInviteCode(String inviteCode); } 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 c91006d..26b1bc5 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 @@ -10,6 +10,7 @@ public interface ShareGroupService { ShareGroup createShareGroup(ShareGroupRequest.createShareGroupRequest request, Member member); ShareGroup findShareGroup(Long shareGroupId); + ShareGroup findShareGroupByInviteCode(String inviteCode); List findProfileList(Long shareGroupId); ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member member); Profile findProfile(Long profileId); 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 8e9090a..8be1d91 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 @@ -89,6 +89,12 @@ public ShareGroup findShareGroup(Long shareGroupId) { .orElseThrow(() -> new BusinessException(ShareGroupErrorCode.SHARE_GROUP_NOT_FOUND)); } + @Override + public ShareGroup findShareGroupByInviteCode(String inviteCode) { + return shareGroupRepository.findByInviteCode(inviteCode) + .orElseThrow(() -> new BusinessException(ShareGroupErrorCode.SHARE_GROUP_NOT_FOUND)); + } + @Override public List findProfileList(Long shareGroupId) { return profileRepository.findByShareGroupId(shareGroupId); From e8a7a5bfadfde0da8ac8213f979a05a8d065a80d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=98=9C=EB=A0=B9?= Date: Thu, 1 Aug 2024 23:38:09 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EB=82=B4=EA=B0=80=20=EC=B0=B8?= =?UTF-8?q?=EC=97=AC=ED=95=9C=20=EA=B3=B5=EC=9C=A0=EA=B7=B8=EB=A3=B9=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=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 | 15 +++++++- .../converter/ShareGroupConverter.java | 3 ++ .../shareGroup/dto/ShareGroupResponse.java | 12 +++++++ .../repository/ProfileRepository.java | 2 ++ .../repository/ShareGroupRepository.java | 4 +++ .../shareGroup/service/ShareGroupService.java | 5 ++- .../service/ShareGroupServiceImpl.java | 36 ++++++++++++++++++- .../result/code/ShareGroupResultCode.java | 4 +-- 8 files changed, 76 insertions(+), 5 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 0581d0a..037da58 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 @@ -15,6 +15,8 @@ import io.swagger.v3.oas.annotations.Parameters; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -63,13 +65,24 @@ public ResultResponse getShareGroupInfo }) public ResultResponse getShareGroupByInviteCode(@RequestParam String inviteCode) { - ShareGroup shareGroup = shareGroupService.findShareGroupByInviteCode(inviteCode); + ShareGroup shareGroup = shareGroupService.findShareGroup(inviteCode); List profileList = shareGroupService.findProfileList(shareGroup.getId()); return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO, ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profileList)); } + @GetMapping("/my") + @Operation(summary = "내가 참여한 공유그룹 목록 조회 API", description = "내가 참여한 공유그룹 목록을 페이징 처리하여 조회하는 API입니다.") + @Parameters(value = { + @Parameter(name = "page", description = "조회할 페이지를 입력해 주세요.(0번부터 시작)"), + @Parameter(name = "size", description = "한 페이지에 나타낼 공유그룹 개수를 입력해주세요.") + }) + public ResultResponse getMyShareGroupList(@LoginMember Member member, @Parameter(hidden = true) Pageable pageable) { + ShareGroupResponse.ShareGroupInfoList shareGroups = shareGroupService.getMyShareGroupList(member, pageable); + return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO_LIST, shareGroups); + } + @PostMapping("/join") @Operation(summary = "공유그룹 참여 API", description = "특정 공유그룹에 참여하는 API입니다.") public ResultResponse joinShareGroup(@Valid @RequestBody ShareGroupRequest.JoinShareGroupRequest request, 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 56f5509..62275f6 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 @@ -4,9 +4,12 @@ import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; +import org.springframework.data.domain.Page; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class ShareGroupConverter { 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 7acee6d..52eb9d3 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 @@ -38,6 +38,18 @@ public static class ShareGroupDetailInfo { private LocalDateTime createdAt; } + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ShareGroupInfoList { + private List shareGroupDetailInfoList; //공유그룹 상세 정보 리스트 + private int page; // 페이지 번호 + private long totalElements; // 해당 조건에 부합하는 요소의 총 개수 + private boolean isFirst; // 첫 페이지 여부 + private boolean isLast; // 마지막 페이지 여부 + } + @Getter @Builder @NoArgsConstructor diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java index a1cb199..ae41103 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java @@ -1,5 +1,6 @@ package com.umc.naoman.domain.shareGroup.repository; +import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.shareGroup.entity.Profile; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +10,5 @@ @Repository public interface ProfileRepository extends JpaRepository { List findByShareGroupId(Long shareGroupId); + List findByMember(Member member); } diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java index 2f03ced..53d3e13 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ShareGroupRepository.java @@ -1,12 +1,16 @@ package com.umc.naoman.domain.shareGroup.repository; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository public interface ShareGroupRepository extends JpaRepository { Optional findByInviteCode(String inviteCode); + Page findByIdIn(List shareGroupIds, Pageable pageable); } 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 26b1bc5..3aafea6 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 @@ -2,16 +2,19 @@ import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; +import org.springframework.data.domain.Pageable; import java.util.List; public interface ShareGroupService { ShareGroup createShareGroup(ShareGroupRequest.createShareGroupRequest request, Member member); ShareGroup findShareGroup(Long shareGroupId); - ShareGroup findShareGroupByInviteCode(String inviteCode); + ShareGroup findShareGroup(String inviteCode); List findProfileList(Long shareGroupId); ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member member); Profile findProfile(Long profileId); + ShareGroupResponse.ShareGroupInfoList getMyShareGroupList(Member member, Pageable pageable); } 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 8be1d91..76135c2 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 @@ -4,6 +4,7 @@ import com.umc.naoman.domain.member.repository.MemberRepository; import com.umc.naoman.domain.shareGroup.converter.ShareGroupConverter; import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.Role; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; @@ -13,12 +14,15 @@ import com.umc.naoman.global.error.code.MemberErrorCode; import com.umc.naoman.global.error.code.ShareGroupErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Service @Transactional(readOnly = true) @@ -83,6 +87,36 @@ public ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member membe return shareGroup; } + @Override + public ShareGroupResponse.ShareGroupInfoList getMyShareGroupList(Member member, Pageable pageable) { + + // 멤버를 통해 profile을 가져와서, 해당 profile의 shareGroupId를 추출 + List shareGroupIds = profileRepository.findByMember(member).stream() + .map(profile -> profile.getShareGroup().getId()) + .distinct() // 중복된 공유 그룹 ID 제거 + .collect(Collectors.toList()); // 리스트로 수집 + + // 추출한 공유 그룹 ID 리스트를 통해 해당 공유 그룹들을 페이징 처리하여 가져오기 + Page shareGroupPage = shareGroupRepository.findByIdIn(shareGroupIds, pageable); + + // 각 공유 그룹에 대한 상세 정보를 가져오기 (DetailInfo response 재사용) + List shareGroupDetailInfoList = shareGroupPage.getContent().stream() + .map(shareGroup -> { + List profiles = findProfileList(shareGroup.getId()); // profile 리스트 가져오기 + return ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profiles); // 공유그룹 DetailInfo 반환 + }) + .collect(Collectors.toList()); + + // 최종적으로 공유 그룹 목록 정보를 DTO로 변환하여 반환 + return ShareGroupResponse.ShareGroupInfoList.builder() + .shareGroupDetailInfoList(shareGroupDetailInfoList) // 만든 info 리스트 + .page(shareGroupPage.getNumber()) + .totalElements(shareGroupPage.getTotalElements()) + .isFirst(shareGroupPage.isFirst()) + .isLast(shareGroupPage.isLast()) + .build(); + } + @Override public ShareGroup findShareGroup(Long shareGroupId) { return shareGroupRepository.findById(shareGroupId) @@ -90,7 +124,7 @@ public ShareGroup findShareGroup(Long shareGroupId) { } @Override - public ShareGroup findShareGroupByInviteCode(String inviteCode) { + public ShareGroup findShareGroup(String inviteCode) { return shareGroupRepository.findByInviteCode(inviteCode) .orElseThrow(() -> new BusinessException(ShareGroupErrorCode.SHARE_GROUP_NOT_FOUND)); } 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 a889a21..d51edba 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 @@ -9,8 +9,8 @@ public enum ShareGroupResultCode implements ResultCode { CREATE_SHARE_GROUP(200, "SG001", "성공적으로 공유 그룹을 생성하였습니다."), SHARE_GROUP_INFO(200, "SG002", "공유 그룹을 성공적으로 조회했습니다."), - JOIN_SHARE_GROUP(200, "SG003", "성공적으로 공유 그룹에 참여했습니다." ) - + JOIN_SHARE_GROUP(200, "SG003", "성공적으로 공유 그룹에 참여했습니다." ), + SHARE_GROUP_INFO_LIST(200, "SG004", "내가 참여한 공유 그룹 목록을 성공적으로 조회하였습니다.") ; private final int status; private final String code; From c9cdce99b7a77a8e925344d04bacfff03f4120c5 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 01:55:52 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=EB=82=B4=20=EA=B3=B5=EC=9C=A0?= =?UTF-8?q?=EA=B7=B8=EB=A3=B9=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ShareGroupController.java | 31 ++++++----- .../converter/ShareGroupConverter.java | 51 +++++++++++++------ .../shareGroup/dto/ShareGroupResponse.java | 23 ++++----- .../repository/ProfileRepository.java | 3 +- .../shareGroup/service/ShareGroupService.java | 4 +- .../service/ShareGroupServiceImpl.java | 32 +++--------- 6 files changed, 73 insertions(+), 71 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 037da58..b2e25c8 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 @@ -3,6 +3,7 @@ import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.shareGroup.converter.ShareGroupConverter; import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest.createShareGroupRequest; import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; @@ -17,6 +18,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -31,17 +34,16 @@ @RequiredArgsConstructor @RequestMapping("/shareGroups") public class ShareGroupController { - private final ShareGroupService shareGroupService; + private final ShareGroupConverter shareGroupConverter; @PostMapping @Operation(summary = "공유그룹 생성 API", description = "새로운 공유그룹을 생성하는 API입니다.") public ResultResponse createShareGroup(@Valid @RequestBody ShareGroupRequest.createShareGroupRequest request, + .ShareGroupInfo> createShareGroup(@Valid @RequestBody createShareGroupRequest request, @LoginMember Member member) { - ShareGroup shareGroup = shareGroupService.createShareGroup(request, member); - return ResultResponse.of(ShareGroupResultCode.CREATE_SHARE_GROUP, ShareGroupConverter.toShareGroupInfoDTO(shareGroup)); + return ResultResponse.of(ShareGroupResultCode.CREATE_SHARE_GROUP, shareGroupConverter.toShareGroupInfo(shareGroup)); } @GetMapping("/{shareGroupId}") @@ -49,13 +51,12 @@ public class ShareGroupController { @Parameters(value = { @Parameter(name = "shareGroupId", description = "특정 공유그룹 id를 입력해 주세요.") }) - public ResultResponse getShareGroupInfo(@PathVariable(name = "shareGroupId") Long shareGroupId) { - + public ResultResponse getShareGroupDetailInfo(@PathVariable(name = "shareGroupId") Long shareGroupId) { ShareGroup shareGroup = shareGroupService.findShareGroup(shareGroupId); List profileList = shareGroupService.findProfileList(shareGroupId); return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO, - ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profileList)); + shareGroupConverter.toShareGroupDetailInfo(shareGroup, profileList)); } @GetMapping @@ -64,12 +65,11 @@ public ResultResponse getShareGroupInfo @Parameter(name = "inviteCode", description = "참여하려는 공유그룹의 초대 코드") }) public ResultResponse getShareGroupByInviteCode(@RequestParam String inviteCode) { - ShareGroup shareGroup = shareGroupService.findShareGroup(inviteCode); List profileList = shareGroupService.findProfileList(shareGroup.getId()); return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO, - ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profileList)); + shareGroupConverter.toShareGroupDetailInfo(shareGroup, profileList)); } @GetMapping("/my") @@ -78,18 +78,21 @@ public ResultResponse getShareGroupByIn @Parameter(name = "page", description = "조회할 페이지를 입력해 주세요.(0번부터 시작)"), @Parameter(name = "size", description = "한 페이지에 나타낼 공유그룹 개수를 입력해주세요.") }) - public ResultResponse getMyShareGroupList(@LoginMember Member member, @Parameter(hidden = true) Pageable pageable) { - ShareGroupResponse.ShareGroupInfoList shareGroups = shareGroupService.getMyShareGroupList(member, pageable); - return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO_LIST, shareGroups); + public ResultResponse getMyShareGroupList(@LoginMember Member member, + @PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) + @Parameter(hidden = true) Pageable pageable) { + Page shareGroupList = shareGroupService.getMyShareGroupList(member, pageable); + return ResultResponse.of(ShareGroupResultCode.SHARE_GROUP_INFO_LIST, + shareGroupConverter.toPagedShareGroupInfo(shareGroupList)); } @PostMapping("/join") @Operation(summary = "공유그룹 참여 API", description = "특정 공유그룹에 참여하는 API입니다.") public ResultResponse joinShareGroup(@Valid @RequestBody ShareGroupRequest.JoinShareGroupRequest request, - @LoginMember Member member) { + @LoginMember Member member) { ShareGroup shareGroup = shareGroupService.joinShareGroup(request.getShareGroupId(), request.getProfileId(), member); return ResultResponse.of(ShareGroupResultCode.JOIN_SHARE_GROUP, - ShareGroupConverter.toJoinShareGroupInfoDTO(shareGroup)); + shareGroupConverter.toJoinShareGroupInfo(shareGroup)); } } 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 62275f6..83f2bdf 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 @@ -2,62 +2,66 @@ import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest; import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse.PagedShareGroupInfo; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse.ShareGroupDetailInfo; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse.ShareGroupInfo; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; import org.springframework.data.domain.Page; +import org.springframework.stereotype.Component; -import java.time.LocalDateTime; -import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; +@Component public class ShareGroupConverter { + private static final String BASE_URL = "https://na0man/invite/"; //baseUrl 상수 - public static ShareGroup toEntity(ShareGroupRequest.createShareGroupRequest request) { + public ShareGroup toEntity(ShareGroupRequest.createShareGroupRequest request) { return ShareGroup.builder() .memberCount(request.getMemberNameList().size()) // 변경 가능성 있음. memberCount 대신 nameList의 size 사용 .name("임시 공유 그룹 이름") //임시 공유 그룹 이름 .build(); } - private static final String baseUrl = "https://na0man/invite/"; //baseUrl 상수 - // 그룹 생성 시 반환하는 정보 DTO - public static ShareGroupResponse.ShareGroupInfo toShareGroupInfoDTO(ShareGroup shareGroup) { - return ShareGroupResponse.ShareGroupInfo.builder() + public ShareGroupInfo toShareGroupInfo(ShareGroup shareGroup) { + return ShareGroupInfo.builder() .shareGroupId(shareGroup.getId()) .name(shareGroup.getName()) - .inviteUrl(baseUrl + shareGroup.getInviteCode()) + .image(shareGroup.getImage()) + .memberCount(shareGroup.getMemberCount()) + .inviteUrl(BASE_URL + shareGroup.getInviteCode()) .createdAt(shareGroup.getCreatedAt()) .build(); } // 그룹 참여 시 반환하는 DTO - public static ShareGroupResponse.ShareGroupId toJoinShareGroupInfoDTO(ShareGroup shareGroup) { + public ShareGroupResponse.ShareGroupId toJoinShareGroupInfo(ShareGroup shareGroup) { return ShareGroupResponse.ShareGroupId.builder() .shareGroupId(shareGroup.getId()) .build(); } // 조회 시, 디테일한 그룹 정보를 반환하는 DTO - public static ShareGroupResponse.ShareGroupDetailInfo toShareGroupDetailInfoDTO(ShareGroup shareGroup, List profiles) { + public ShareGroupDetailInfo toShareGroupDetailInfo(ShareGroup shareGroup, List profiles) { List profileInfoList = profiles.stream() - .map(ShareGroupConverter::toProfileInfo) + .map(this::toProfileInfo) .toList(); - return ShareGroupResponse.ShareGroupDetailInfo.builder() + return ShareGroupDetailInfo.builder() .shareGroupId(shareGroup.getId()) - .name("임시 공유 그룹 이름") //임시 공유 그룹 이름 (고유 코드 출력) + .name(shareGroup.getName()) //임시 공유 그룹 이름 (고유 코드 출력) .image(shareGroup.getImage()) .memberCount(shareGroup.getMemberCount()) + .inviteUrl(BASE_URL + shareGroup.getInviteCode()) .profileInfoList(profileInfoList) //프로필 리스트 출력 .createdAt(shareGroup.getCreatedAt()) .build(); } // profile 정보 반환 DTO - public static ShareGroupResponse.ProfileInfo toProfileInfo(Profile profile) { + public ShareGroupResponse.ProfileInfo toProfileInfo(Profile profile) { return ShareGroupResponse.ProfileInfo.builder() .profileId(profile.getId()) .name(profile.getName()) @@ -66,4 +70,21 @@ public static ShareGroupResponse.ProfileInfo toProfileInfo(Profile profile) { .build(); } + public PagedShareGroupInfo toPagedShareGroupInfo(Page shareGroupList) { + // 각 공유 그룹에 대한 상세 정보를 가져오기 (DetailInfo response 재사용) + List shareGroupInfoList = shareGroupList + .stream() + .map(this::toShareGroupInfo) + .toList(); + + return PagedShareGroupInfo.builder() + .shareGroupInfoList(shareGroupInfoList) // 만든 info 리스트 + .page(shareGroupList.getNumber()) + .totalElements(shareGroupList.getTotalElements()) + .isFirst(shareGroupList.isFirst()) + .isLast(shareGroupList.isLast()) + .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 52eb9d3..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 @@ -5,6 +5,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; import java.time.LocalDateTime; import java.util.List; @@ -12,38 +13,34 @@ public abstract class ShareGroupResponse { @Getter - @Builder + @SuperBuilder @NoArgsConstructor @AllArgsConstructor public static class ShareGroupInfo { private Long shareGroupId; private String name; //공유그룹 이름 반환 + private String image; //공유그룹 대표 이미지 반환. 처음 공유 그룹 생성 시에는 null + private int memberCount; //공유 그룹의 프로필 개수 private String inviteUrl; //공유그룹 초대 코드 반환 - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") + + @JsonFormat(pattern = "yyyy-MM-dd") private LocalDateTime createdAt; } @Getter - @Builder + @SuperBuilder @NoArgsConstructor @AllArgsConstructor - public static class ShareGroupDetailInfo { - private Long shareGroupId; - private String name; //공유그룹 이름 반환 - private String image; //공유그룹 대표 이미지 반환 - private int memberCount; + public static class ShareGroupDetailInfo extends ShareGroupInfo { private List profileInfoList; - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") - private LocalDateTime createdAt; } @Getter @Builder @NoArgsConstructor @AllArgsConstructor - public static class ShareGroupInfoList { - private List shareGroupDetailInfoList; //공유그룹 상세 정보 리스트 + public static class PagedShareGroupInfo { + private List shareGroupInfoList; //공유그룹 상세 정보 리스트 private int page; // 페이지 번호 private long totalElements; // 해당 조건에 부합하는 요소의 총 개수 private boolean isFirst; // 첫 페이지 여부 diff --git a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java index ae41103..2142f44 100644 --- a/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java +++ b/src/main/java/com/umc/naoman/domain/shareGroup/repository/ProfileRepository.java @@ -1,6 +1,5 @@ package com.umc.naoman.domain.shareGroup.repository; -import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.shareGroup.entity.Profile; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -10,5 +9,5 @@ @Repository public interface ProfileRepository extends JpaRepository { List findByShareGroupId(Long shareGroupId); - List findByMember(Member member); + List findByMemberId(Long memberId); } 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 3aafea6..6a20177 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 @@ -2,9 +2,9 @@ import com.umc.naoman.domain.member.entity.Member; import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest; -import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.List; @@ -16,5 +16,5 @@ public interface ShareGroupService { List findProfileList(Long shareGroupId); ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member member); Profile findProfile(Long profileId); - ShareGroupResponse.ShareGroupInfoList getMyShareGroupList(Member member, Pageable pageable); + Page getMyShareGroupList(Member member, Pageable pageable); } 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 76135c2..216a584 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 @@ -5,13 +5,13 @@ import com.umc.naoman.domain.shareGroup.converter.ShareGroupConverter; import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest; import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse; +import com.umc.naoman.domain.shareGroup.dto.ShareGroupResponse.PagedShareGroupInfo; import com.umc.naoman.domain.shareGroup.entity.Profile; import com.umc.naoman.domain.shareGroup.entity.Role; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; import com.umc.naoman.domain.shareGroup.repository.ProfileRepository; import com.umc.naoman.domain.shareGroup.repository.ShareGroupRepository; import com.umc.naoman.global.error.BusinessException; -import com.umc.naoman.global.error.code.MemberErrorCode; import com.umc.naoman.global.error.code.ShareGroupErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -32,16 +32,16 @@ public class ShareGroupServiceImpl implements ShareGroupService { private final ShareGroupRepository shareGroupRepository; private final ProfileRepository profileRepository; private final MemberRepository memberRepository; + private final ShareGroupConverter shareGroupConverter; @Transactional @Override public ShareGroup createShareGroup(ShareGroupRequest.createShareGroupRequest request, Member member) { - // 초대링크를 위한 고유번호 생성 (UUID) String inviteCode = UUID.randomUUID().toString().replace("-", "").toUpperCase(); // 변환 로직 - ShareGroup newShareGroup = ShareGroupConverter.toEntity(request); + ShareGroup newShareGroup = shareGroupConverter.toEntity(request); newShareGroup.setInviteCode(inviteCode); // 생성된 공유 그룹 저장 @@ -88,33 +88,15 @@ public ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member membe } @Override - public ShareGroupResponse.ShareGroupInfoList getMyShareGroupList(Member member, Pageable pageable) { - + public Page getMyShareGroupList(Member member, Pageable pageable) { // 멤버를 통해 profile을 가져와서, 해당 profile의 shareGroupId를 추출 - List shareGroupIds = profileRepository.findByMember(member).stream() + List shareGroupIdList = profileRepository.findByMemberId(member.getId()) + .stream() .map(profile -> profile.getShareGroup().getId()) - .distinct() // 중복된 공유 그룹 ID 제거 .collect(Collectors.toList()); // 리스트로 수집 // 추출한 공유 그룹 ID 리스트를 통해 해당 공유 그룹들을 페이징 처리하여 가져오기 - Page shareGroupPage = shareGroupRepository.findByIdIn(shareGroupIds, pageable); - - // 각 공유 그룹에 대한 상세 정보를 가져오기 (DetailInfo response 재사용) - List shareGroupDetailInfoList = shareGroupPage.getContent().stream() - .map(shareGroup -> { - List profiles = findProfileList(shareGroup.getId()); // profile 리스트 가져오기 - return ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profiles); // 공유그룹 DetailInfo 반환 - }) - .collect(Collectors.toList()); - - // 최종적으로 공유 그룹 목록 정보를 DTO로 변환하여 반환 - return ShareGroupResponse.ShareGroupInfoList.builder() - .shareGroupDetailInfoList(shareGroupDetailInfoList) // 만든 info 리스트 - .page(shareGroupPage.getNumber()) - .totalElements(shareGroupPage.getTotalElements()) - .isFirst(shareGroupPage.isFirst()) - .isLast(shareGroupPage.isLast()) - .build(); + return shareGroupRepository.findByIdIn(shareGroupIdList, pageable); } @Override