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: 그룹 소속 유저 조회 기능 #28

Merged
merged 6 commits into from
Jul 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
29 changes: 15 additions & 14 deletions src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package slvtwn.khu.toyouserver.application;

import java.util.List;
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;

@Service
@Transactional(readOnly = true)
public class GroupService {

private final GroupRepository groupRepository;
private final UserRepository userRepository;
private final MemberRepository memberRepository;
private final MemberService memberService;

public GroupService(GroupRepository groupRepository, UserRepository userRepository,
MemberRepository memberRepository) {
public GroupService(GroupRepository groupRepository,
MemberRepository memberRepository, MemberService memberService) {
this.groupRepository = groupRepository;
this.userRepository = userRepository;
this.memberRepository = memberRepository;
this.memberService = memberService;
}

@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));
return new GroupResponse(group.getId(), group.getName());
Group savedGroup = memberService.registerMember(groupId, userId).getGroup();
return new GroupResponse(savedGroup.getId(), savedGroup.getName());
}

@Transactional
Expand All @@ -41,4 +34,12 @@ public GroupResponse create(String 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package slvtwn.khu.toyouserver.application;

import org.springframework.stereotype.Service;
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.exception.ToyouException;
import slvtwn.khu.toyouserver.persistance.GroupRepository;
import slvtwn.khu.toyouserver.persistance.UserRepository;

@Service
public class MemberService {

private final UserRepository userRepository;
private final MemberRepository memberRepository;
private final GroupRepository groupRepository;


public MemberService(GroupRepository groupRepository, UserRepository userRepository,
MemberRepository memberRepository) {
this.groupRepository = groupRepository;
this.userRepository = userRepository;
this.memberRepository = memberRepository;
}

public Member 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));
return memberRepository.save(new Member(user, group));
}
}
29 changes: 17 additions & 12 deletions src/main/java/slvtwn/khu/toyouserver/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@
@Getter
public class Member extends BaseTimeEntity {

@Id
@GeneratedValue
private Long id;
@Id
@GeneratedValue
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_id")
private Group group;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_id")
private Group group;

public Member(Group group) {
this.group = group;
}
public Member(Group group) {
this.group = group;
}

public Member(User user, Group group) {
this.user = user;
this.group = group;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
List<Member> findByGroupId(Long groupId);

@Query("SELECT m.group FROM Member m WHERE m.user.id = :userId")
List<Group> findGroupsByUserId(Long userId);
}
4 changes: 4 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/domain/SessionUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

@Entity
@Table(name = "session_users")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class SessionUser extends BaseTimeEntity {
Expand All @@ -20,6 +22,8 @@ public class SessionUser extends BaseTimeEntity {
@Column(name = "user_id", nullable = false)
private Long userId;

// TODO ; 카드

public SessionUser(String sessionId, Long userId) {
this.sessionId = sessionId;
this.userId = userId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.web.bind.annotation.RequestBody;
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.GroupResponse;

Expand All @@ -17,13 +19,18 @@ public class GroupController {

private final GroupService groupService;

@PostMapping("/groups/create")
@PostMapping("/groups")
public GroupResponse createGroup(@RequestBody GroupCreateRequest request) {
return groupService.create(request.name());
}

@PostMapping("/groups/{groupId}/members")
public GroupResponse registerMember(@PathVariable long groupId) {
return groupService.registerUser(groupId, 1L); // TODO: user -> argumentResolver 등록 필요
}

@GetMapping("/groups")
public List<GroupResponse> findRegisteredGroups(@UserAuthentication User user) {
return groupService.findRegisteredGroups(user.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package slvtwn.khu.toyouserver.application;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.LocalDate;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import slvtwn.khu.toyouserver.domain.Group;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.dto.GroupResponse;
import slvtwn.khu.toyouserver.persistance.GroupRepository;
import slvtwn.khu.toyouserver.persistance.UserRepository;

@SpringBootTest
class GroupServiceTest {

@Autowired
GroupRepository groupRepository;

@Autowired
UserRepository userRepository;

@Autowired
GroupService groupService;

@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);
}

@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);

GroupResponse groupResponse = groupService.registerUser(group.getId(), 1L);

assertThat(groupResponse.id()).isEqualTo(group.getId());
assertThat(groupResponse.name()).isEqualTo(group.getName());
}

@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.registerUser(group1.getId(), user.getId());
groupService.registerUser(group2.getId(), 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());
}
}