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: 그룹 내 전체 유저 조회 #22

Merged
merged 3 commits into from
Jul 12, 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
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 : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석은 공공의 적입니다 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 브랜치에서 개발 안하던 부분이라 너무 거슬렸었는데 제가 써둔 거였네요 🤦🏻 추가 수정하겠습니다

// group.addMember(user);
return new GroupResponse(group.getId(), group.getName());
}
return new GroupResponse(group.getId(), group.getName());
}

@Transactional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly 클래스 레벨에서 걸려있으니 Transactional 빼도 됩니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#24 처럼 Group 생성하는 기능은 조회하는 메소드가 아니지만 GroupService에서 작성해두어야 할 것 같아요.
service 레이어를 두는 쪽으로 개발하되 조회 여부로 분리하거나(DomainQueryService-readOnly = true, DomainCommandService), 하나의 DomainService에서 method별 요청을 트랜잭션으로 부여할 수 있을 것 같은데.. 어떠신지요 ? 🌵

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) {
}