Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 그룹 내 다른 유저 조회 & 변경된 API에 맞게 명세 수정 #31

Merged
merged 6 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 47 additions & 31 deletions src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
Original file line number Diff line number Diff line change
@@ -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<GroupResponse> findRegisteredGroups(long userId) {
List<Group> 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<GroupMemberResponse> 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<GroupResponse> findRegisteredGroups(long userId) {
List<Group> groups = memberRepository.findGroupsByUserId(userId);
return groups.stream()
.map(group -> new GroupResponse(group.getId(), group.getName()))
.toList();
}
}
26 changes: 14 additions & 12 deletions src/main/java/slvtwn/khu/toyouserver/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/dto/GroupMemberResponse.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<GroupResponse> 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<GroupResponse> findRegisteredGroups(@UserAuthentication User user) {
return groupService.findRegisteredGroups(user.getId());
@GetMapping("/groups/{groupId}/members")
public List<GroupMemberResponse> findMembers(@PathVariable Long groupId) {
return groupService.findMembers(groupId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupResponse> groups = groupService.findRegisteredGroups(user.getId());

List<GroupResponse> 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());
}
}
assertThat(groups).hasSize(2);
assertThat(groups.get(0).name()).isEqualTo(group1.getName());
assertThat(groups.get(1).name()).isEqualTo(group2.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down