From 5710787fbda81f55b0e6abdc5eec2344c5aa4445 Mon Sep 17 00:00:00 2001 From: DongGeon0908 Date: Mon, 9 Sep 2024 00:16:03 +0900 Subject: [PATCH] imp: refactor coroutine function --- .../hero/alignlab/domain/auth/application/AuthFacade.kt | 6 +----- .../domain/discussion/application/DiscussionService.kt | 4 ++-- .../alignlab/domain/image/application/ImageService.kt | 4 ++-- .../domain/log/application/SystemActionLogService.kt | 9 +++++++-- .../alignlab/domain/user/application/UserInfoService.kt | 6 ++++++ 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt index 6ea1fe4..d8d048e 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/application/AuthFacade.kt @@ -20,8 +20,6 @@ import com.hero.alignlab.domain.user.model.response.UserInfoResponse import com.hero.alignlab.exception.ErrorCode import com.hero.alignlab.exception.InvalidRequestException import com.hero.alignlab.exception.InvalidTokenException -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import org.springframework.stereotype.Service import reactor.core.publisher.Mono import java.time.LocalDateTime @@ -109,9 +107,7 @@ class AuthFacade( } suspend fun getUserInfo(user: AuthUser): UserInfoResponse { - val userInfo = withContext(Dispatchers.IO) { - userInfoService.getUserByIdOrThrowSync(user.uid) - } + val userInfo = userInfoService.getUserByIdOrThrow(user.uid) return UserInfoResponse.from(userInfo) } diff --git a/src/main/kotlin/com/hero/alignlab/domain/discussion/application/DiscussionService.kt b/src/main/kotlin/com/hero/alignlab/domain/discussion/application/DiscussionService.kt index 49d4caa..596015a 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/discussion/application/DiscussionService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/discussion/application/DiscussionService.kt @@ -1,6 +1,6 @@ package com.hero.alignlab.domain.discussion.application -import com.hero.alignlab.common.extension.executes +import com.hero.alignlab.common.extension.coExecute import com.hero.alignlab.config.database.TransactionTemplates import com.hero.alignlab.domain.auth.model.AuthUser import com.hero.alignlab.domain.discussion.domain.Discussion @@ -22,7 +22,7 @@ class DiscussionService( content = request.content, ) - val createdDiscussion = txTemplates.writer.executes { + val createdDiscussion = txTemplates.writer.coExecute { discussionRepository.save(discussion) } diff --git a/src/main/kotlin/com/hero/alignlab/domain/image/application/ImageService.kt b/src/main/kotlin/com/hero/alignlab/domain/image/application/ImageService.kt index 1909794..b4cc40e 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/image/application/ImageService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/image/application/ImageService.kt @@ -1,7 +1,7 @@ package com.hero.alignlab.domain.image.application import com.hero.alignlab.client.s3.client.S3Client -import com.hero.alignlab.common.extension.executes +import com.hero.alignlab.common.extension.coExecute import com.hero.alignlab.config.database.TransactionTemplates import com.hero.alignlab.domain.auth.model.AuthUser import com.hero.alignlab.domain.image.domain.ImageMetadata @@ -51,7 +51,7 @@ class ImageService( suspend fun uploadImage(uid: Long, type: ImageType, image: FilePart): ImageMetadata { val imageUrl = s3Client.upload(image) - return txTemplates.writer.executes { + return txTemplates.writer.coExecute { imageMetadataRepository.save( ImageMetadata( filename = image.filename(), diff --git a/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt b/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt index 24aa436..b39ab09 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt @@ -1,5 +1,7 @@ package com.hero.alignlab.domain.log.application +import com.hero.alignlab.common.extension.coExecute +import com.hero.alignlab.config.database.TransactionTemplates import com.hero.alignlab.domain.log.domain.SystemActionLog import com.hero.alignlab.domain.log.infrastructure.SystemActionLogRepository import org.springframework.stereotype.Service @@ -8,9 +10,12 @@ import org.springframework.transaction.annotation.Transactional @Service class SystemActionLogService( private val systemActionLogRepository: SystemActionLogRepository, + private val txTemplates: TransactionTemplates, ) { @Transactional - fun record(systemActionLog: SystemActionLog) { - systemActionLogRepository.save(systemActionLog) + suspend fun record(systemActionLog: SystemActionLog): SystemActionLog { + return txTemplates.writer.coExecute { + systemActionLogRepository.save(systemActionLog) + } } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt b/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt index fdabeb4..1fd1dfb 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt @@ -19,6 +19,12 @@ class UserInfoService( private val userInfoRepository: UserInfoRepository, private val encryptor: Encryptor, ) { + suspend fun getUserByIdOrThrow(uid: Long): UserInfo { + return withContext(Dispatchers.IO) { + getUserByIdOrThrowSync(uid) + } + } + fun getUserByIdOrThrowSync(id: Long): UserInfo { return getUserByIdOrNullSync(id) ?: throw NotFoundException(ErrorCode.NOT_FOUND_USER_ERROR)