-
Notifications
You must be signed in to change notification settings - Fork 1
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
Client로직 추가 #20
Changes from 10 commits
29cfeca
07f3746
aade2c7
7db27e5
9db1721
eac967a
85369d8
654bb25
60c161b
9aaa560
e55a89b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.ok().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( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
으악 린트 불편해용 |
||||||
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, | ||
){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Transactional(readOnly = true) 어떰 |
||
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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.msg.gauth.global.util | ||
|
||
import com.msg.gauth.domain.user.User | ||
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 | ||
){ | ||
baekteun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private fun getCurrentEmail():String{ | ||
baekteun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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())!! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 언래핑보다 throw 처리하자 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no content어땨