Skip to content

Commit

Permalink
fix: 그룹 스코어 갱신 이벤트 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 3, 2024
1 parent 963e9be commit 3c0fc33
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hero.alignlab.domain.group.application

import com.hero.alignlab.common.extension.coExecuteOrNull
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.group.domain.GroupUser
import com.hero.alignlab.domain.group.domain.GroupUserScore
import com.hero.alignlab.domain.group.infrastructure.GroupUserScoreRepository
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -57,4 +58,25 @@ class GroupUserScoreService(
groupUserScoreRepository.deleteAllInBatch()
}
}

suspend fun createOrUpdateGroupUserScore(groupUser: GroupUser, score: Int) {
val groupUserScore = findByUidOrNull(groupUser.uid)

val createOrUpdateGroupUserScore = when (groupUserScore == null) {
true -> GroupUserScore(
groupId = groupUser.groupId,
groupUserId = groupUser.id,
uid = groupUser.uid,
score = score
)

false -> groupUserScore.apply {
this.score = score
}
}

txTemplates.writer.coExecuteOrNull {
saveSync(createOrUpdateGroupUserScore)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class GroupUserService(
groupUserRepository.deleteById(groupUserId)
}

suspend fun findByUid(uid: Long): GroupUser? {
suspend fun findByUidOrNull(uid: Long): GroupUser? {
return withContext(Dispatchers.IO) {
groupUserRepository.findByUid(uid)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyGroupFacade(
) {
// TODO : 기획 확인후, 코드 변경
suspend fun getMyGroup(user: AuthUser): MyGroupResponse? {
val groupUser = groupUserService.findByUid(user.uid) ?: return null
val groupUser = groupUserService.findByUidOrNull(user.uid) ?: return null
val group = groupService.findByIdOrThrow(groupUser.groupId)
val groupUserCount = groupUserService.countAllByGroupId(groupUser.groupId)
val userInfo = userInfoService.findByIdOrThrow(group.ownerUid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.hero.alignlab.common.extension.coExecuteOrNull
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.group.application.GroupUserScoreService
import com.hero.alignlab.domain.group.application.GroupUserService
import com.hero.alignlab.domain.group.domain.GroupUserScore
import com.hero.alignlab.domain.pose.application.PoseCountService
import com.hero.alignlab.domain.pose.application.PoseKeyPointSnapshotService
import com.hero.alignlab.domain.pose.domain.PoseCount
Expand Down Expand Up @@ -51,37 +50,20 @@ class PoseSnapshotListener(
this.totalCount.count[event.poseSnapshot.type] = typeCount + 1
}

val score = updatedPoseCount.totalCount.count
.filter { (key, _) -> key in BAD_POSE }
.values
.sum()

/** group score 처리 */
val groupUser = groupUserService.findByUid(event.poseSnapshot.uid)
val updatedGroupUserScore = when (groupUser == null) {
true -> null
false -> {
val groupUserScore = groupUserScoreService.findByUidOrNull(event.poseSnapshot.uid)

when (groupUserScore != null) {
true -> groupUserScore.apply {
this.score = score
}

false -> GroupUserScore(
groupId = groupUser.groupId,
groupUserId = groupUser.id,
uid = groupUser.uid,
score = score
)
}
}
}

/** 포즈에 연관된 데이터 처리 */
txTemplates.writer.coExecuteOrNull {
poseKeyPointSnapshotService.saveAllSync(keyPoints)
poseCountService.saveSync(updatedPoseCount)
updatedGroupUserScore?.run { groupUserScoreService.saveSync(this) }
}

/** group score 처리 */
groupUserService.findByUidOrNull(event.poseSnapshot.uid)?.run {
val score = updatedPoseCount.totalCount.count
.filter { (key, _) -> key in BAD_POSE }
.values
.sum()

groupUserScoreService.createOrUpdateGroupUserScore(this, score)
}
}
}
Expand Down

0 comments on commit 3c0fc33

Please sign in to comment.