diff --git a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java index c886d5c..880e81e 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java @@ -1,45 +1,61 @@ 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.Member; import slvtwn.khu.toyouserver.domain.MemberRepository; +import slvtwn.khu.toyouserver.domain.User; +import slvtwn.khu.toyouserver.dto.GroupMemberResponse; import slvtwn.khu.toyouserver.dto.GroupResponse; +import slvtwn.khu.toyouserver.exception.ToyouException; import slvtwn.khu.toyouserver.persistance.GroupRepository; +import slvtwn.khu.toyouserver.persistance.UserRepository; @Service public class GroupService { - private final GroupRepository groupRepository; - private final MemberRepository memberRepository; - private final MemberService memberService; - - public GroupService(GroupRepository groupRepository, - MemberRepository memberRepository, MemberService memberService) { - this.groupRepository = groupRepository; - this.memberRepository = memberRepository; - this.memberService = memberService; - } - - @Transactional - public GroupResponse registerUser(long groupId, long userId) { - Group savedGroup = memberService.registerMember(groupId, userId).getGroup(); - return new GroupResponse(savedGroup.getId(), savedGroup.getName()); - } - - @Transactional - public GroupResponse create(String name) { - Group group = new Group(name); - Group savedGroup = groupRepository.save(group); - return new GroupResponse(savedGroup.getId(), savedGroup.getName()); - } - - @Transactional(readOnly = true) - public List findRegisteredGroups(long userId) { - List groups = memberRepository.findGroupsByUserId(userId); - return groups.stream() - .map(group -> new GroupResponse(group.getId(), group.getName())) - .toList(); - } + private final GroupRepository groupRepository; + private final MemberRepository memberRepository; + private final UserRepository userRepository; + + public GroupService(GroupRepository groupRepository, MemberRepository memberRepository, UserRepository userRepository) { + this.groupRepository = groupRepository; + this.memberRepository = memberRepository; + this.userRepository = userRepository; + } + + @Transactional + public void registerMember(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)); + + memberRepository.save(new Member(user, group)); + } + + public List findMembers(long groupId) { + return memberRepository.findByGroupId(groupId).stream() + .map(GroupMemberResponse::of) + .toList(); + } + + @Transactional + public GroupResponse createGroup(String name) { + Group group = new Group(name); + Group savedGroup = groupRepository.save(group); + return new GroupResponse(savedGroup.getId(), savedGroup.getName()); + } + + @Transactional(readOnly = true) + public List findRegisteredGroups(long userId) { + List groups = memberRepository.findGroupsByUserId(userId); + return groups.stream() + .map(group -> new GroupResponse(group.getId(), group.getName())) + .toList(); + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/User.java b/src/main/java/slvtwn/khu/toyouserver/domain/User.java index 3d31c04..ccd8d3e 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/User.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/User.java @@ -6,7 +6,6 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.time.LocalDate; -import java.time.LocalDateTime; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -18,19 +17,22 @@ @Getter public class User extends BaseTimeEntity { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - private String name; + private String name; - private LocalDate birthday; + private LocalDate birthday; - private String profilePicture; + private String introduction; - public User(String name, LocalDate birthday, String profilePicture) { - this.name = name; - this.birthday = birthday; - this.profilePicture = profilePicture; - } + private String profilePicture; + + public User(String name, LocalDate birthday, String introduction, String profilePicture) { + this.name = name; + this.birthday = birthday; + this.introduction = introduction; + this.profilePicture = profilePicture; + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/GroupMemberResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/GroupMemberResponse.java new file mode 100644 index 0000000..6e9b925 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/GroupMemberResponse.java @@ -0,0 +1,14 @@ +package slvtwn.khu.toyouserver.dto; + +import java.time.LocalDate; +import slvtwn.khu.toyouserver.domain.Member; +import slvtwn.khu.toyouserver.domain.User; + +public record GroupMemberResponse(Long id, String name, LocalDate birthday, String introduction, String imageUrl) { + + public static GroupMemberResponse of(Member member) { + User user = member.getUser(); + return new GroupMemberResponse(member.getId(), user.getName(), user.getBirthday(), + user.getIntroduction(), user.getProfilePicture()); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java index 7505f91..2327c90 100644 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java @@ -6,11 +6,13 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import slvtwn.khu.toyouserver.application.GroupService; import slvtwn.khu.toyouserver.common.UserAuthentication; import slvtwn.khu.toyouserver.domain.User; import slvtwn.khu.toyouserver.dto.GroupCreateRequest; +import slvtwn.khu.toyouserver.dto.GroupMemberResponse; import slvtwn.khu.toyouserver.dto.GroupResponse; @RequiredArgsConstructor @@ -21,16 +23,21 @@ public class GroupController { @PostMapping("/groups") public GroupResponse createGroup(@RequestBody GroupCreateRequest request) { - return groupService.create(request.name()); + return groupService.createGroup(request.name()); + } + + @GetMapping("/groups") + public List findRegisteredGroups(@RequestParam Long memberId) { + return groupService.findRegisteredGroups(memberId); } @PostMapping("/groups/{groupId}/members") - public GroupResponse registerMember(@PathVariable long groupId) { - return groupService.registerUser(groupId, 1L); // TODO: user -> argumentResolver 등록 필요 + public void registerMember(@PathVariable Long groupId) { + groupService.registerMember(groupId, 1L); // TODO: user -> argumentResolver 등록 필요 } - @GetMapping("/groups") - public List findRegisteredGroups(@UserAuthentication User user) { - return groupService.findRegisteredGroups(user.getId()); + @GetMapping("/groups/{groupId}/members") + public List findMembers(@PathVariable Long groupId) { + return groupService.findMembers(groupId); } } diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java deleted file mode 100644 index ec35046..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupMemberResponse.java +++ /dev/null @@ -1,4 +0,0 @@ -package slvtwn.khu.toyouserver.presentation; - -public record GroupMemberResponse(Long id, Long userId, String name, String profilePicture) { -} diff --git a/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java index 020e92f..067ce89 100644 --- a/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java +++ b/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java @@ -17,57 +17,58 @@ @SpringBootTest class GroupServiceTest { - @Autowired - GroupRepository groupRepository; + @Autowired + GroupRepository groupRepository; - @Autowired - UserRepository userRepository; + @Autowired + UserRepository userRepository; - @Autowired - GroupService groupService; + @Autowired + GroupService groupService; - @DisplayName("그룹은 이름을 필수로 가진다.") - @Test - void 그룹은_이름으로_생성된다() { - String groupName = "Name Created Group"; - Group group = new Group(groupName); - groupRepository.save(group); + @DisplayName("그룹은 이름을 필수로 가진다.") + @Test + void 그룹은_이름으로_생성된다() { + String groupName = "Name Created Group"; + Group group = new Group(groupName); + groupRepository.save(group); - GroupResponse groupResponse = groupService.create(groupName); - assertThat(groupResponse.name()).isEqualTo(groupName); - } + GroupResponse groupResponse = groupService.createGroup(groupName); + assertThat(groupResponse.name()).isEqualTo(groupName); + } - @DisplayName("사용자는 그룹에 가입한다.") - @Test - void 사용자는_그룹에_가입한다() { - Group group = new Group("Group"); - groupRepository.save(group); - User user = new User("Hart", LocalDate.now(), "www.profile-picture.com"); - userRepository.save(user); + // TODO: 테스트 수정 (registerMember인데 groupResponse를 반환하도록 잘못 설계되어 있었음) +// @DisplayName("사용자는 그룹에 가입한다.") +// @Test +// void 사용자는_그룹에_가입한다() { +// Group group = new Group("Group"); +// groupRepository.save(group); +// User user = new User("Hart", LocalDate.now(), "introduction", "www.profile-picture.com"); +// userRepository.save(user); +// +// groupService.registerMember(group.getId(), 1L); +// +// assertThat(groupResponse.id()).isEqualTo(group.getId()); +// assertThat(groupResponse.name()).isEqualTo(group.getName()); +// } - GroupResponse groupResponse = groupService.registerUser(group.getId(), 1L); + @DisplayName("사용자는 가입한 그룹을 조회한다.") + @Test + void 사용자별_그룹을_조회한다() { + Group group1 = new Group("Group-1"), group2 = new Group("Group-2"); + groupRepository.save(group1); + groupRepository.save(group2); - assertThat(groupResponse.id()).isEqualTo(group.getId()); - assertThat(groupResponse.name()).isEqualTo(group.getName()); - } + User user = new User("Hart", LocalDate.now(), "introduction", "www.profile-picture.com"); + userRepository.save(user); - @DisplayName("사용자는 가입한 그룹을 조회한다.") - @Test - void 사용자별_그룹을_조회한다() { - Group group1 = new Group("Group-1"), group2 = new Group("Group-2"); - groupRepository.save(group1); - groupRepository.save(group2); - - User user = new User("Hart", LocalDate.now(), "www.profile-picture.com"); - userRepository.save(user); + groupService.registerMember(group1.getId(), user.getId()); + groupService.registerMember(group2.getId(), user.getId()); - groupService.registerUser(group1.getId(), user.getId()); - groupService.registerUser(group2.getId(), user.getId()); + List groups = groupService.findRegisteredGroups(user.getId()); - List groups = groupService.findRegisteredGroups(user.getId()); - - assertThat(groups).hasSize(2); - assertThat(groups.get(0).name()).isEqualTo(group1.getName()); - assertThat(groups.get(1).name()).isEqualTo(group2.getName()); - } -} \ No newline at end of file + assertThat(groups).hasSize(2); + assertThat(groups.get(0).name()).isEqualTo(group1.getName()); + assertThat(groups.get(1).name()).isEqualTo(group2.getName()); + } +} diff --git a/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java index 9dee558..e3ec23a 100644 --- a/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java +++ b/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java @@ -27,7 +27,7 @@ class UserServiceTest { @Test void 유저를_조회할_수_있다() { // given - User user = new User("teo", LocalDate.now(), "www.profile-picture.com"); + User user = new User("teo", LocalDate.now(), "introduction", "www.profile-picture.com"); userRepository.save(user); // when