Skip to content

Commit

Permalink
✨ :: Implementation Adapter ^.^
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSu1768 committed Nov 28, 2023
1 parent 0e4f5a2 commit 24aad6c
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package com.info.maeumgagym.domain.user

import com.info.common.PersistenceAdapter
import com.info.maeumgagym.domain.user.exception.UserNotFoundException
import com.info.maeumgagym.domain.user.mapper.UserMapper
import com.info.maeumgagym.domain.user.repository.UserRepository
import com.info.maeumgagym.user.model.User
import com.info.maeumgagym.user.port.out.CreateUserPort
import com.info.maeumgagym.user.port.out.FindUserByOAuthIdPort
import com.info.maeumgagym.user.port.out.FindUserPort
import org.springframework.data.repository.findByIdOrNull
import java.util.*

@PersistenceAdapter
class UserPersistenceAdapter() : FindUserPort, CreateUserPort, FindUserByOAuthIdPort {
class UserPersistenceAdapter(
private val userRepository: UserRepository,
private val userMapper: UserMapper
) : FindUserPort, CreateUserPort, FindUserByOAuthIdPort {
override fun findUserById(userId: UUID): User {
TODO("Not yet implemented")
val user = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException
return userMapper.toDomain(user)
}

override fun createUser(user: User): User {
TODO("Not yet implemented")
val userJpaEntity = userRepository.save(userMapper.toEntity(user))
return userMapper.toDomain(userJpaEntity)
}

override fun findUserByOAuthId(oauthId: String): User? {
TODO("Not yet implemented")
}
override fun findUserByOAuthId(oauthId: String): User? =
userRepository.findByOauthId(oauthId)?.let { userMapper.toDomain(it) }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.info.maeumgagym.domain.user.converter

import com.info.maeumgagym.domain.user.exception.RoleConverterException
import com.info.maeumgagym.domain.user.role.Role
import com.info.maeumgagym.user.model.Role
import javax.persistence.AttributeConverter
import javax.persistence.Converter

Expand All @@ -12,8 +12,9 @@ class RoleConverter : AttributeConverter<MutableList<Role>, String> {
const val SPLIT_CHAR = ","
}

override fun convertToDatabaseColumn(attribute: MutableList<Role>): String =
String.format(SPLIT_CHAR, attribute)
override fun convertToDatabaseColumn(attribute: MutableList<Role>): String {
return attribute.joinToString(SPLIT_CHAR)
}

override fun convertToEntityAttribute(dbData: String): MutableList<Role> =
dbData.split(SPLIT_CHAR).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ package com.info.maeumgagym.domain.user.entity
import com.info.maeumgagym.TableNames
import com.info.maeumgagym.domain.base.BaseUUIDEntity
import com.info.maeumgagym.domain.user.converter.RoleConverter
import com.info.maeumgagym.domain.user.role.Role
import com.info.maeumgagym.user.model.Role
import org.hibernate.annotations.SQLDelete
import org.hibernate.annotations.Where
import java.util.*
import javax.persistence.Column
import javax.persistence.Convert
import javax.persistence.Entity

@Where(clause = "is_deleted = false")
@SQLDelete(sql = "UPDATE ${TableNames.USER_TABLE} SET is_deleted = true WHERE id = ?")
@Entity(name = TableNames.USER_TABLE)
class User(
name: String,
oauthId: String
) : BaseUUIDEntity() {
class UserJpaEntity(
id: UUID?,
nickname: String,
oauthId: String,
roles: MutableList<Role>
) : BaseUUIDEntity(id) {

@Column(name = "nickname", nullable = false, length = 10, unique = true)
var nickname: String = name
var nickname: String = nickname
protected set

fun updateNickname(nickname: String) {
Expand All @@ -43,6 +46,6 @@ class User(

@Convert(converter = RoleConverter::class)
@Column(name = "roles", length = 15)
var roles: MutableList<Role> = mutableListOf()
var roles: MutableList<Role> = roles
protected set
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.info.maeumgagym.domain.user.mapper

import com.info.maeumgagym.domain.user.entity.UserJpaEntity
import com.info.maeumgagym.user.model.User
import org.springframework.stereotype.Component

@Component
class UserMapper {
fun toEntity(user: User): UserJpaEntity {
return UserJpaEntity(
id = user.id,
nickname = user.nickname,
oauthId = user.oauthId,
roles = user.roles
)
}

fun toDomain(userJpaEntity: UserJpaEntity): User {
return User(
id = userJpaEntity.id!!,
nickname = userJpaEntity.nickname,
oauthId = userJpaEntity.oauthId,
roles = userJpaEntity.roles
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.info.maeumgagym.auth.dto.response

data class KakaoInfoResponse(
val oauthId: String,
val id: String,
val properties: Properties
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class KakaoLoginService(
) : KakaoLoginUseCase {
override fun login(accessToken: String): TokenResponse {
val userInfo = getKakaoInfoPort.getInfo(accessToken)
findUserByOAuthIdPort.findUserByOAuthId(userInfo.oauthId)?.let {
findUserByOAuthIdPort.findUserByOAuthId(userInfo.id)?.let {
return generateJwtPort.generateToken(it.id)
}
val user = createUserPort.createUser(
User(
nickname = userInfo.properties.nickname,
roles = mutableListOf(Role.USER)
roles = mutableListOf(Role.USER, Role.ADMIN),
oauthId = userInfo.id
)
)
return generateJwtPort.generateToken(user.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import java.util.UUID
data class User(
val id: UUID = UUID(0, 0),
val nickname: String,
val roles: MutableList<Role> = mutableListOf()
val roles: MutableList<Role> = mutableListOf(),
val oauthId: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ class KakaoAuthAdapter(
)
}

override fun getInfo(accessToken: String): KakaoInfoResponse = kakaoInfoClient.kakaoInfo(accessToken)
override fun getInfo(accessToken: String): KakaoInfoResponse = kakaoInfoClient.kakaoInfo("Bearer $accessToken")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.info.maeumgagym.global.config.feign.FeignConfig
import com.info.maeumgagym.auth.dto.response.KakaoInfoResponse
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestParam

@FeignClient(
Expand All @@ -13,5 +14,5 @@ import org.springframework.web.bind.annotation.RequestParam
)
interface KakaoInfoClient {
@PostMapping("/v2/user/me")
fun kakaoInfo(@RequestParam("Authorization") accessToken: String): KakaoInfoResponse
fun kakaoInfo(@RequestHeader("Authorization") accessToken: String): KakaoInfoResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SecurityConfig(
.requestMatchers(CorsUtils::isCorsRequest)
.permitAll()
.antMatchers("/oauth/google/**").permitAll()
.antMatchers("/app/login/kakao").permitAll()
.anyRequest()
.authenticated()

Expand Down

0 comments on commit 24aad6c

Please sign in to comment.