diff --git a/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2AttendanceService.java b/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2AttendanceService.java index bf632fe..66616c5 100644 --- a/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2AttendanceService.java +++ b/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2AttendanceService.java @@ -30,6 +30,7 @@ import team.wego.wegobackend.group.v2.domain.repository.GroupUserV2QueryRepository; import team.wego.wegobackend.group.v2.domain.repository.GroupUserV2Repository; import team.wego.wegobackend.group.v2.domain.repository.GroupV2Repository; +import team.wego.wegobackend.user.domain.User; import team.wego.wegobackend.user.repository.UserRepository; @Slf4j @@ -51,9 +52,8 @@ public class GroupV2AttendanceService { @Transactional public AttendanceGroupV2Response attend(Long userId, Long groupId, String message) { // 회원 체크 - if (userId == null) { - throw new GroupException(GroupErrorCode.USER_ID_NULL); - } + User member = userRepository.findById(userId) + .orElseThrow(() -> new GroupException(GroupErrorCode.USER_ID_NULL)); // 모임 체크: for update로 가져오기 GroupV2 group = groupV2Repository.findByIdForUpdate(groupId) @@ -119,7 +119,8 @@ public AttendanceGroupV2Response attend(Long userId, Long groupId, String messag } // FULL 자동 전환 - long newCount = groupUserV2Repository.countByGroupIdAndStatus(groupId, GroupUserV2Status.ATTEND); + long newCount = groupUserV2Repository.countByGroupIdAndStatus(groupId, + GroupUserV2Status.ATTEND); if (newCount == group.getMaxParticipants() && group.getStatus() == GroupV2Status.RECRUITING) { @@ -132,6 +133,8 @@ public AttendanceGroupV2Response attend(Long userId, Long groupId, String messag eventPublisher.publishEvent( new GroupJoinedEvent(groupId, group.getHost().getId(), userId)); + member.increaseGroupJoinedCount(); + return AttendanceGroupV2Response.of(group, newCount, membership); } @@ -298,6 +301,11 @@ public GroupUserV2StatusResponse approve(Long approverUserId, Long groupId, group.changeStatus(GroupV2Status.FULL); } + User member = userRepository.findById(targetUserId) + .orElseThrow(() -> new GroupException(GroupErrorCode.USER_ID_NULL, targetUserId)); + + member.increaseGroupJoinedCount(); + eventPublisher.publishEvent( new GroupJoinApprovedEvent(groupId, approverUserId, targetUserId)); diff --git a/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2Service.java b/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2Service.java index d075ae9..922ce26 100644 --- a/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2Service.java +++ b/src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2Service.java @@ -202,11 +202,14 @@ public CreateGroupV2Response create(Long userId, CreateGroupV2Request request) { groupCreateCooldownService.acquireOrThrowWithRollbackRelease(userId, COOL_DOWN_SECONDS); - // 회원 조회 + // 회원 조회 후 카운트 User host = userRepository.findById(userId) .orElseThrow(() -> new GroupException(GroupErrorCode.HOST_USER_NOT_FOUND, userId) ); + host.increaseGroupCreatedCount(); + host.increaseGroupJoinedCount(); + // 모임 주소 생성 GroupV2Address address = GroupV2Address.of(request.location(), request.locationDetail()); @@ -283,9 +286,15 @@ public GetGroupV2Response getGroup(Long userId, Long groupId) { .orElseThrow( () -> new GroupException(GroupErrorCode.GROUP_NOT_FOUND_BY_ID, groupId)); + // HOST 여부에 따라 응답 결과를 분기 처리하자. + boolean isHost = group.getHost().getId().equals(userId); + + List users = isHost + ? groupUserV2Repository.findByGroupIdOrderByJoinedAtAscWithUser(groupId) + : groupUserV2Repository.findAttendByGroupIdOrderByJoinedAtAscWithUser(groupId); + // 컬렉션은 안전하게 따로 보관해서 옮겨야 좋다고 한다. List images = groupImageV2Repository.findAllByGroupIdWithVariants(groupId); - List users = groupUserV2Repository.findAllByGroupIdWithUser(groupId); Long chatRoomId = group.getChatRoom() != null ? group.getChatRoom().getId() : null; diff --git a/src/main/java/team/wego/wegobackend/group/v2/domain/repository/GroupUserV2Repository.java b/src/main/java/team/wego/wegobackend/group/v2/domain/repository/GroupUserV2Repository.java index c0503a3..a52b413 100644 --- a/src/main/java/team/wego/wegobackend/group/v2/domain/repository/GroupUserV2Repository.java +++ b/src/main/java/team/wego/wegobackend/group/v2/domain/repository/GroupUserV2Repository.java @@ -64,4 +64,24 @@ List findUserIdsByGroupIdAndStatuses( @Modifying(clearAutomatically = true, flushAutomatically = true) @Query("delete from GroupUserV2 gu where gu.user.id = :userId") void deleteByUserId(@Param("userId") Long userId); + + @Query(""" + select gu + from GroupUserV2 gu + join fetch gu.user u + where gu.group.id = :groupId + order by gu.joinedAt asc + """) + List findByGroupIdOrderByJoinedAtAscWithUser(@Param("groupId") Long groupId); + + @Query(""" + select gu + from GroupUserV2 gu + join fetch gu.user u + where gu.group.id = :groupId + and gu.status = 'ATTEND' + order by gu.joinedAt asc + """) + List findAttendByGroupIdOrderByJoinedAtAscWithUser(@Param("groupId") Long groupId); + }