Skip to content

Commit

Permalink
fix: 그룹 탈퇴 기능 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 18, 2024
1 parent 822b0d3 commit 1d68234
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ class GroupFacade(
}

suspend fun withdraw(user: AuthUser, groupId: Long) {
withdraw(groupId, user.uid)
withdraw(user.uid, groupId)
}

suspend fun withdraw(groupId: Long, uid: Long) {
suspend fun withdraw(uid: Long, groupId: Long) {
val group = groupService.findByIdOrThrow(groupId)

/** 그룹 승계 또는 제거 */
if (group.ownerUid == uid) {
withdrawGroupOwner(group)
withdrawGroupOwner(uid, group)
}

/** 그룹원 제거 */
withdrawGroupUser(group, uid)
}

private suspend fun withdrawGroupOwner(group: Group) {
val groupUser = groupUserService.findTop1ByGroupIdOrderByCreatedAtAsc(group.id)
private suspend fun withdrawGroupOwner(uid: Long, group: Group) {
val groupUser = groupUserService.findTop1ByGroupIdAndUidNotOrderByCreatedAtAsc(group.id, uid)

txTemplates.writer.executesOrNull {
when (groupUser == null) {
Expand All @@ -94,12 +94,13 @@ class GroupFacade(
groupService.saveSync(succeedGroup)
}
}
groupUserService.deleteByGroupIdAndUidSync(group.id, uid)
}
}

private suspend fun withdrawGroupUser(group: Group, uid: Long) {
txTemplates.writer.executesOrNull {
groupUserService.deleteBySync(group.id, uid)
groupUserService.deleteByGroupIdAndUidSync(group.id, uid)
groupService.saveSync(group.apply { this.userCount -= 1 })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class GroupUserService(
return groupUserRepository.findAllByGroupIdAndUidIn(groupId, uids)
}

suspend fun findTop1ByGroupIdOrderByCreatedAtAsc(groupId: Long): GroupUser? {
suspend fun findTop1ByGroupIdAndUidNotOrderByCreatedAtAsc(groupId: Long, uid: Long): GroupUser? {
return withContext(Dispatchers.IO) {
groupUserRepository.findTop1ByGroupIdOrderByCreatedAtAsc(groupId)
groupUserRepository.findTop1ByGroupIdAndUidNotOrderByCreatedAtAsc(groupId, uid)
}
}

@Transactional
fun deleteBySync(groupId: Long, uid: Long) {
fun deleteByGroupIdAndUidSync(groupId: Long, uid: Long) {
groupUserRepository.deleteByGroupIdAndUid(groupId, uid)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface GroupUserRepository : JpaRepository<GroupUser, Long> {

fun findAllByGroupIdAndUidIn(groupId: Long, uids: List<Long>): List<GroupUser>

fun findTop1ByGroupIdOrderByCreatedAtAsc(groupId: Long): GroupUser?
fun findTop1ByGroupIdAndUidNotOrderByCreatedAtAsc(groupId: Long, uid: Long): GroupUser?

fun deleteByGroupIdAndUid(groupId: Long, uid: Long)

Expand Down

0 comments on commit 1d68234

Please sign in to comment.