From 3712203bf0c74ab509e467ca9afd4a125d9a9331 Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Fri, 12 Jul 2024 15:03:32 +0900 Subject: [PATCH 1/3] =?UTF-8?q?:sparkles:=20feat:=20=EA=B7=B8=EB=A3=B9=20?= =?UTF-8?q?=EC=86=8C=EC=86=8D=20=EB=A9=A4=EB=B2=84=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?Presentation=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/GroupController.java | 17 ++++++++++++----- .../presentation/GroupMemberResponse.java | 4 ++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java index 17c195f..cbb6e6a 100644 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java @@ -1,6 +1,8 @@ package slvtwn.khu.toyouserver.presentation; +import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @@ -11,10 +13,15 @@ @RestController public class GroupController { - private final GroupService groupService; + private final GroupService groupService; - @PostMapping("/groups/{groupId}/members") - public GroupResponse registerMember(@PathVariable long groupId) { - return groupService.registerUser(groupId, 1L); // TODO: user -> argumentResolver 등록 필요 - } + @PostMapping("/groups/{groupId}/members") + public GroupResponse registerMember(@PathVariable long groupId) { + return groupService.registerUser(groupId, 1L); // TODO: user -> argumentResolver 등록 필요 + } + + @GetMapping("/groups/{groupId}/members") + public List getRegisteredMembers(@PathVariable long groupId) { + return groupService.getRegisteredMembers(groupId); + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java new file mode 100644 index 0000000..ec35046 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java @@ -0,0 +1,4 @@ +package slvtwn.khu.toyouserver.presentation; + +public record GroupMemberResponse(Long id, Long userId, String name, String profilePicture) { +} From e0d5c6da5f7678165364b1f7b6d30d75bd0ed63a Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Fri, 12 Jul 2024 15:03:43 +0900 Subject: [PATCH 2/3] =?UTF-8?q?:sparkles:=20feat:=20=EA=B7=B8=EB=A3=B9=20?= =?UTF-8?q?=EC=86=8C=EC=86=8D=20=EB=A9=A4=EB=B2=84=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?Application=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toyouserver/application/GroupService.java | 47 +++++++++++++------ .../toyouserver/domain/MemberRepository.java | 10 ++++ 2 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 src/main/java/slvtwn/khu/toyouserver/domain/MemberRepository.java diff --git a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java index ae06406..0fda25f 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java @@ -1,36 +1,53 @@ package slvtwn.khu.toyouserver.application; +import java.util.List; +import java.util.stream.Collectors; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.common.ErrorType; import slvtwn.khu.toyouserver.domain.Group; +import slvtwn.khu.toyouserver.domain.MemberRepository; import slvtwn.khu.toyouserver.domain.User; import slvtwn.khu.toyouserver.dto.GroupResponse; import slvtwn.khu.toyouserver.exception.ToyouException; import slvtwn.khu.toyouserver.persistance.GroupRepository; import slvtwn.khu.toyouserver.persistance.UserRepository; +import slvtwn.khu.toyouserver.presentation.GroupMemberResponse; @Service @Transactional(readOnly = true) public class GroupService { - private final GroupRepository groupRepository; - private final UserRepository userRepository; + private final GroupRepository groupRepository; + private final UserRepository userRepository; + private final MemberRepository memberRepository; - public GroupService(GroupRepository groupRepository, UserRepository userRepository) { - this.groupRepository = groupRepository; - this.userRepository = userRepository; - } + public GroupService(GroupRepository groupRepository, UserRepository userRepository, + MemberRepository memberRepository) { + this.groupRepository = groupRepository; + this.userRepository = userRepository; + this.memberRepository = memberRepository; + } - @Transactional - public GroupResponse registerUser(long groupId, long userId) { - Group group = groupRepository.findById(groupId) - .orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND)); - User user = userRepository.findById(userId) - .orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND)); + @Transactional + public GroupResponse registerUser(long groupId, long userId) { + Group group = groupRepository.findById(groupId) + .orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND)); + User user = userRepository.findById(userId) + .orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND)); - // TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다. + // TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다. // group.addMember(user); - return new GroupResponse(group.getId(), group.getName()); - } + return new GroupResponse(group.getId(), group.getName()); + } + + public List getRegisteredMembers(long groupId) { + return memberRepository.findByGroupId(groupId).stream() + .map(member -> new GroupMemberResponse( + member.getId(), + member.getUser().getId(), + member.getUser().getName(), + member.getUser().getProfilePicture())) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/MemberRepository.java b/src/main/java/slvtwn/khu/toyouserver/domain/MemberRepository.java new file mode 100644 index 0000000..0bf3e99 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/domain/MemberRepository.java @@ -0,0 +1,10 @@ +package slvtwn.khu.toyouserver.domain; + +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MemberRepository extends JpaRepository { + List findByGroupId(Long groupId); +} From 84055873137b79d5595f381f4cd9be7a06db52b2 Mon Sep 17 00:00:00 2001 From: Hyoseop Song Date: Fri, 12 Jul 2024 15:26:54 +0900 Subject: [PATCH 3/3] =?UTF-8?q?:sparkles:=20feat:=20=EC=A1=B0=ED=9A=8C=20A?= =?UTF-8?q?PI=20Transactional=20=EB=B6=80=EC=97=AC=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/slvtwn/khu/toyouserver/application/GroupService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java index 0fda25f..6a64b66 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java @@ -41,6 +41,7 @@ public GroupResponse registerUser(long groupId, long userId) { return new GroupResponse(group.getId(), group.getName()); } + @Transactional public List getRegisteredMembers(long groupId) { return memberRepository.findByGroupId(groupId).stream() .map(member -> new GroupMemberResponse(