Skip to content

Commit

Permalink
imp: refactor coroutine function
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 8, 2024
1 parent cd6554c commit 5710787
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,7 +22,7 @@ class DiscussionService(
content = request.content,
)

val createdDiscussion = txTemplates.writer.executes {
val createdDiscussion = txTemplates.writer.coExecute {
discussionRepository.save(discussion)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5710787

Please sign in to comment.