From fa8991113c4ef29bfebb75d827cf0b51c653460b Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Sat, 12 Oct 2024 14:00:06 +0900 Subject: [PATCH 01/12] =?UTF-8?q?DRAW-392=20fix:=20401=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EB=B0=9C=EC=83=9D=20=EC=B2=98=EB=A6=AC=20=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xorker/draw/exception/ApiExceptionHandler.kt | 11 +++++++++++ .../kotlin/com/xorker/draw/room/RoomController.kt | 2 ++ app/support/auth/build.gradle.kts | 2 +- .../support/auth/PrincipalUserArgumentResolver.kt | 4 ++-- .../draw/support/auth/security/SecurityConfig.kt | 2 +- .../auth/security/TokenAuthenticationFilter.kt | 2 +- 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt b/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt index 54f0efa8..62cecba1 100644 --- a/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt +++ b/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt @@ -5,6 +5,7 @@ import org.springframework.core.Ordered import org.springframework.core.annotation.Order import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity +import org.springframework.security.core.AuthenticationException import org.springframework.validation.BindException import org.springframework.web.HttpRequestMethodNotSupportedException import org.springframework.web.bind.MethodArgumentNotValidException @@ -21,6 +22,16 @@ class ApiExceptionHandler( ) { private val log = logger() + @ExceptionHandler(AuthenticationException::class) + protected fun handleException(ex: AuthenticationException): ExceptionResponseEntity { + return handleException(UnAuthenticationException) + } + + @ExceptionHandler(AccessDeniedException::class) + protected fun handleException(ex: AccessDeniedException): ExceptionResponseEntity { + return handleException(UnAuthorizedException) + } + @ExceptionHandler(HttpRequestMethodNotSupportedException::class) protected fun handleException(ex: HttpRequestMethodNotSupportedException): ExceptionResponseEntity { log.warn(ex.message, ex) diff --git a/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt b/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt index cfcfe821..2126ad8e 100644 --- a/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt +++ b/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt @@ -6,6 +6,7 @@ import com.xorker.draw.support.auth.PrincipalUser import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Parameter import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @@ -17,6 +18,7 @@ class RoomController( @Operation(summary = "현재 참여 중인 방 정보") @GetMapping("/api/v1/playing-room") + @PreAuthorize("isAuthenticated()") fun getPlayingRoom( @Parameter(hidden = true) user: PrincipalUser, ): PlayingRoomResponse { diff --git a/app/support/auth/build.gradle.kts b/app/support/auth/build.gradle.kts index 18ad5715..9656aa47 100644 --- a/app/support/auth/build.gradle.kts +++ b/app/support/auth/build.gradle.kts @@ -8,7 +8,7 @@ dependencies { implementation(project(":core")) implementation("org.springframework.boot:spring-boot-starter-web:${Versions.SPRING_BOOT}") - implementation("org.springframework.boot:spring-boot-starter-security:${Versions.SPRING_BOOT}") + api("org.springframework.boot:spring-boot-starter-security:${Versions.SPRING_BOOT}") } tasks { diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/PrincipalUserArgumentResolver.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/PrincipalUserArgumentResolver.kt index 5c044516..44e349ca 100644 --- a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/PrincipalUserArgumentResolver.kt +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/PrincipalUserArgumentResolver.kt @@ -23,8 +23,8 @@ internal class PrincipalUserArgumentResolver : HandlerMethodArgumentResolver { @Nullable binderFactory: WebDataBinderFactory?, ): PrincipalUser? { val principal = SecurityContextHolder.getContext() - .authentication - .principal as? UserId + ?.authentication + ?.principal as? UserId ?: return null return PrincipalUser(principal) } diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityConfig.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityConfig.kt index eb50179a..8ede0555 100644 --- a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityConfig.kt +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityConfig.kt @@ -9,9 +9,9 @@ import org.springframework.security.config.http.SessionCreationPolicy import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter -@EnableMethodSecurity @EnableWebSecurity @Configuration +@EnableMethodSecurity(prePostEnabled = true) internal class SecurityConfig { @Bean fun filterChain( diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/TokenAuthenticationFilter.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/TokenAuthenticationFilter.kt index 9b24b978..1b753ec0 100644 --- a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/TokenAuthenticationFilter.kt +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/TokenAuthenticationFilter.kt @@ -32,7 +32,7 @@ internal class TokenAuthenticationFilter( private fun getAccessToken(request: HttpServletRequest): String? { val accessToken = request.getHeader(HEADER_AUTHORIZATION) - if (accessToken.isNullOrBlank().not() && accessToken.startsWith(HEADER_BEARER)) { + if (accessToken.isNullOrBlank().not() && accessToken.lowercase().startsWith(HEADER_BEARER)) { return accessToken.substring(HEADER_BEARER.length) } return null From 550c6acfdee5bbbf8e245a929d08e8a8310de8d8 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Mon, 14 Oct 2024 18:57:16 +0900 Subject: [PATCH 02/12] =?UTF-8?q?DRAW-392=20fix:=20Spring-Security=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A4=84=EC=9D=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../draw/exception/ApiExceptionHandler.kt | 11 -------- .../com/xorker/draw/room/RoomController.kt | 4 +-- app/support/auth/build.gradle.kts | 2 +- .../auth/SecurityExceptionControllerAdvice.kt | 25 +++++++++++++++++++ .../auth/security/SecurityExceptionHandler.kt | 4 +-- .../draw/exception/ExceptionButtonType.kt | 4 +-- .../websocket/handler/BaseWebSocketHandler.kt | 2 +- .../draw/websocket/session/SessionFactory.kt | 2 +- .../xorker/draw/exception/XorkerException.kt | 4 +-- 9 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt diff --git a/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt b/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt index 62cecba1..54f0efa8 100644 --- a/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt +++ b/app/api/src/main/kotlin/com/xorker/draw/exception/ApiExceptionHandler.kt @@ -5,7 +5,6 @@ import org.springframework.core.Ordered import org.springframework.core.annotation.Order import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity -import org.springframework.security.core.AuthenticationException import org.springframework.validation.BindException import org.springframework.web.HttpRequestMethodNotSupportedException import org.springframework.web.bind.MethodArgumentNotValidException @@ -22,16 +21,6 @@ class ApiExceptionHandler( ) { private val log = logger() - @ExceptionHandler(AuthenticationException::class) - protected fun handleException(ex: AuthenticationException): ExceptionResponseEntity { - return handleException(UnAuthenticationException) - } - - @ExceptionHandler(AccessDeniedException::class) - protected fun handleException(ex: AccessDeniedException): ExceptionResponseEntity { - return handleException(UnAuthorizedException) - } - @ExceptionHandler(HttpRequestMethodNotSupportedException::class) protected fun handleException(ex: HttpRequestMethodNotSupportedException): ExceptionResponseEntity { log.warn(ex.message, ex) diff --git a/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt b/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt index 2126ad8e..c519e303 100644 --- a/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt +++ b/app/api/src/main/kotlin/com/xorker/draw/room/RoomController.kt @@ -2,11 +2,11 @@ package com.xorker.draw.room import com.xorker.draw.mafia.MafiaGameUseCase import com.xorker.draw.room.dto.PlayingRoomResponse +import com.xorker.draw.support.auth.NeedLogin import com.xorker.draw.support.auth.PrincipalUser import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Parameter import io.swagger.v3.oas.annotations.tags.Tag -import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @@ -18,7 +18,7 @@ class RoomController( @Operation(summary = "현재 참여 중인 방 정보") @GetMapping("/api/v1/playing-room") - @PreAuthorize("isAuthenticated()") + @NeedLogin fun getPlayingRoom( @Parameter(hidden = true) user: PrincipalUser, ): PlayingRoomResponse { diff --git a/app/support/auth/build.gradle.kts b/app/support/auth/build.gradle.kts index 9656aa47..18ad5715 100644 --- a/app/support/auth/build.gradle.kts +++ b/app/support/auth/build.gradle.kts @@ -8,7 +8,7 @@ dependencies { implementation(project(":core")) implementation("org.springframework.boot:spring-boot-starter-web:${Versions.SPRING_BOOT}") - api("org.springframework.boot:spring-boot-starter-security:${Versions.SPRING_BOOT}") + implementation("org.springframework.boot:spring-boot-starter-security:${Versions.SPRING_BOOT}") } tasks { diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt new file mode 100644 index 00000000..99e42b7b --- /dev/null +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt @@ -0,0 +1,25 @@ +package com.xorker.draw.support.auth + +import com.xorker.draw.exception.UnAuthenticationException +import com.xorker.draw.exception.UnAuthorizedException +import org.springframework.core.Ordered +import org.springframework.core.annotation.Order +import org.springframework.security.access.AccessDeniedException +import org.springframework.security.core.AuthenticationException +import org.springframework.web.bind.annotation.ControllerAdvice +import org.springframework.web.bind.annotation.ExceptionHandler + +@Order(value = Ordered.HIGHEST_PRECEDENCE) +@ControllerAdvice +class SecurityExceptionControllerAdvice { + + @ExceptionHandler(AuthenticationException::class) + fun handleException(ex: AuthenticationException) { + throw UnAuthenticationException(ex) + } + + @ExceptionHandler(AccessDeniedException::class) + fun handleException(ex: AccessDeniedException) { + throw UnAuthorizedException(ex) + } +} diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityExceptionHandler.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityExceptionHandler.kt index 5f7b9ba0..830fc935 100644 --- a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityExceptionHandler.kt +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/security/SecurityExceptionHandler.kt @@ -21,7 +21,7 @@ internal class SecurityExceptionHandler( response: HttpServletResponse, authenticationException: AuthenticationException, ) { - handler.resolveException(request, response, null, UnAuthenticationException) + handler.resolveException(request, response, null, UnAuthenticationException(authenticationException)) } override fun handle( @@ -29,6 +29,6 @@ internal class SecurityExceptionHandler( response: HttpServletResponse, accessDeniedException: AccessDeniedException, ) { - handler.resolveException(request, response, null, UnAuthorizedException) + handler.resolveException(request, response, null, UnAuthorizedException(accessDeniedException)) } } diff --git a/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt b/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt index 360d9b2d..2948ca17 100644 --- a/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt +++ b/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt @@ -41,11 +41,11 @@ fun XorkerException.getButtons(): List { NeedForceUpdateException -> buttonForceUpdate InvalidRequestValueException, - UnAuthorizedException, + is UnAuthorizedException, NotFoundRoomException, -> buttonOkClose - UnAuthenticationException, + is UnAuthenticationException, is UnknownException, -> buttonOkCancel diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt index 2772ff26..9b84292a 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt @@ -41,7 +41,7 @@ internal abstract class BaseWebSocketHandler( override fun afterConnectionEstablished(session: WebSocketSession) { registerRequestId() - val user = getUser(session) ?: throw UnAuthenticationException + val user = getUser(session) ?: throw UnAuthenticationException() val locale = session.getHeader(HEADER_LOCALE) ?: throw InvalidRequestValueException val sessionDto = SessionWrapper(session, user, locale) diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/session/SessionFactory.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/session/SessionFactory.kt index 016706b2..8e0b0f68 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/session/SessionFactory.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/session/SessionFactory.kt @@ -31,6 +31,6 @@ internal class SessionFactory( } private fun getUserId(token: String): UserId { - return tokenUseCase.getUserId(token) ?: throw UnAuthenticationException + return tokenUseCase.getUserId(token) ?: throw UnAuthenticationException() } } diff --git a/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt b/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt index 32be3692..fad1fe35 100644 --- a/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt +++ b/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt @@ -9,8 +9,8 @@ sealed class XorkerException(val code: String, message: String, cause: Throwable sealed class ClientException(code: String, message: String, cause: Throwable? = null) : XorkerException(code, message, cause) data object NeedForceUpdateException : ClientException("forceUpdate", "인증 실패") { private fun readResolve(): Any = NeedForceUpdateException } -data object UnAuthenticationException : ClientException("auth401", "인증 실패") { private fun readResolve(): Any = UnAuthenticationException } -data object UnAuthorizedException : ClientException("auth403", "인가 실패") { private fun readResolve(): Any = UnAuthorizedException } +class UnAuthenticationException(cause: Throwable? = null) : ClientException("auth401", "인증 실패", cause) +class UnAuthorizedException(cause: Throwable? = null) : ClientException("auth403", "인가 실패", cause) data object InvalidRequestValueException : ClientException("c001", "Request 값 잘못됨") { private fun readResolve(): Any = InvalidRequestValueException } data object OAuthFailureException : ClientException("c002", "OAuth 인증 실패") { private fun readResolve(): Any = OAuthFailureException } From f5b7e38c54caebd208445a4c615100e3a4ed0aed Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Mon, 14 Oct 2024 20:01:03 +0900 Subject: [PATCH 03/12] =?UTF-8?q?DRAW-392=20fix:=20401,=20403=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=EC=8B=9C=ED=82=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/SecurityExceptionControllerAdvice.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt index 99e42b7b..da43d41f 100644 --- a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt @@ -1,25 +1,28 @@ package com.xorker.draw.support.auth -import com.xorker.draw.exception.UnAuthenticationException import com.xorker.draw.exception.UnAuthorizedException import org.springframework.core.Ordered import org.springframework.core.annotation.Order +import org.springframework.http.HttpStatus import org.springframework.security.access.AccessDeniedException import org.springframework.security.core.AuthenticationException -import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestControllerAdvice @Order(value = Ordered.HIGHEST_PRECEDENCE) -@ControllerAdvice +@RestControllerAdvice class SecurityExceptionControllerAdvice { + @ResponseStatus(HttpStatus.UNAUTHORIZED) @ExceptionHandler(AuthenticationException::class) - fun handleException(ex: AuthenticationException) { - throw UnAuthenticationException(ex) + fun handleException(ex: AuthenticationException): String { + return "{}" } + @ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler(AccessDeniedException::class) - fun handleException(ex: AccessDeniedException) { - throw UnAuthorizedException(ex) + fun handleException(ex: AccessDeniedException): String { + return "{}" } } From 2ae461550e3a384ba38da73069213f733795a845 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Mon, 14 Oct 2024 20:09:03 +0900 Subject: [PATCH 04/12] DRAW-392 style: ktlint --- .../draw/support/auth/SecurityExceptionControllerAdvice.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt index da43d41f..690fe1c5 100644 --- a/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt +++ b/app/support/auth/src/main/kotlin/com/xorker/draw/support/auth/SecurityExceptionControllerAdvice.kt @@ -1,6 +1,5 @@ package com.xorker.draw.support.auth -import com.xorker.draw.exception.UnAuthorizedException import org.springframework.core.Ordered import org.springframework.core.annotation.Order import org.springframework.http.HttpStatus From c0a86474d7153f96b32e4535a07c97a9a193838a Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Wed, 16 Oct 2024 20:44:56 +0900 Subject: [PATCH 05/12] =?UTF-8?q?DRAW-400=20feat:=20Rematching=20Action=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/xorker/draw/websocket/WebSocketController.kt | 6 ++++++ .../kotlin/com/xorker/draw/websocket/WebSocketRouter.kt | 2 ++ .../xorker/draw/websocket/message/request/RequestAction.kt | 2 ++ .../kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt | 4 ++-- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketController.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketController.kt index 60e40666..413514e4 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketController.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketController.kt @@ -11,6 +11,7 @@ import com.xorker.draw.mafia.WaitingQueueUseCase import com.xorker.draw.room.RoomId import com.xorker.draw.websocket.message.request.mafia.MafiaGameRandomMatchingRequest import com.xorker.draw.websocket.message.request.mafia.SessionInitializeRequest +import com.xorker.draw.websocket.session.Session import com.xorker.draw.websocket.session.SessionFactory import com.xorker.draw.websocket.session.SessionManager import org.slf4j.MDC @@ -26,6 +27,11 @@ internal class WebSocketController( private val userConnectionUseCase: UserConnectionUseCase, ) { + fun rematch(session: Session) { + userConnectionUseCase.disconnectUser(session.user) + waitingQueueUseCase.enqueue(session.user, session.locale) + } + fun initializeWaitingQueueSession(session: WebSocketSession, request: MafiaGameRandomMatchingRequest) { val sessionDto = sessionFactory.create(session, request) diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketRouter.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketRouter.kt index 6ba17b67..1427cc3f 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketRouter.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/WebSocketRouter.kt @@ -30,6 +30,7 @@ internal class WebSocketRouter( RequestAction.PING -> sessionManager.setPing(session.id) RequestAction.INIT -> throw InvalidRequestValueException RequestAction.RANDOM_MATCHING -> throw InvalidRequestValueException + RequestAction.REMATCHING -> webSocketController.rematch(session) RequestAction.START_GAME -> mafiaPhaseUseCase.startGame(session.user) RequestAction.DRAW -> mafiaGameUseCase.draw(session.user, request.extractBody()) RequestAction.END_TURN -> mafiaGameUseCase.nextTurnByUser(session.user) @@ -77,6 +78,7 @@ internal class WebSocketRouter( RequestAction.PING -> throw UnSupportedException RequestAction.INIT -> throw UnSupportedException RequestAction.RANDOM_MATCHING -> throw UnSupportedException + RequestAction.REMATCHING -> webSocketController.rematch(sessionDto) RequestAction.START_GAME -> mafiaPhaseUseCase.startGame(sessionDto.user) RequestAction.DRAW -> mafiaGameUseCase.draw(sessionDto.user, request.extractBody()) diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/message/request/RequestAction.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/message/request/RequestAction.kt index ee509e41..8ea9789c 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/message/request/RequestAction.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/message/request/RequestAction.kt @@ -10,6 +10,8 @@ enum class RequestAction( @Deprecated("handshake 할 때 초기화") RANDOM_MATCHING("마피아 게임 랜덤 매칭"), + + REMATCHING("새로운 게임 매칭"), START_GAME("마피아 게임 시작"), DRAW("그림 그리기"), END_TURN("턴 넘기기"), diff --git a/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt b/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt index d4539488..220e82bc 100644 --- a/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt +++ b/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt @@ -81,7 +81,7 @@ internal class MafiaGameRoomService( val userId = user.id val gameInfo = mafiaGameRepository.getGameInfo(userId) ?: return - if (gameInfo.phase == MafiaPhase.Wait) { + if (gameInfo.phase == MafiaPhase.Wait || gameInfo.phase is MafiaPhase.End) { exitUser(user) return } @@ -102,7 +102,7 @@ internal class MafiaGameRoomService( val userId = user.id val gameInfo = mafiaGameRepository.getGameInfo(userId) ?: return - if (gameInfo.phase != MafiaPhase.Wait) { + if (gameInfo.phase != MafiaPhase.Wait && gameInfo.phase !is MafiaPhase.End) { disconnectUser(user) return } From ca96b8c864e9eab3deda222c00a1d46058275954 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Wed, 16 Oct 2024 22:30:36 +0900 Subject: [PATCH 06/12] =?UTF-8?q?DRAW-400=20feat:=20=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=ED=99=94=EB=A9=B4=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EB=B9=A0=EB=A5=B8=20=EB=A7=A4=EC=B9=AD=20=EC=BC=80=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../draw/mafia/phase/MafiaPhaseEndGameProcessor.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseEndGameProcessor.kt b/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseEndGameProcessor.kt index dc21d8ef..19f489b1 100644 --- a/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseEndGameProcessor.kt +++ b/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseEndGameProcessor.kt @@ -8,6 +8,7 @@ import com.xorker.draw.mafia.MafiaGameResultRepository import com.xorker.draw.mafia.MafiaPhase import com.xorker.draw.mafia.assertIs import com.xorker.draw.timer.TimerRepository +import java.time.Duration import org.springframework.stereotype.Component @Component @@ -25,10 +26,6 @@ internal class MafiaPhaseEndGameProcessor( val room = gameInfo.room - timerRepository.startTimer(room.id, gameOption.endTime) { - processEndGame(gameInfo) - } - val endPhase = assertAndGetEndPhase(phase) judgeGameResult(endPhase) @@ -39,6 +36,15 @@ internal class MafiaPhaseEndGameProcessor( mafiaGameResultRepository.saveMafiaGameResult(gameInfo) + if (room.isRandomMatching) { + timerRepository.startTimer(room.id, Duration.ofMillis(1)) { + mafiaGameRepository.removeGameInfo(gameInfo) + } + } else { + timerRepository.startTimer(room.id, gameOption.endTime) { + processEndGame(gameInfo) + } + } return endPhase } From 56661121875411a5c5c8a0310d8a34da8f253eda Mon Sep 17 00:00:00 2001 From: sunwoong Date: Wed, 16 Oct 2024 22:59:29 +0900 Subject: [PATCH 07/12] =?UTF-8?q?DRAW-375=20refactor:=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xorker/draw/lock/RedisLockAdapter.kt | 9 ++- .../xorker/draw/mafia/MafiaGameRoomService.kt | 4 +- .../com/xorker/draw/mafia/MafiaGameService.kt | 16 ++-- .../draw/mafia/phase/MafiaPhaseService.kt | 76 ++++++++----------- .../com/xorker/draw/lock/LockRepository.kt | 3 +- 5 files changed, 51 insertions(+), 57 deletions(-) diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/lock/RedisLockAdapter.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/lock/RedisLockAdapter.kt index e61e3025..39bcf28d 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/lock/RedisLockAdapter.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/lock/RedisLockAdapter.kt @@ -11,7 +11,7 @@ internal class RedisLockAdapter( private val redisTemplate: RedisTemplate, ) : LockRepository { - override fun lock(key: String) { + override fun lock(key: String, call: () -> R): R { while (getLock(key).not()) { try { Thread.sleep(SLEEP_TIME) @@ -19,9 +19,14 @@ internal class RedisLockAdapter( throw UnSupportedException } } + try { + return call.invoke() + } finally { + unlock(key) + } } - override fun unlock(key: String) { + private fun unlock(key: String) { redisTemplate.delete(key + LOCK) } diff --git a/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt b/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt index 5e9daeca..2ba0f939 100644 --- a/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt +++ b/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt @@ -6,6 +6,7 @@ import com.xorker.draw.exception.InvalidRequestOtherPlayingException import com.xorker.draw.exception.InvalidRequestValueException import com.xorker.draw.exception.MaxRoomException import com.xorker.draw.exception.NotFoundRoomException +import com.xorker.draw.lock.LockRepository import com.xorker.draw.room.Room import com.xorker.draw.room.RoomId import com.xorker.draw.room.RoomRepository @@ -17,6 +18,7 @@ import org.springframework.stereotype.Service internal class MafiaGameRoomService( private val mafiaGameRepository: MafiaGameRepository, private val roomRepository: RoomRepository, + private val lockRepository: LockRepository, private val mafiaGameInfoEventProducer: MafiaGameInfoEventProducer, ) : UserConnectionUseCase { @@ -33,7 +35,7 @@ internal class MafiaGameRoomService( val gameInfo = mafiaGameRepository.getGameInfo(roomId) ?: throw NotFoundRoomException - synchronized(gameInfo) { + lockRepository.lock(roomId.value) { if (gameInfo.phase != MafiaPhase.Wait && gameInfo.room.players.any { it.userId == user.id }.not()) { throw AlreadyPlayingRoomException } diff --git a/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameService.kt b/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameService.kt index c0a4509f..08368066 100644 --- a/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameService.kt +++ b/core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameService.kt @@ -130,18 +130,16 @@ internal class MafiaGameService( ) { val voterUserId = voter.id - lockRepository.lock(voterUserId.value.toString()) + lockRepository.lock(voterUserId.value.toString()) { + players.forEach { player -> + val userIds = player.value - players.forEach { player -> - val userIds = player.value - - if (voterUserId in userIds) { - userIds.remove(voterUserId) + if (voterUserId in userIds) { + userIds.remove(voterUserId) + } } + players[targetUserId]?.add(voterUserId) ?: InvalidRequestValueException } - players[targetUserId]?.add(voterUserId) ?: InvalidRequestValueException - - lockRepository.unlock(voterUserId.value.toString()) } private fun validateIsMafia(player: User, mafiaPlayer: MafiaPlayer) { diff --git a/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseService.kt b/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseService.kt index fa700655..ea2d958b 100644 --- a/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseService.kt +++ b/core/src/main/kotlin/com/xorker/draw/mafia/phase/MafiaPhaseService.kt @@ -38,13 +38,11 @@ internal class MafiaPhaseService( private fun startGame(gameInfo: MafiaGameInfo): MafiaPhase.Ready { val roomId = gameInfo.room.id - lockRepository.lock(roomId.value) + val phase = lockRepository.lock(roomId.value) { + assertIs(gameInfo.phase) - assertIs(gameInfo.phase) - - val phase = mafiaPhaseStartGameProcessor.startMafiaGame(gameInfo) { playGame(roomId) } - - lockRepository.unlock(roomId.value) + mafiaPhaseStartGameProcessor.startMafiaGame(gameInfo) { playGame(roomId) } + } mafiaGameInfoEventProducer.changePhase(gameInfo) @@ -54,14 +52,12 @@ internal class MafiaPhaseService( override fun playGame(roomId: RoomId): MafiaPhase.Playing { val gameInfo = getGameInfo(roomId) - lockRepository.lock(roomId.value) - - val readyPhase = gameInfo.phase - assertIs(readyPhase) + val phase = lockRepository.lock(roomId.value) { + val readyPhase = gameInfo.phase + assertIs(readyPhase) - val phase = mafiaPhasePlayGameProcessor.playMafiaGame(gameInfo) { vote(roomId) } - - lockRepository.unlock(roomId.value) + mafiaPhasePlayGameProcessor.playMafiaGame(gameInfo) { vote(roomId) } + } mafiaGameInfoEventProducer.changePhase(gameInfo) @@ -71,22 +67,20 @@ internal class MafiaPhaseService( override fun vote(roomId: RoomId): MafiaPhase.Vote { val gameInfo = getGameInfo(roomId) - lockRepository.lock(roomId.value) - - val playingPhase = gameInfo.phase - assertIs(playingPhase) - - val phase = mafiaPhasePlayVoteProcessor.playVote( - gameInfo, - { - interAnswer(roomId) - }, - { - endGame(roomId) - }, - ) - - lockRepository.unlock(roomId.value) + val phase = lockRepository.lock(roomId.value) { + val playingPhase = gameInfo.phase + assertIs(playingPhase) + + mafiaPhasePlayVoteProcessor.playVote( + gameInfo, + { + interAnswer(roomId) + }, + { + endGame(roomId) + }, + ) + } mafiaGameInfoEventProducer.changePhase(gameInfo) @@ -96,14 +90,12 @@ internal class MafiaPhaseService( override fun interAnswer(roomId: RoomId): MafiaPhase.InferAnswer { val gameInfo = getGameInfo(roomId) - lockRepository.lock(roomId.value) - - val votePhase = gameInfo.phase - assertIs(votePhase) - - val phase = mafiaPhaseInferAnswerProcessor.playInferAnswer(gameInfo) { endGame(roomId) } + val phase = lockRepository.lock(roomId.value) { + val votePhase = gameInfo.phase + assertIs(votePhase) - lockRepository.unlock(roomId.value) + mafiaPhaseInferAnswerProcessor.playInferAnswer(gameInfo) { endGame(roomId) } + } mafiaGameInfoEventProducer.changePhase(gameInfo) @@ -113,14 +105,12 @@ internal class MafiaPhaseService( override fun endGame(roomId: RoomId): MafiaPhase.End { val gameInfo = getGameInfo(roomId) - lockRepository.lock(roomId.value) - - val votePhase = gameInfo.phase - assert(votePhase) - - val phase = mafiaPhaseEndGameProcessor.endGame(gameInfo) + val phase = lockRepository.lock(roomId.value) { + val votePhase = gameInfo.phase + assert(votePhase) - lockRepository.unlock(roomId.value) + mafiaPhaseEndGameProcessor.endGame(gameInfo) + } mafiaGameInfoEventProducer.changePhase(gameInfo) diff --git a/domain/src/main/kotlin/com/xorker/draw/lock/LockRepository.kt b/domain/src/main/kotlin/com/xorker/draw/lock/LockRepository.kt index 4229d10a..5390841c 100644 --- a/domain/src/main/kotlin/com/xorker/draw/lock/LockRepository.kt +++ b/domain/src/main/kotlin/com/xorker/draw/lock/LockRepository.kt @@ -1,6 +1,5 @@ package com.xorker.draw.lock interface LockRepository { - fun lock(key: String) - fun unlock(key: String) + fun lock(key: String, call: () -> R): R } From 0bde910eec2fb556bf1d245036c42f87c6a81541 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Thu, 17 Oct 2024 20:59:53 +0900 Subject: [PATCH 08/12] =?UTF-8?q?DRAW-390=20feat:=20=EA=B5=AC=EA=B8=80=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/oauth/build.gradle.kts | 1 + .../com/xorker/draw/oauth/OAuthAdapter.kt | 3 +++ .../oauth/google/GoogleApiConfiguration.kt | 8 ++++++ .../draw/oauth/google/GoogleApiProperties.kt | 8 ++++++ .../draw/oauth/google/GoogleAuthService.kt | 27 +++++++++++++++++++ .../src/main/resources/application-oauth.yml | 2 ++ buildSrc/src/main/kotlin/Versions.kt | 1 + .../com/xorker/draw/auth/AuthPlatform.kt | 2 ++ .../kotlin/com/xorker/draw/auth/AuthType.kt | 1 + 9 files changed, 53 insertions(+) create mode 100644 adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiConfiguration.kt create mode 100644 adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt create mode 100644 adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleAuthService.kt diff --git a/adapter/oauth/build.gradle.kts b/adapter/oauth/build.gradle.kts index a05988cf..e88db00f 100644 --- a/adapter/oauth/build.gradle.kts +++ b/adapter/oauth/build.gradle.kts @@ -10,6 +10,7 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter:${Versions.SPRING_BOOT}") implementation("org.springframework.cloud:spring-cloud-starter-openfeign:${Versions.OPEN_FEIGN}") + implementation("com.google.api-client:google-api-client:${Versions.GOOGLE_API_CLIENT}") } tasks { diff --git a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/OAuthAdapter.kt b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/OAuthAdapter.kt index 4cf8ce0b..2453c7e8 100644 --- a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/OAuthAdapter.kt +++ b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/OAuthAdapter.kt @@ -3,15 +3,18 @@ package com.xorker.draw.oauth import com.xorker.draw.auth.AuthRepository import com.xorker.draw.auth.AuthType import com.xorker.draw.oauth.apple.AppleAuthService +import com.xorker.draw.oauth.google.GoogleAuthService import org.springframework.stereotype.Component @Component internal class OAuthAdapter( private val appleAuthService: AppleAuthService, + private val googleAuthService: GoogleAuthService, ) : AuthRepository { override fun getPlatformUserId(authType: AuthType, token: String): String { return when (authType) { AuthType.APPLE_ID_TOKEN -> appleAuthService.getPlatformUserId(token) + AuthType.GOOGLE_ID_TOKEN -> googleAuthService.getPlatformUserId(token) } } diff --git a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiConfiguration.kt b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiConfiguration.kt new file mode 100644 index 00000000..d3acc8eb --- /dev/null +++ b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiConfiguration.kt @@ -0,0 +1,8 @@ +package com.xorker.draw.oauth.google + +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Configuration + +@Configuration +@EnableConfigurationProperties(GoogleApiProperties::class) +class GoogleApiConfiguration diff --git a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt new file mode 100644 index 00000000..dfdb2f73 --- /dev/null +++ b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt @@ -0,0 +1,8 @@ +package com.xorker.draw.oauth.google + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties("social.google") +data class GoogleApiProperties( + val clientId: String, +) diff --git a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleAuthService.kt b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleAuthService.kt new file mode 100644 index 00000000..e0e20258 --- /dev/null +++ b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleAuthService.kt @@ -0,0 +1,27 @@ +package com.xorker.draw.oauth.google + +import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier +import com.google.api.client.http.javanet.NetHttpTransport +import com.google.api.client.json.gson.GsonFactory +import com.xorker.draw.exception.OAuthFailureException +import java.security.GeneralSecurityException +import org.springframework.stereotype.Component + +@Component +internal class GoogleAuthService( + googleApiProperties: GoogleApiProperties, +) { + private val idTokenVerifier = + GoogleIdTokenVerifier.Builder(NetHttpTransport(), GsonFactory.getDefaultInstance()) + .setAudience(listOf(googleApiProperties.clientId)) + .build() + + fun getPlatformUserId(token: String): String { + try { + val idToken = idTokenVerifier.verify(token) + return idToken?.payload?.subject ?: throw OAuthFailureException + } catch (e: GeneralSecurityException) { + throw OAuthFailureException + } + } +} diff --git a/adapter/oauth/src/main/resources/application-oauth.yml b/adapter/oauth/src/main/resources/application-oauth.yml index 67402ac3..f2ff7970 100644 --- a/adapter/oauth/src/main/resources/application-oauth.yml +++ b/adapter/oauth/src/main/resources/application-oauth.yml @@ -2,3 +2,5 @@ oauth: apple: iss: https://appleid.apple.com client-id: ${APPLE_CLIENT_ID} + google: + client-id: ${GOOGLE_CLIENT_ID} diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 9e78cd14..4df65d27 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -15,4 +15,5 @@ object Versions { const val MICROMETER = "1.12.9" const val SENTRY = "7.0.0" const val FIREBASE = "9.3.0" + const val GOOGLE_API_CLIENT = "1.32.1" } diff --git a/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt b/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt index a773fac1..211515aa 100644 --- a/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt +++ b/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt @@ -2,5 +2,7 @@ package com.xorker.draw.auth enum class AuthPlatform(description: String) { APPLE("애플"), + GOOGLE("구글"), + ; } diff --git a/domain/src/main/kotlin/com/xorker/draw/auth/AuthType.kt b/domain/src/main/kotlin/com/xorker/draw/auth/AuthType.kt index 838dfa6e..788f71e9 100644 --- a/domain/src/main/kotlin/com/xorker/draw/auth/AuthType.kt +++ b/domain/src/main/kotlin/com/xorker/draw/auth/AuthType.kt @@ -2,4 +2,5 @@ package com.xorker.draw.auth enum class AuthType(val authPlatform: AuthPlatform) { APPLE_ID_TOKEN(AuthPlatform.APPLE), + GOOGLE_ID_TOKEN(AuthPlatform.GOOGLE), } From 69227eab65acb2b4fe6dab2c8d0a8eacee34ae9a Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Mon, 21 Oct 2024 23:35:29 +0900 Subject: [PATCH 09/12] DRAW-390 style: ktlint --- .../kotlin/com/xorker/draw/auth/AuthPlatform.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt b/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt index 211515aa..0c43e45a 100644 --- a/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt +++ b/domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt @@ -1,8 +1,8 @@ -package com.xorker.draw.auth - -enum class AuthPlatform(description: String) { - APPLE("애플"), - GOOGLE("구글"), - - ; -} +package com.xorker.draw.auth + +enum class AuthPlatform(description: String) { + APPLE("애플"), + GOOGLE("구글"), + + ; +} From e6169ccedf3f74a2800096669ffb75ab312d2bab Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Wed, 23 Oct 2024 23:35:48 +0900 Subject: [PATCH 10/12] =?UTF-8?q?DRAW-390=20fix:=20=EB=B2=84=EA=B7=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../draw/oauth/google/GoogleApiProperties.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt index dfdb2f73..a5ffd0c6 100644 --- a/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt +++ b/adapter/oauth/src/main/kotlin/com/xorker/draw/oauth/google/GoogleApiProperties.kt @@ -1,8 +1,8 @@ -package com.xorker.draw.oauth.google - -import org.springframework.boot.context.properties.ConfigurationProperties - -@ConfigurationProperties("social.google") -data class GoogleApiProperties( - val clientId: String, -) +package com.xorker.draw.oauth.google + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties("oauth.google") +data class GoogleApiProperties( + val clientId: String, +) From 998579e634a57f28ef9afa62ee9743057ed1c8e8 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Thu, 24 Oct 2024 20:20:53 +0900 Subject: [PATCH 11/12] =?UTF-8?q?DRAW-390=20fix:=20User=20name=20Nullable?= =?UTF-8?q?=20=EB=8C=80=EC=9D=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/com/xorker/draw/user/UserAdapter.kt | 8 ++++---- .../src/main/kotlin/com/xorker/draw/user/UserJpaEntity.kt | 7 +++---- core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt | 6 +++--- domain/src/main/kotlin/com/xorker/draw/user/User.kt | 5 +++++ .../main/kotlin/com/xorker/draw/user/UserRepository.kt | 8 ++++---- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserAdapter.kt b/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserAdapter.kt index 11919965..89835304 100644 --- a/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserAdapter.kt +++ b/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserAdapter.kt @@ -13,19 +13,19 @@ internal class UserAdapter( private val userJpaRepository: UserJpaRepository, private val authUserJpaRepository: AuthUserJpaRepository, ) : UserRepository { - override fun getUser(platform: AuthPlatform, platformUserId: String): User? = + override fun getUser(platform: AuthPlatform, platformUserId: String): UserInfo? = authUserJpaRepository.find(platform, platformUserId)?.user?.toDomain() - override fun getUser(userId: UserId): User? = + override fun getUser(userId: UserId): UserInfo? = userJpaRepository.findByIdOrNull(userId.value)?.toDomain() - override fun createUser(platform: AuthPlatform, platformUserId: String, userName: String): User { + override fun createUser(platform: AuthPlatform, platformUserId: String, userName: String): UserInfo { val user = UserJpaEntity() val authUser = authUserJpaRepository.save(AuthUserJpaEntity.of(platform, platformUserId, user)) return authUser.user.toDomain() } - override fun createUser(userName: String): User { + override fun createUser(userName: String?): UserInfo { val user = UserJpaEntity.of(userName) val savedUser = userJpaRepository.save(user) return savedUser.toDomain() diff --git a/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserJpaEntity.kt b/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserJpaEntity.kt index 31bbbb6e..d1cdde44 100644 --- a/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserJpaEntity.kt +++ b/adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserJpaEntity.kt @@ -1,7 +1,6 @@ package com.xorker.draw.user import com.xorker.draw.BaseJpaEntity -import com.xorker.draw.exception.InvalidUserStatusException import jakarta.persistence.Column import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue @@ -35,7 +34,7 @@ internal class UserJpaEntity : BaseJpaEntity() { internal fun of(id: Long): UserJpaEntity = UserJpaEntity().apply { this.id = id } - internal fun of(name: String): UserJpaEntity = + internal fun of(name: String?): UserJpaEntity = UserJpaEntity().apply { this.name = name } internal fun from(user: User): UserJpaEntity = @@ -54,7 +53,7 @@ internal class UserJpaEntity : BaseJpaEntity() { } } -internal fun UserJpaEntity.toDomain(): User = User( +internal fun UserJpaEntity.toDomain(): UserInfo = UserInfo( id = UserId(this.id), - name = this.name ?: throw InvalidUserStatusException, + name = this.name, ) diff --git a/core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt b/core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt index 3d40abe9..3bd68f4a 100644 --- a/core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt +++ b/core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt @@ -3,8 +3,8 @@ package com.xorker.draw.auth import com.xorker.draw.auth.token.AccessTokenRepository import com.xorker.draw.auth.token.RefreshTokenRepository import com.xorker.draw.auth.token.Token -import com.xorker.draw.user.User import com.xorker.draw.user.UserId +import com.xorker.draw.user.UserInfo import com.xorker.draw.user.UserRepository import java.time.Duration import java.time.Period @@ -28,7 +28,7 @@ internal class AuthService( } override fun anonymousSignIn(): Token { - val user = userRepository.createUser(""); // TODO 이름 정책 정해지면 변경 예정 + val user = userRepository.createUser(null); // TODO 이름 정책 정해지면 변경 예정 return createToken(user.id, Period.ofYears(100)) } @@ -45,7 +45,7 @@ internal class AuthService( userRepository.withdrawal(userId) } - private fun createUser(authType: AuthType, platformUserId: String): User { + private fun createUser(authType: AuthType, platformUserId: String): UserInfo { val userName = authRepository.getPlatformUserName(authType, platformUserId) return userRepository.createUser(authType.authPlatform, platformUserId, userName) diff --git a/domain/src/main/kotlin/com/xorker/draw/user/User.kt b/domain/src/main/kotlin/com/xorker/draw/user/User.kt index 1bc9a384..c592c27c 100644 --- a/domain/src/main/kotlin/com/xorker/draw/user/User.kt +++ b/domain/src/main/kotlin/com/xorker/draw/user/User.kt @@ -7,3 +7,8 @@ data class User( val id: UserId, val name: String, ) + +data class UserInfo( + val id: UserId, + val name: String?, +) diff --git a/domain/src/main/kotlin/com/xorker/draw/user/UserRepository.kt b/domain/src/main/kotlin/com/xorker/draw/user/UserRepository.kt index 9aecf15c..136ef676 100644 --- a/domain/src/main/kotlin/com/xorker/draw/user/UserRepository.kt +++ b/domain/src/main/kotlin/com/xorker/draw/user/UserRepository.kt @@ -3,13 +3,13 @@ package com.xorker.draw.user import com.xorker.draw.auth.AuthPlatform interface UserRepository { - fun getUser(platform: AuthPlatform, platformUserId: String): User? + fun getUser(platform: AuthPlatform, platformUserId: String): UserInfo? - fun getUser(userId: UserId): User? + fun getUser(userId: UserId): UserInfo? - fun createUser(platform: AuthPlatform, platformUserId: String, userName: String): User + fun createUser(platform: AuthPlatform, platformUserId: String, userName: String): UserInfo - fun createUser(userName: String): User + fun createUser(userName: String?): UserInfo fun withdrawal(userId: UserId) } From 4508975c28cfc22219481a11516672ce8ac1c189 Mon Sep 17 00:00:00 2001 From: sunwoong Date: Sun, 3 Nov 2024 19:30:42 +0900 Subject: [PATCH 12/12] =?UTF-8?q?DRAW-408=20refactor:=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xorker/draw/config/RedisConfig.kt | 8 +- .../com/xorker/draw/mafia/MafiaGameAdapter.kt | 14 +-- ...ameInfo.kt => MafiaGameInfoRedisEntity.kt} | 28 +++--- ...ption.kt => MafiaGameOptionRedisEntity.kt} | 4 +- ...aKeyword.kt => MafiaKeywordRedisEntity.kt} | 4 +- ...MafiaPhase.kt => MafiaPhaseRedisEntity.kt} | 96 +++++++++---------- ...fiaPlayer.kt => MafiaPlayerRedisEntity.kt} | 4 +- ...isMafiaRoom.kt => MafiaRoomRedisEntity.kt} | 12 +-- 8 files changed, 85 insertions(+), 85 deletions(-) rename adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/{RedisMafiaGameInfo.kt => MafiaGameInfoRedisEntity.kt} (65%) rename adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/{RedisMafiaGameOption.kt => MafiaGameOptionRedisEntity.kt} (90%) rename adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/{RedisMafiaKeyword.kt => MafiaKeywordRedisEntity.kt} (73%) rename adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/{RedisMafiaPhase.kt => MafiaPhaseRedisEntity.kt} (75%) rename adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/{RedisMafiaPlayer.kt => MafiaPlayerRedisEntity.kt} (85%) rename adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/{RedisMafiaRoom.kt => MafiaRoomRedisEntity.kt} (67%) diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/config/RedisConfig.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/config/RedisConfig.kt index 4cd5d191..c048f52b 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/config/RedisConfig.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/config/RedisConfig.kt @@ -1,6 +1,6 @@ package com.xorker.draw.config -import com.xorker.draw.mafia.dto.RedisMafiaGameInfo +import com.xorker.draw.mafia.dto.MafiaGameInfoRedisEntity import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -32,13 +32,13 @@ internal class RedisConfig { } @Bean - fun redisTemplateWithObject(connectionFactory: RedisConnectionFactory): RedisTemplate { - val template = RedisTemplate() + fun redisTemplateWithObject(connectionFactory: RedisConnectionFactory): RedisTemplate { + val template = RedisTemplate() template.connectionFactory = connectionFactory template.keySerializer = StringRedisSerializer() - template.valueSerializer = Jackson2JsonRedisSerializer(RedisMafiaGameInfo::class.java) + template.valueSerializer = Jackson2JsonRedisSerializer(MafiaGameInfoRedisEntity::class.java) return template } diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/MafiaGameAdapter.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/MafiaGameAdapter.kt index 4b5cb9b4..d50dadbf 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/MafiaGameAdapter.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/MafiaGameAdapter.kt @@ -1,7 +1,7 @@ package com.xorker.draw.mafia -import com.xorker.draw.mafia.dto.RedisMafiaGameInfo -import com.xorker.draw.mafia.dto.toRedisMafiaGameInfo +import com.xorker.draw.mafia.dto.MafiaGameInfoRedisEntity +import com.xorker.draw.mafia.dto.toMafiaGameInfoRedisEntity import com.xorker.draw.room.Room import com.xorker.draw.room.RoomId import com.xorker.draw.room.RoomRepository @@ -14,7 +14,7 @@ import org.springframework.stereotype.Component @Component internal class MafiaGameAdapter( private val metricManager: MetricManager, - private val redisTemplateWithObject: RedisTemplate, + private val redisTemplateWithObject: RedisTemplate, private val redisTemplate: RedisTemplate, private val timerRepository: TimerRepository, ) : MafiaGameRepository, RoomRepository { @@ -34,7 +34,7 @@ internal class MafiaGameAdapter( redisTemplateWithObject .opsForValue() - .set(room.id.value, gameInfo.toRedisMafiaGameInfo()) + .set(room.id.value, gameInfo.toMafiaGameInfoRedisEntity()) room.players.forEach { redisTemplate @@ -66,7 +66,7 @@ internal class MafiaGameAdapter( return redisTemplateWithObject .opsForValue() .get(roomId.value) - ?.toMafiaGameInfo() + ?.toDomain() } override fun getGameInfo(userId: UserId): MafiaGameInfo? { @@ -77,7 +77,7 @@ internal class MafiaGameAdapter( return redisTemplateWithObject .opsForValue() .get(roomId) - ?.toMafiaGameInfo() + ?.toDomain() } override fun removePlayer(userId: UserId) { @@ -88,7 +88,7 @@ internal class MafiaGameAdapter( return redisTemplateWithObject .opsForValue() .get(roomId.value) - ?.toMafiaGameInfo() + ?.toDomain() ?.room } } diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaGameInfo.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaGameInfoRedisEntity.kt similarity index 65% rename from adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaGameInfo.kt rename to adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaGameInfoRedisEntity.kt index 2c8af5d2..f54639da 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaGameInfo.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaGameInfoRedisEntity.kt @@ -4,23 +4,23 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.xorker.draw.mafia.MafiaGameInfo -data class RedisMafiaGameInfo @JsonCreator constructor( - @JsonProperty("room") val room: RedisMafiaRoom, - @JsonProperty("phase") val phase: RedisMafiaPhase, - @JsonProperty("gameOption") val gameOption: RedisMafiaGameOption, +data class MafiaGameInfoRedisEntity @JsonCreator constructor( + @JsonProperty("room") val room: MafiaRoomRedisEntity, + @JsonProperty("phase") val phase: MafiaPhaseRedisEntity, + @JsonProperty("gameOption") val gameOption: MafiaGameOptionRedisEntity, ) { - fun toMafiaGameInfo(): MafiaGameInfo = MafiaGameInfo( - room = room.toRoom(), - phase = phase.toMafiaPhase(), - gameOption = gameOption.toGameOption(), + fun toDomain(): MafiaGameInfo = MafiaGameInfo( + room = room.toDomain(), + phase = phase.toDomain(), + gameOption = gameOption.toDomain(), ) } -fun MafiaGameInfo.toRedisMafiaGameInfo(): RedisMafiaGameInfo = RedisMafiaGameInfo( - room = RedisMafiaRoom( +fun MafiaGameInfo.toMafiaGameInfoRedisEntity(): MafiaGameInfoRedisEntity = MafiaGameInfoRedisEntity( + room = MafiaRoomRedisEntity( id = room.id.value, locale = room.locale, - owner = RedisMafiaPlayer( + owner = MafiaPlayerRedisEntity( id = room.owner.userId.value, nickname = room.owner.nickname, color = room.owner.color, @@ -28,7 +28,7 @@ fun MafiaGameInfo.toRedisMafiaGameInfo(): RedisMafiaGameInfo = RedisMafiaGameInf ), maxMemberNum = room.maxMemberNum, players = room.players.map { player -> - RedisMafiaPlayer( + MafiaPlayerRedisEntity( id = player.userId.value, nickname = player.nickname, color = player.color, @@ -37,8 +37,8 @@ fun MafiaGameInfo.toRedisMafiaGameInfo(): RedisMafiaGameInfo = RedisMafiaGameInf }, isRandomMatching = room.isRandomMatching, ), - phase = phase.toRedisMafiaPhase(), - gameOption = RedisMafiaGameOption( + phase = phase.toMafiaPhaseRedisEntity(), + gameOption = MafiaGameOptionRedisEntity( minimum = gameOption.minimum, maximum = gameOption.maximum, readyTime = gameOption.readyTime.toMillis(), diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaGameOption.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaGameOptionRedisEntity.kt similarity index 90% rename from adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaGameOption.kt rename to adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaGameOptionRedisEntity.kt index 82bfc3bd..8169b63a 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaGameOption.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaGameOptionRedisEntity.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.xorker.draw.mafia.MafiaGameOption import java.time.Duration -data class RedisMafiaGameOption @JsonCreator constructor( +data class MafiaGameOptionRedisEntity @JsonCreator constructor( @JsonProperty("minimum") val minimum: Int, @JsonProperty("maximum") val maximum: Int, @JsonProperty("readyTime") val readyTime: Long, @@ -19,7 +19,7 @@ data class RedisMafiaGameOption @JsonCreator constructor( @JsonProperty("endTime") val endTime: Long, ) -fun RedisMafiaGameOption.toGameOption(): MafiaGameOption = MafiaGameOption( +fun MafiaGameOptionRedisEntity.toDomain(): MafiaGameOption = MafiaGameOption( minimum = this.minimum, maximum = this.maximum, readyTime = Duration.ofMillis(this.readyTime), diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaKeyword.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaKeywordRedisEntity.kt similarity index 73% rename from adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaKeyword.kt rename to adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaKeywordRedisEntity.kt index bbe60f51..afe76874 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaKeyword.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaKeywordRedisEntity.kt @@ -4,12 +4,12 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.xorker.draw.mafia.MafiaKeyword -data class RedisMafiaKeyword @JsonCreator constructor( +data class MafiaKeywordRedisEntity @JsonCreator constructor( @JsonProperty("category") val category: String, @JsonProperty("answer") val answer: String, ) -fun RedisMafiaKeyword.toMafiaKeyword(): MafiaKeyword = MafiaKeyword( +fun MafiaKeywordRedisEntity.toDomain(): MafiaKeyword = MafiaKeyword( answer = this.answer, category = this.category, ) diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaPhase.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaPhaseRedisEntity.kt similarity index 75% rename from adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaPhase.kt rename to adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaPhaseRedisEntity.kt index 40573ed7..d5219006 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaPhase.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaPhaseRedisEntity.kt @@ -7,12 +7,12 @@ import com.xorker.draw.mafia.turn.TurnInfo import com.xorker.draw.user.UserId import java.util.* -data class RedisMafiaPhase @JsonCreator constructor( +data class MafiaPhaseRedisEntity @JsonCreator constructor( @JsonProperty("status") val status: RedisMafiaPhaseStatus, - @JsonProperty("turnList") val turnList: List? = null, - @JsonProperty("mafiaPlayer") val mafiaPlayer: RedisMafiaPlayer? = null, - @JsonProperty("keyword") val keyword: RedisMafiaKeyword? = null, - @JsonProperty("drawData") val drawData: List? = null, + @JsonProperty("turnList") val turnList: List? = null, + @JsonProperty("mafiaPlayer") val mafiaPlayer: MafiaPlayerRedisEntity? = null, + @JsonProperty("keyword") val keyword: MafiaKeywordRedisEntity? = null, + @JsonProperty("drawData") val drawData: List? = null, @JsonProperty("players") val players: Map>? = null, @JsonProperty("round") val round: Int? = null, @JsonProperty("turn") val turn: Int? = null, @@ -21,7 +21,7 @@ data class RedisMafiaPhase @JsonCreator constructor( @JsonProperty("mafiaWin") val isMafiaWin: Boolean? = null, ) -data class RedisDrawInfo @JsonCreator constructor( +data class DrawInfoRedisEntity @JsonCreator constructor( @JsonProperty("userId") val userId: Long, @JsonProperty("draw") val draw: Map, ) @@ -58,56 +58,56 @@ fun deserializePlayers(players: Map>): Map RedisMafiaPhase( + is MafiaPhase.Wait -> MafiaPhaseRedisEntity( status = RedisMafiaPhaseStatus.WAIT, ) - is MafiaPhase.Ready -> RedisMafiaPhase( + is MafiaPhase.Ready -> MafiaPhaseRedisEntity( status = RedisMafiaPhaseStatus.READY, turnList = turnList.map { player -> - RedisMafiaPlayer( + MafiaPlayerRedisEntity( id = player.userId.value, nickname = player.nickname, color = player.color, isConnect = player.isConnect(), ) }, - mafiaPlayer = RedisMafiaPlayer( + mafiaPlayer = MafiaPlayerRedisEntity( id = mafiaPlayer.userId.value, nickname = mafiaPlayer.nickname, color = mafiaPlayer.color, isConnect = mafiaPlayer.isConnect(), ), - keyword = RedisMafiaKeyword( + keyword = MafiaKeywordRedisEntity( answer = keyword.answer, category = keyword.category, ), ) - is MafiaPhase.Playing -> RedisMafiaPhase( + is MafiaPhase.Playing -> MafiaPhaseRedisEntity( status = RedisMafiaPhaseStatus.PLAYING, turnList = turnList.map { player -> - RedisMafiaPlayer( + MafiaPlayerRedisEntity( id = player.userId.value, nickname = player.nickname, color = player.color, isConnect = player.isConnect(), ) }, - mafiaPlayer = RedisMafiaPlayer( + mafiaPlayer = MafiaPlayerRedisEntity( id = mafiaPlayer.userId.value, nickname = mafiaPlayer.nickname, color = mafiaPlayer.color, isConnect = mafiaPlayer.isConnect(), ), - keyword = RedisMafiaKeyword( + keyword = MafiaKeywordRedisEntity( answer = keyword.answer, category = keyword.category, ), drawData = drawData.map { pair -> - RedisDrawInfo( + DrawInfoRedisEntity( userId = pair.first.value, draw = pair.second, ) @@ -116,28 +116,28 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase { turn = turnInfo.turn, ) - is MafiaPhase.Vote -> RedisMafiaPhase( + is MafiaPhase.Vote -> MafiaPhaseRedisEntity( status = RedisMafiaPhaseStatus.VOTE, turnList = turnList.map { player -> - RedisMafiaPlayer( + MafiaPlayerRedisEntity( id = player.userId.value, nickname = player.nickname, color = player.color, isConnect = player.isConnect(), ) }, - mafiaPlayer = RedisMafiaPlayer( + mafiaPlayer = MafiaPlayerRedisEntity( id = mafiaPlayer.userId.value, nickname = mafiaPlayer.nickname, color = mafiaPlayer.color, isConnect = mafiaPlayer.isConnect(), ), - keyword = RedisMafiaKeyword( + keyword = MafiaKeywordRedisEntity( answer = keyword.answer, category = keyword.category, ), drawData = drawData.map { pair -> - RedisDrawInfo( + DrawInfoRedisEntity( userId = pair.first.value, draw = pair.second, ) @@ -145,28 +145,28 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase { players = serializePlayers(this.players), ) - is MafiaPhase.InferAnswer -> RedisMafiaPhase( + is MafiaPhase.InferAnswer -> MafiaPhaseRedisEntity( status = RedisMafiaPhaseStatus.INFER_ANSWER, turnList = turnList.map { player -> - RedisMafiaPlayer( + MafiaPlayerRedisEntity( id = player.userId.value, nickname = player.nickname, color = player.color, isConnect = player.isConnect(), ) }, - mafiaPlayer = RedisMafiaPlayer( + mafiaPlayer = MafiaPlayerRedisEntity( id = mafiaPlayer.userId.value, nickname = mafiaPlayer.nickname, color = mafiaPlayer.color, isConnect = mafiaPlayer.isConnect(), ), - keyword = RedisMafiaKeyword( + keyword = MafiaKeywordRedisEntity( answer = keyword.answer, category = keyword.category, ), drawData = drawData.map { pair -> - RedisDrawInfo( + DrawInfoRedisEntity( userId = pair.first.value, draw = pair.second, ) @@ -174,28 +174,28 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase { answer = answer, ) - is MafiaPhase.End -> RedisMafiaPhase( + is MafiaPhase.End -> MafiaPhaseRedisEntity( status = RedisMafiaPhaseStatus.END, turnList = turnList.map { player -> - RedisMafiaPlayer( + MafiaPlayerRedisEntity( id = player.userId.value, nickname = player.nickname, color = player.color, isConnect = player.isConnect(), ) }, - mafiaPlayer = RedisMafiaPlayer( + mafiaPlayer = MafiaPlayerRedisEntity( id = mafiaPlayer.userId.value, nickname = mafiaPlayer.nickname, color = mafiaPlayer.color, isConnect = mafiaPlayer.isConnect(), ), - keyword = RedisMafiaKeyword( + keyword = MafiaKeywordRedisEntity( answer = keyword.answer, category = keyword.category, ), drawData = drawData.map { pair -> - RedisDrawInfo( + DrawInfoRedisEntity( userId = pair.first.value, draw = pair.second, ) @@ -207,22 +207,22 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase { } } -fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) { +fun MafiaPhaseRedisEntity.toDomain(): MafiaPhase = when (status) { RedisMafiaPhaseStatus.WAIT -> MafiaPhase.Wait RedisMafiaPhaseStatus.READY -> MafiaPhase.Ready( turnList = turnList!!.map { player -> - player.toPlayer() + player.toDomain() }, - mafiaPlayer = mafiaPlayer!!.toPlayer(), - keyword = keyword!!.toMafiaKeyword(), + mafiaPlayer = mafiaPlayer!!.toDomain(), + keyword = keyword!!.toDomain(), ) RedisMafiaPhaseStatus.PLAYING -> MafiaPhase.Playing( turnList = turnList!!.map { player -> - player.toPlayer() + player.toDomain() }, - mafiaPlayer = mafiaPlayer!!.toPlayer(), - keyword = keyword!!.toMafiaKeyword(), + mafiaPlayer = mafiaPlayer!!.toDomain(), + keyword = keyword!!.toDomain(), turnInfo = TurnInfo(round!!, turn!!), drawData = drawData!!.map { item -> Pair(UserId(item.userId), item.draw) @@ -231,10 +231,10 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) { RedisMafiaPhaseStatus.VOTE -> MafiaPhase.Vote( turnList = turnList!!.map { player -> - player.toPlayer() + player.toDomain() }, - mafiaPlayer = mafiaPlayer!!.toPlayer(), - keyword = keyword!!.toMafiaKeyword(), + mafiaPlayer = mafiaPlayer!!.toDomain(), + keyword = keyword!!.toDomain(), drawData = drawData!!.map { item -> Pair(UserId(item.userId), item.draw) }.toMutableList(), @@ -243,10 +243,10 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) { RedisMafiaPhaseStatus.INFER_ANSWER -> MafiaPhase.InferAnswer( turnList = turnList!!.map { player -> - player.toPlayer() + player.toDomain() }, - mafiaPlayer = mafiaPlayer!!.toPlayer(), - keyword = keyword!!.toMafiaKeyword(), + mafiaPlayer = mafiaPlayer!!.toDomain(), + keyword = keyword!!.toDomain(), drawData = drawData!!.map { item -> Pair(UserId(item.userId), item.draw) }.toMutableList(), @@ -255,10 +255,10 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) { RedisMafiaPhaseStatus.END -> MafiaPhase.End( turnList = turnList!!.map { player -> - player.toPlayer() + player.toDomain() }, - mafiaPlayer = mafiaPlayer!!.toPlayer(), - keyword = keyword!!.toMafiaKeyword(), + mafiaPlayer = mafiaPlayer!!.toDomain(), + keyword = keyword!!.toDomain(), drawData = drawData!!.map { item -> Pair(UserId(item.userId), item.draw) }.toMutableList(), diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaPlayer.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaPlayerRedisEntity.kt similarity index 85% rename from adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaPlayer.kt rename to adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaPlayerRedisEntity.kt index 6943ce4c..8a05fe87 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaPlayer.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaPlayerRedisEntity.kt @@ -5,14 +5,14 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.xorker.draw.mafia.MafiaPlayer import com.xorker.draw.user.UserId -data class RedisMafiaPlayer @JsonCreator constructor( +data class MafiaPlayerRedisEntity @JsonCreator constructor( @JsonProperty("id") val id: Long, @JsonProperty("nickname") val nickname: String, @JsonProperty("color") val color: String, @JsonProperty("connect") val isConnect: Boolean, ) -fun RedisMafiaPlayer.toPlayer(): MafiaPlayer { +fun MafiaPlayerRedisEntity.toDomain(): MafiaPlayer { val mafiaPlayer = MafiaPlayer( userId = UserId(this.id), nickname = this.nickname, diff --git a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaRoom.kt b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaRoomRedisEntity.kt similarity index 67% rename from adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaRoom.kt rename to adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaRoomRedisEntity.kt index 70dd7386..74ddc3f0 100644 --- a/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/RedisMafiaRoom.kt +++ b/adapter/redis/src/main/kotlin/com/xorker/draw/mafia/dto/MafiaRoomRedisEntity.kt @@ -6,22 +6,22 @@ import com.xorker.draw.mafia.MafiaPlayer import com.xorker.draw.room.Room import com.xorker.draw.room.RoomId -data class RedisMafiaRoom @JsonCreator constructor( +data class MafiaRoomRedisEntity @JsonCreator constructor( @JsonProperty("id") val id: String, @JsonProperty("locale") val locale: String, - @JsonProperty("owner") val owner: RedisMafiaPlayer, + @JsonProperty("owner") val owner: MafiaPlayerRedisEntity, @JsonProperty("maxMemberNum") val maxMemberNum: Int, - @JsonProperty("players") val players: List, + @JsonProperty("players") val players: List, @JsonProperty("randomMatching") val isRandomMatching: Boolean, ) -fun RedisMafiaRoom.toRoom(): Room = Room( +fun MafiaRoomRedisEntity.toDomain(): Room = Room( id = RoomId(this.id), locale = this.locale, - owner = this.owner.toPlayer(), + owner = this.owner.toDomain(), maxMemberNum = this.maxMemberNum, players = this.players.map { player -> - player.toPlayer() + player.toDomain() }.toMutableList(), isRandomMatching = isRandomMatching, )