Skip to content

Commit

Permalink
Merge pull request #22 from Princess-in-silvertown/feat/15
Browse files Browse the repository at this point in the history
Feat: 그룹 내 전체 유저 조회
  • Loading branch information
woosung1223 authored Jul 12, 2024
2 parents f03a297 + 8405587 commit 8c7a4b9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
48 changes: 33 additions & 15 deletions src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
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());
}

@Transactional
public List<GroupMemberResponse> 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());
}
}
10 changes: 10 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/domain/MemberRepository.java
Original file line number Diff line number Diff line change
@@ -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<Member, Long> {
List<Member> findByGroupId(Long groupId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<GroupMemberResponse> getRegisteredMembers(@PathVariable long groupId) {
return groupService.getRegisteredMembers(groupId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package slvtwn.khu.toyouserver.presentation;

public record GroupMemberResponse(Long id, Long userId, String name, String profilePicture) {
}

0 comments on commit 8c7a4b9

Please sign in to comment.