Skip to content

Commit 4b8bdc6

Browse files
authored
모임 참여 카운트와 정렬 순서 개선
모임 참여 카운트와 정렬 순서 개선
2 parents dc20f5a + 611e3a1 commit 4b8bdc6

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2AttendanceService.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import team.wego.wegobackend.group.v2.domain.repository.GroupUserV2QueryRepository;
3131
import team.wego.wegobackend.group.v2.domain.repository.GroupUserV2Repository;
3232
import team.wego.wegobackend.group.v2.domain.repository.GroupV2Repository;
33+
import team.wego.wegobackend.user.domain.User;
3334
import team.wego.wegobackend.user.repository.UserRepository;
3435

3536
@Slf4j
@@ -51,9 +52,8 @@ public class GroupV2AttendanceService {
5152
@Transactional
5253
public AttendanceGroupV2Response attend(Long userId, Long groupId, String message) {
5354
// 회원 체크
54-
if (userId == null) {
55-
throw new GroupException(GroupErrorCode.USER_ID_NULL);
56-
}
55+
User member = userRepository.findById(userId)
56+
.orElseThrow(() -> new GroupException(GroupErrorCode.USER_ID_NULL));
5757

5858
// 모임 체크: for update로 가져오기
5959
GroupV2 group = groupV2Repository.findByIdForUpdate(groupId)
@@ -119,7 +119,8 @@ public AttendanceGroupV2Response attend(Long userId, Long groupId, String messag
119119
}
120120

121121
// FULL 자동 전환
122-
long newCount = groupUserV2Repository.countByGroupIdAndStatus(groupId, GroupUserV2Status.ATTEND);
122+
long newCount = groupUserV2Repository.countByGroupIdAndStatus(groupId,
123+
GroupUserV2Status.ATTEND);
123124

124125
if (newCount == group.getMaxParticipants()
125126
&& group.getStatus() == GroupV2Status.RECRUITING) {
@@ -132,6 +133,8 @@ public AttendanceGroupV2Response attend(Long userId, Long groupId, String messag
132133
eventPublisher.publishEvent(
133134
new GroupJoinedEvent(groupId, group.getHost().getId(), userId));
134135

136+
member.increaseGroupJoinedCount();
137+
135138
return AttendanceGroupV2Response.of(group, newCount, membership);
136139
}
137140

@@ -298,6 +301,11 @@ public GroupUserV2StatusResponse approve(Long approverUserId, Long groupId,
298301
group.changeStatus(GroupV2Status.FULL);
299302
}
300303

304+
User member = userRepository.findById(targetUserId)
305+
.orElseThrow(() -> new GroupException(GroupErrorCode.USER_ID_NULL, targetUserId));
306+
307+
member.increaseGroupJoinedCount();
308+
301309
eventPublisher.publishEvent(
302310
new GroupJoinApprovedEvent(groupId, approverUserId, targetUserId));
303311

src/main/java/team/wego/wegobackend/group/v2/application/service/GroupV2Service.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,14 @@ public CreateGroupV2Response create(Long userId, CreateGroupV2Request request) {
202202

203203
groupCreateCooldownService.acquireOrThrowWithRollbackRelease(userId, COOL_DOWN_SECONDS);
204204

205-
// 회원 조회
205+
// 회원 조회 후 카운트
206206
User host = userRepository.findById(userId)
207207
.orElseThrow(() -> new GroupException(GroupErrorCode.HOST_USER_NOT_FOUND, userId)
208208
);
209209

210+
host.increaseGroupCreatedCount();
211+
host.increaseGroupJoinedCount();
212+
210213
// 모임 주소 생성
211214
GroupV2Address address = GroupV2Address.of(request.location(), request.locationDetail());
212215

@@ -283,9 +286,15 @@ public GetGroupV2Response getGroup(Long userId, Long groupId) {
283286
.orElseThrow(
284287
() -> new GroupException(GroupErrorCode.GROUP_NOT_FOUND_BY_ID, groupId));
285288

289+
// HOST 여부에 따라 응답 결과를 분기 처리하자.
290+
boolean isHost = group.getHost().getId().equals(userId);
291+
292+
List<GroupUserV2> users = isHost
293+
? groupUserV2Repository.findByGroupIdOrderByJoinedAtAscWithUser(groupId)
294+
: groupUserV2Repository.findAttendByGroupIdOrderByJoinedAtAscWithUser(groupId);
295+
286296
// 컬렉션은 안전하게 따로 보관해서 옮겨야 좋다고 한다.
287297
List<GroupImageV2> images = groupImageV2Repository.findAllByGroupIdWithVariants(groupId);
288-
List<GroupUserV2> users = groupUserV2Repository.findAllByGroupIdWithUser(groupId);
289298

290299
Long chatRoomId = group.getChatRoom() != null ? group.getChatRoom().getId() : null;
291300

src/main/java/team/wego/wegobackend/group/v2/domain/repository/GroupUserV2Repository.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,24 @@ List<Long> findUserIdsByGroupIdAndStatuses(
6464
@Modifying(clearAutomatically = true, flushAutomatically = true)
6565
@Query("delete from GroupUserV2 gu where gu.user.id = :userId")
6666
void deleteByUserId(@Param("userId") Long userId);
67+
68+
@Query("""
69+
select gu
70+
from GroupUserV2 gu
71+
join fetch gu.user u
72+
where gu.group.id = :groupId
73+
order by gu.joinedAt asc
74+
""")
75+
List<GroupUserV2> findByGroupIdOrderByJoinedAtAscWithUser(@Param("groupId") Long groupId);
76+
77+
@Query("""
78+
select gu
79+
from GroupUserV2 gu
80+
join fetch gu.user u
81+
where gu.group.id = :groupId
82+
and gu.status = 'ATTEND'
83+
order by gu.joinedAt asc
84+
""")
85+
List<GroupUserV2> findAttendByGroupIdOrderByJoinedAtAscWithUser(@Param("groupId") Long groupId);
86+
6787
}

0 commit comments

Comments
 (0)