Skip to content

Commit

Permalink
refac: group logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 30, 2024
1 parent 8f8520c commit 55d45f5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class AuthFacade(
val userInfo = txTemplates.writer.coExecute {
val userInfo = userInfoService.saveSync(UserInfo(nickname = request.username))

credentialUserInfoService.save(
credentialUserInfoService.saveSync(
CredentialUserInfo(
uid = userInfo.id,
username = request.username,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package com.hero.alignlab.domain.dev.resource

import com.hero.alignlab.common.extension.wrapOk
import com.hero.alignlab.config.swagger.SwaggerTag.DEV_TAG
import com.hero.alignlab.domain.auth.model.OAuthProvider
import com.hero.alignlab.domain.dev.application.DevOAuthService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*

@Tag(name = "Dev OAuth 인증 및 인가 관리")
@Tag(name = DEV_TAG)
@RestController
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class DevOAuthResource(
private val devOAuthService: DevOAuthService,
) {
@Operation(summary = "인가 코드 받기")
@Operation(summary = "[DEV] 인가 코드 받기")
@GetMapping("/api/dev/v1/oauth/{provider}/authorize")
suspend fun getDevOAuthAuthorizeCode(
@PathVariable provider: OAuthProvider,
) = devOAuthService.getOAuthAuthorizeCode(provider).wrapOk()

@Operation(summary = "OAuth Token Generate")
@Operation(summary = "[DEV] OAuth Token Generate")
@GetMapping("/api/dev/v1/oauth/{provider}/token")
suspend fun redirectedDevOAuthAuthorizeCode(
@PathVariable provider: OAuthProvider,
@RequestParam code: String,
) = devOAuthService.resolveOAuth(provider, code).wrapOk()

@Operation(summary = "사용자 정보 조회")
@Operation(summary = "[DEV] 사용자 정보 조회")
@GetMapping("/api/dev/v1/oauth/user")
suspend fun getOAuthUserInfos(
@RequestParam accessToken: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.*
class DevUserInfoResource(
private val userInfoService: UserInfoService,
) {
@Operation(summary = "유저 정보 조회")
@Operation(summary = "[DEV] 유저 정보 조회")
@GetMapping("/api/dev/v1/users/{id}")
suspend fun getUserInfo(
@PathVariable id: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController
class DevWebsocketResource(
private val reactiveConcurrentUserWebSocketHandler: ReactiveConcurrentUserWebSocketHandler
) {
@Operation(summary = "websocket connection closed")
@Operation(summary = "[DEV] websocket connection closed")
@PostMapping("/api/dev/v1/websocket/connection-closed")
suspend fun closedConnection(
@RequestHeader("X-HERO-DEV-TOKEN") token: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.hero.alignlab.domain.group.application

import arrow.fx.coroutines.parZip
import com.hero.alignlab.common.extension.executes
import com.hero.alignlab.common.extension.executesOrNull
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.auth.model.AuthUser
import com.hero.alignlab.domain.group.domain.Group
import com.hero.alignlab.domain.group.domain.GroupUser
import com.hero.alignlab.domain.group.model.response.GetGroupResponse
import com.hero.alignlab.domain.group.model.response.JoinGroupResponse
import com.hero.alignlab.exception.ErrorCode
Expand All @@ -20,17 +23,33 @@ class GroupFacade(
suspend fun withdraw(user: AuthUser, groupId: Long) {
val group = groupService.findByIdOrThrow(groupId)

if (group.ownerUid == user.uid) {
val groupUser = groupUserService.findTop1ByGroupIdOrderByCreatedAtAsc(groupId)
when (group.ownerUid == user.uid) {
true -> withdrawGroupOwner(group)
false -> withdrawGroupUser(groupId, user)
}
}

if (groupUser == null) {
groupService.deleteByIdSync(groupId)
} else {
group.apply {
this.ownerUid = groupUser.uid
}.run { groupService.saveSync(this) }
}
} else {
private suspend fun withdrawGroupOwner(group: Group) {
val groupUser = groupUserService.findTop1ByGroupIdOrderByCreatedAtAsc(group.id)

when (groupUser == null) {
true -> groupService.deleteByIdSync(group.id)
false -> succeedGroupOwner(group, groupUser)
}
}

private fun succeedGroupOwner(group: Group, groupUser: GroupUser) {
val succeedGroup = group.apply {
this.ownerUid = groupUser.uid
}

txTemplates.writer.executesOrNull {
groupService.saveSync(succeedGroup)
}
}

private suspend fun withdrawGroupUser(groupId: Long, user: AuthUser) {
txTemplates.writer.executesOrNull {
groupUserService.deleteBySync(groupId, user.uid)
}
}
Expand All @@ -50,36 +69,43 @@ class GroupFacade(

val groupUser = groupUsers[groupId]

if (groupUser == null && groupUsers.isNotEmpty()) {
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_JOIN_ERROR)
}
when {
/** 이미 다른 그룹에 속해있는 유저 */
groupUser == null && groupUsers.isNotEmpty() -> {
throw InvalidRequestException(ErrorCode.DUPLICATE_GROUP_JOIN_ERROR)
}

if (groupUser != null) {
JoinGroupResponse(
groupId = group.id,
uid = groupUser.uid,
groupUserId = groupUser.id
)
}
/** 이미 그룹원인 경우 */
groupUser != null -> {
JoinGroupResponse(
groupId = group.id,
uid = groupUser.uid,
groupUserId = groupUser.id
)
}

val createdGroupUser = txTemplates.writer.executes {
groupUserService.saveSync(groupId, user.uid)
}
/** 그룹에 조인 */
else -> {
val createdGroupUser = txTemplates.writer.executes {
groupUserService.saveSync(groupId, user.uid)
}

JoinGroupResponse(
groupId = createdGroupUser.groupId,
uid = createdGroupUser.uid,
groupUserId = createdGroupUser.id
)
JoinGroupResponse(
groupId = createdGroupUser.groupId,
uid = createdGroupUser.uid,
groupUserId = createdGroupUser.id
)
}
}
}
}

suspend fun getGroup(user: AuthUser, groupId: Long): GetGroupResponse {
return parZip(
{ groupService.findByIdOrThrow(groupId) },
{ groupUserService.existsByGroupIdAndUid(groupId, user.uid) },
) { group, includeGroup ->
if (!includeGroup) {
) { group, joinedGroup ->
if (!joinedGroup) {
throw NotFoundException(ErrorCode.NOT_FOUND_GROUP_ERROR)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package com.hero.alignlab.domain.user.application

import com.hero.alignlab.common.encrypt.EncryptData
import com.hero.alignlab.common.encrypt.Encryptor
import com.hero.alignlab.domain.user.domain.CredentialUserInfo
import com.hero.alignlab.domain.user.infrastructure.CredentialUserInfoRepository
import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.NotFoundException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class CredentialUserInfoService(
private val credentialUserInfoRepository: CredentialUserInfoRepository,
private val encryptor: Encryptor,
) {
suspend fun existsByUsername(username: String): Boolean {
return withContext(Dispatchers.IO) {
credentialUserInfoRepository.existsByUsername(username)
existsByUsernameSync(username)
}
}

fun save(credentialUserInfo: CredentialUserInfo): CredentialUserInfo {
return credentialUserInfoRepository.save(credentialUserInfo)
private fun existsByUsernameSync(username: String): Boolean {
return credentialUserInfoRepository.existsByUsername(username)
}

suspend fun findByUsernameAndPassword(username: String, password: String): CredentialUserInfo {
return withContext(Dispatchers.IO) {
credentialUserInfoRepository.findByUsernameAndPassword(username, EncryptData.enc(password, encryptor))
} ?: throw NotFoundException(ErrorCode.NOT_FOUND_USER_ERROR)
@Transactional
fun saveSync(credentialUserInfo: CredentialUserInfo): CredentialUserInfo {
return credentialUserInfoRepository.save(credentialUserInfo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ class UserInfoService(

suspend fun findByCredential(username: String, password: String): UserInfo {
return withContext(Dispatchers.IO) {
userInfoRepository.findByCredential(
username = username,
password = EncryptData.enc(password, encryptor)
)
findByCredentialSync(username, password)
} ?: throw NotFoundException(ErrorCode.NOT_FOUND_USER_ERROR)
}

private fun findByCredentialSync(username: String, password: String): UserInfo? {
return userInfoRepository.findByCredential(
username = username,
password = EncryptData.enc(password, encryptor)
)
}

fun findAllByIds(ids: List<Long>): List<UserInfo> {
return userInfoRepository.findAllById(ids)
}
Expand Down

0 comments on commit 55d45f5

Please sign in to comment.