Skip to content

Commit

Permalink
feat: Point 조회 API 추가 및 BadRequest 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jhhong0509 committed Sep 4, 2022
1 parent 4361bef commit 156fed0
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class User(

val grade: Int,

val profileFileName: String,
val profileFileName: String?,

val password: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xquare.v1userservice.user.api

import com.xquare.v1userservice.user.User
import com.xquare.v1userservice.user.api.dtos.CreatUserDomainRequest
import com.xquare.v1userservice.user.api.dtos.PointDomainResponse
import com.xquare.v1userservice.user.api.dtos.SignInDomainRequest
import com.xquare.v1userservice.user.api.dtos.TokenResponse
import com.xquare.v1userservice.user.api.dtos.UserDeviceTokenResponse
Expand All @@ -14,4 +15,5 @@ interface UserApi {
suspend fun userSignIn(signInDomainRequest: SignInDomainRequest): TokenResponse
suspend fun userTokenRefresh(refreshToken: String): TokenResponse
suspend fun getUserDeviceTokensByIdIn(idList: List<UUID>): UserDeviceTokenResponse
suspend fun getUserPointInformation(userId: UUID): PointDomainResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data class CreatUserDomainRequest(

val verificationCode: String,

val profileFileName: String,
val profileFileName: String?,

val password: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.xquare.v1userservice.user.api.dtos

data class PointDomainResponse(
val userName: String,
val goodPoint: Int,
val badPoint: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.xquare.v1userservice.user.api.CreateUserInPendingStateProcessor
import com.xquare.v1userservice.user.api.UpdateUserCreatedStateStepProcessor
import com.xquare.v1userservice.user.api.UserApi
import com.xquare.v1userservice.user.api.dtos.CreatUserDomainRequest
import com.xquare.v1userservice.user.api.dtos.PointDomainResponse
import com.xquare.v1userservice.user.api.dtos.SignInDomainRequest
import com.xquare.v1userservice.user.api.dtos.TokenResponse
import com.xquare.v1userservice.user.api.dtos.UserDeviceTokenResponse
Expand All @@ -23,6 +24,7 @@ import com.xquare.v1userservice.user.spi.AuthorityListSpi
import com.xquare.v1userservice.user.spi.JwtTokenGeneratorSpi
import com.xquare.v1userservice.user.spi.PasswordEncoderSpi
import com.xquare.v1userservice.user.spi.PasswordMatcherSpi
import com.xquare.v1userservice.user.spi.PointSpi
import com.xquare.v1userservice.user.spi.SaveUserBaseAuthorityCompensator
import com.xquare.v1userservice.user.spi.SaveUserBaseAuthorityProcessor
import com.xquare.v1userservice.user.spi.UserRepositorySpi
Expand All @@ -47,7 +49,8 @@ class UserApiImpl(
private val jwtTokenGeneratorSpi: JwtTokenGeneratorSpi,
private val refreshTokenSpi: RefreshTokenSpi,
private val authorityListSpi: AuthorityListSpi,
private val passwordMatcherSpi: PasswordMatcherSpi
private val passwordMatcherSpi: PasswordMatcherSpi,
private val pointSpi: PointSpi
) : UserApi {
override suspend fun saveUser(creatUserDomainRequest: CreatUserDomainRequest): User {
val verificationCode = verificationCodeSpi.getByCode(creatUserDomainRequest.verificationCode)
Expand Down Expand Up @@ -193,4 +196,17 @@ class UserApiImpl(

return refreshTokenSpi.saveRefreshToken(refreshTokenDomain)
}

override suspend fun getUserPointInformation(userId: UUID): PointDomainResponse {
val user = userRepositorySpi.findByIdAndStateWithCreated(userId)
?: throw UserNotFoundException(UserNotFoundException.USER_ID_NOT_FOUND)

val userPoint = pointSpi.getUserPoint(userId)

return PointDomainResponse(
goodPoint = userPoint.goodPoint,
badPoint = userPoint.badPoint,
userName = user.name
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.xquare.v1userservice.user.spi

import com.xquare.v1userservice.annotations.Spi
import com.xquare.v1userservice.user.spi.dtos.PointResponse
import java.util.UUID

@Spi
interface PointSpi {
suspend fun getUserPoint(userId: UUID): PointResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.xquare.v1userservice.user.spi.dtos

data class PointResponse(
val goodPoint: Int,
val badPoint: Int
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.xquare.v1userservice.authority
package com.xquare.v1userservice.configuration.property

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
@ConfigurationProperties(prefix = "service.authority")
class AuthorityProperties(
val host: String
@ConfigurationProperties(prefix = "service")
class ServiceProperties(
val baseHost: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.xquare.v1userservice.user.exceptions

import com.xquare.v1userservice.exceptions.BaseException

class PointRequestFailedException(
message: String,
statusCode: Int
) : BaseException(message, statusCode)
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.xquare.v1userservice.configuration.validate.RequestBodyValidator
import com.xquare.v1userservice.user.User
import com.xquare.v1userservice.user.api.UserApi
import com.xquare.v1userservice.user.api.dtos.CreatUserDomainRequest
import com.xquare.v1userservice.user.api.dtos.PointDomainResponse
import com.xquare.v1userservice.user.api.dtos.SignInDomainRequest
import com.xquare.v1userservice.user.api.dtos.UserDeviceTokenResponse
import com.xquare.v1userservice.user.router.dto.CreateUserRequest
import com.xquare.v1userservice.user.router.dto.GetUserDeviceTokenListResponse
import com.xquare.v1userservice.user.router.dto.GetUserPointResponse
import com.xquare.v1userservice.user.router.dto.GetUserResponse
import com.xquare.v1userservice.user.router.dto.SignInRequest
import java.net.URI
Expand Down Expand Up @@ -109,4 +111,17 @@ class UserHandler(
private fun UserDeviceTokenResponse.toGetUserDeviceTokenListResponse() = GetUserDeviceTokenListResponse(
tokens = this.tokens
)

suspend fun getUserPointHandler(serverRequest: ServerRequest): ServerResponse {
val userId = serverRequest.pathVariable("userId")
val pointDomainResponse = userApi.getUserPointInformation(UUID.fromString(userId))
val pointResponse = pointDomainResponse.toResponse()
return ServerResponse.ok().bodyValueAndAwait(pointResponse)
}

private fun PointDomainResponse.toResponse() = GetUserPointResponse(
name = this.userName,
goodPoint = this.goodPoint,
badPoint = this.badPoint
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UserRouter {
GET("/id/{userId}", userHandler::getUserByIdHandler)
GET("/account-id/{accountId}", userHandler::getUserByAccountIdHandler)
GET("/tokens/fcm", userHandler::getUserDeviceTokensHandler)
GET("/points", userHandler::getUserPointHandler)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class CreateUserRequest(
@field:NotBlank
val verificationCode: String,

val profileFileName: String,
val profileFileName: String?,

@field:NotNull
val password: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.xquare.v1userservice.user.router.dto

data class GetUserPointResponse(
val name: String,
val goodPoint: Int,
val badPoint: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class GetUserResponse(

val num: Int,

val profileFileName: String,
val profileFileName: String?,

val password: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xquare.v1userservice.user.spi

import com.xquare.v1userservice.authority.AuthorityProperties
import com.xquare.v1userservice.configuration.property.ServiceProperties
import com.xquare.v1userservice.user.exceptions.AuthorityRequestFailedException
import com.xquare.v1userservice.user.spi.dtos.AuthorityListResponse
import java.util.UUID
Expand All @@ -12,7 +12,7 @@ import org.springframework.web.reactive.function.client.awaitBody
@Repository
class AuthorityListSpiImpl(
private val webClient: WebClient,
private val authorityProperties: AuthorityProperties
private val serviceProperties: ServiceProperties
) : AuthorityListSpi {
override suspend fun getAuthorities(userId: UUID): List<String> {
val clientResponse = sendGetAuthoritiesRequest(userId)
Expand All @@ -22,7 +22,7 @@ class AuthorityListSpiImpl(
private suspend fun sendGetAuthoritiesRequest(userId: UUID): WebClient.ResponseSpec {
return webClient.get().uri { uri ->
uri.scheme("http")
.host(authorityProperties.host)
.host(serviceProperties.baseHost)
.path("/authorities/{userId}")
.build(userId)
}.retrieve()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.xquare.v1userservice.user.spi

import com.xquare.v1userservice.configuration.property.ServiceProperties
import com.xquare.v1userservice.user.exceptions.PointRequestFailedException
import com.xquare.v1userservice.user.spi.dtos.PointResponse
import com.xquare.v1userservice.user.spi.dtos.UserPointResponse
import java.util.UUID
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Repository
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.awaitBody

@Repository
class PointSpiImpl(
private val webClient: WebClient,
private val serviceProperties: ServiceProperties
) : PointSpi {
override suspend fun getUserPoint(userId: UUID): PointResponse {
return webClient.get().uri {
it.scheme("https")
.host(serviceProperties.baseHost)
.path("/point/{userId}")
.build(userId)
}.retrieve()
.onStatus(HttpStatus::isError) {
throw PointRequestFailedException("Failed request to get user point", it.rawStatusCode())
}
.awaitBody<UserPointResponse>().let {
PointResponse(
goodPoint = it.goodPoint,
badPoint = it.badPoint
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xquare.v1userservice.user.spi

import com.xquare.v1userservice.application.properties.ApplicationProperties
import com.xquare.v1userservice.configuration.property.ServiceProperties
import com.xquare.v1userservice.user.exceptions.ApplicationRequestFailedException
import com.xquare.v1userservice.user.spi.dtos.SaveUserBaseApplicationRequest
import java.util.UUID
Expand All @@ -12,7 +12,7 @@ import org.springframework.web.reactive.function.client.awaitBodilessEntity
@Repository
class SaveUserBaseApplicationSpiImpl(
private val webClient: WebClient,
private val applicationProperties: ApplicationProperties
private val serviceProperties: ServiceProperties
) : SaveUserBaseApplicationProcessor, SaveUserBaseApplicationCompensator {
override suspend fun processStep(userId: UUID) {
val baseApplicationRequest = buildBaseApplicationRequest(userId)
Expand All @@ -27,7 +27,7 @@ class SaveUserBaseApplicationSpiImpl(
webClient.post()
.uri {
it.scheme("http")
.host(applicationProperties.host)
.host(serviceProperties.baseHost)
.path("/applications/signup")
.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xquare.v1userservice.user.spi

import com.xquare.v1userservice.authority.AuthorityProperties
import com.xquare.v1userservice.configuration.property.ServiceProperties
import com.xquare.v1userservice.user.exceptions.AuthorityRequestFailedException
import com.xquare.v1userservice.user.spi.dtos.SaveUserBaseAuthorityRequest
import java.util.UUID
Expand All @@ -13,7 +13,7 @@ import org.springframework.web.reactive.function.client.awaitBodilessEntity
@Repository
class SaveUserBaseAuthoritySpiImpl(
private val webClient: WebClient,
private val authorityProperties: AuthorityProperties,
private val serviceProperties: ServiceProperties,
) : SaveUserBaseAuthorityProcessor, SaveUserBaseAuthorityCompensator {
override suspend fun processStep(userId: UUID) {
val request = SaveUserBaseAuthorityRequest(userId)
Expand All @@ -24,7 +24,7 @@ class SaveUserBaseAuthoritySpiImpl(
webClient.post()
.uri {
it.scheme("http")
.host(authorityProperties.host)
.host(serviceProperties.baseHost)
.path("/authorities/access-management/basic")
.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.xquare.v1userservice.user.spi.dtos

data class UserPointResponse(
val goodPoint: Int,
val badPoint: Int
)

0 comments on commit 156fed0

Please sign in to comment.