Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client로직 추가 #20

Merged
merged 11 commits into from
Sep 21, 2022
15 changes: 11 additions & 4 deletions src/main/kotlin/com/msg/gauth/domain/client/Client.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.msg.gauth.domain.client

import com.msg.gauth.domain.client.persentation.dto.request.ClientUpdateReqDto
import com.msg.gauth.domain.user.User
import com.msg.gauth.global.entity.BaseIdEntity
import javax.persistence.*
Expand All @@ -8,11 +9,17 @@ import javax.persistence.*
class Client(
val clientId: String,
val clientSecret: String,
val redirectUri: String,
val serviceName: String,
val serviceUri: String,
var redirectUri: String,
var serviceName: String,
var serviceUri: String,

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
val createdBy: User
): BaseIdEntity()
): BaseIdEntity() {
fun update(clientUpdateReqDto: ClientUpdateReqDto){
this.redirectUri = clientUpdateReqDto.redirectUri
this.serviceName = clientUpdateReqDto.serviceName
this.serviceUri = clientUpdateReqDto.serviceUri
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.msg.gauth.domain.client.exception

import com.msg.gauth.global.exception.ErrorCode
import com.msg.gauth.global.exception.exceptions.BasicException

class ClientNotFindException: BasicException(ErrorCode.CLIENT_NOT_FOUND)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.msg.gauth.domain.client.persentation

import com.msg.gauth.domain.client.persentation.dto.request.ClientRegisterReqDto
import com.msg.gauth.domain.client.persentation.dto.request.ClientUpdateReqDto
import com.msg.gauth.domain.client.persentation.dto.response.ClientAllResDto
import com.msg.gauth.domain.client.persentation.dto.response.ClientOneResDto
import com.msg.gauth.domain.client.persentation.dto.response.ClientRegisterResDto
import com.msg.gauth.domain.client.services.GetAllClientsService
import com.msg.gauth.domain.client.services.GetOneClientService
import com.msg.gauth.domain.client.services.RegisterClientService
import com.msg.gauth.domain.client.services.UpdateClientService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/client")
class ClientController(
private val getAllClientsService: GetAllClientsService,
private val updateClientService: UpdateClientService,
private val getOneClientService: GetOneClientService,
private val registerClientService: RegisterClientService,
) {

@PostMapping
fun registerClient(@RequestBody clientRegisterReqDto: ClientRegisterReqDto):ResponseEntity<ClientRegisterResDto>{
val clientRegisterResDto = registerClientService.execute(clientRegisterReqDto)
return ResponseEntity.ok(clientRegisterResDto)
}

@GetMapping
fun getMyAllClients(): ResponseEntity<List<ClientAllResDto>>{
val result = getAllClientsService.execute()
return ResponseEntity.ok(result)
}

@GetMapping("/{id}")
fun getMyOneClient(@PathVariable id: String): ResponseEntity<ClientOneResDto>{
val clientOneResDto = getOneClientService.execute(id)
return ResponseEntity.ok(clientOneResDto)
}

@PatchMapping("/{id}")
fun updateClient(@PathVariable id: String, @RequestBody clientUpdateDto: ClientUpdateReqDto): ResponseEntity<Void>{
updateClientService.updateClient(id, clientUpdateDto)
return ResponseEntity.noContent().build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.msg.gauth.domain.client.persentation.dto.request

import com.msg.gauth.domain.client.Client
import com.msg.gauth.domain.user.User

data class ClientRegisterReqDto(
val serviceName: String,
val serviceUri: String,
val redirectUri: String,
){
fun toEntity(user: User, clientSecret: String, clientId: String): Client =
Client(
clientId = clientId,
clientSecret = clientSecret,
serviceName = serviceName,
serviceUri = serviceUri,
redirectUri = redirectUri,
createdBy = user,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.msg.gauth.domain.client.persentation.dto.request

data class ClientUpdateReqDto(
val serviceName: String,
val serviceUri: String,
val redirectUri: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.msg.gauth.domain.client.persentation.dto.response

import com.msg.gauth.domain.client.Client

data class ClientAllResDto(
val clientId: String,
val serviceName: String,
val serviceUri: String,
) {
constructor(client: Client): this(
clientId = client.clientId,
serviceName = client.serviceName,
serviceUri = client.serviceUri,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.msg.gauth.domain.client.persentation.dto.response

import com.msg.gauth.domain.client.Client

data class ClientOneResDto(
val clientId: String,
val clientSecret: String,
val redirectUri: String,
val serviceName: String,
val serviceUri: String,
){
constructor(client: Client): this(
clientId = client.clientId,
clientSecret = client.clientSecret,
redirectUri = client.redirectUri,
serviceName = client.serviceName,
serviceUri = client.serviceUri,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.msg.gauth.domain.client.persentation.dto.response

import com.msg.gauth.domain.client.Client

data class ClientRegisterResDto(
val clientId: String,
val clientSecret: String,
val redirectUri: String,
val serviceName: String,
val serviceUri: String,
) {
constructor(client: Client): this(
clientId = client.clientId,
clientSecret = client.clientSecret,
redirectUri = client.redirectUri,
serviceName = client.serviceName,
serviceUri = client.serviceUri,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.msg.gauth.domain.client.repository

import com.msg.gauth.domain.client.Client
import com.msg.gauth.domain.user.User
import org.springframework.data.jpa.repository.JpaRepository

interface ClientRepository: JpaRepository<Client, Long> {
fun findAllByCreatedBy(createdBy: User): List<Client>
fun findByClientIdAndCreatedBy(clientId: String, createdBy: User): Client?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.msg.gauth.domain.client.services

import com.msg.gauth.domain.client.persentation.dto.response.ClientAllResDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.global.util.CurrentUserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class GetAllClientsService(
private val clientRepository: ClientRepository,
private val currentUserUtil: CurrentUserUtil,
) {
fun execute(): List<ClientAllResDto> =
clientRepository.findAllByCreatedBy(currentUserUtil.getCurrentUser())
.map { ClientAllResDto(it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.msg.gauth.domain.client.services

import com.msg.gauth.domain.client.exception.ClientNotFindException
import com.msg.gauth.domain.client.persentation.dto.response.ClientOneResDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.global.util.CurrentUserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class GetOneClientService(
private val clientRepository: ClientRepository,
private val currentUserUtil: CurrentUserUtil,
) {
fun execute(clientId: String): ClientOneResDto{
val client = clientRepository.findByClientIdAndCreatedBy(clientId, currentUserUtil.getCurrentUser()) ?: throw ClientNotFindException()
return ClientOneResDto(client)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.msg.gauth.domain.client.services

import com.msg.gauth.domain.client.persentation.dto.request.ClientRegisterReqDto
import com.msg.gauth.domain.client.persentation.dto.response.ClientRegisterResDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.global.util.CurrentUserUtil
import org.springframework.stereotype.Service
import java.util.UUID

@Service
class RegisterClientService(
private val clientRepository: ClientRepository,
private val currentUserUtil: CurrentUserUtil,
) {
fun execute(clientRegisterDto: ClientRegisterReqDto): ClientRegisterResDto {
val clientSecret = createUUID()
val clientId = createUUID()
val user = currentUserUtil.getCurrentUser()
val client = clientRegisterDto.toEntity(user, clientSecret, clientId)
return ClientRegisterResDto(clientRepository.save(client))
}

private fun createUUID() = (UUID.randomUUID().toString() + UUID.randomUUID().toString()).replace("-", "")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.msg.gauth.domain.client.services

import com.msg.gauth.domain.client.exception.ClientNotFindException
import com.msg.gauth.domain.client.persentation.dto.request.ClientUpdateReqDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.global.util.CurrentUserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class UpdateClientService(
private val clientRepository: ClientRepository,
private val currentUserUtil: CurrentUserUtil,
) {
@Transactional
fun updateClient(clientId: String, clientUpdateReqDto: ClientUpdateReqDto){
val client = clientRepository.findByClientIdAndCreatedBy(clientId, currentUserUtil.getCurrentUser()) ?: throw ClientNotFindException()
client.update(clientUpdateReqDto)
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/com/msg/gauth/global/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ enum class ErrorCode(
EXPIRED_REFRESH_TOKEN("리프레시 토큰이 만료되었습니다", 401),
INVALID_REFRESH_TOKEN("리프레시 토큰이 변질되었습니다", 401),
EMAIL_NOT_VERIFIED("인증된 이메일이 아닙니다.", 401),
USER_NOT_SAME("유저가 일치하지 않습니다", 401),

NOT_FOUND("리소스를 찾을수 없음", 404),
USER_NOT_FOUND("해당 유저를 찾을 수 없습니다.", 404),
CLIENT_NOT_FOUND("해당 클라이언트를 찾을 수 없습니다.", 404),

DUPLICATE_EMAIL("중복되는 이메일입니다.", 409),

MANY_REQUEST_EMAIL_AUTH("15분에 최대 3번 이메일 인증을 요청할 수 있습니다.", 429),

MAIL_SEND_FAIL("메일을 보내는데 실패했습니다.", 500),
INTERNAL_SERVER_ERROR("서버 내부 에러", 500)

;
}
28 changes: 28 additions & 0 deletions src/main/kotlin/com/msg/gauth/global/util/CurrentUserUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.msg.gauth.global.util

import com.msg.gauth.domain.user.User
import com.msg.gauth.domain.user.exception.UserNotFoundException
import com.msg.gauth.domain.user.repository.UserRepository
import com.msg.gauth.global.security.auth.AuthDetails
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.stereotype.Component

@Component
class CurrentUserUtil(
private val userRepository: UserRepository
) {
private fun getCurrentEmail(): String{
val principal = SecurityContextHolder.getContext().authentication.principal
val email: String =
if(principal is UserDetails){
(principal as AuthDetails).username
}else{
principal.toString()
}
return email
}

fun getCurrentUser(): User =
userRepository.findByEmail(getCurrentEmail())?: throw UserNotFoundException()
}