Skip to content

Commit

Permalink
[feat #23] 닉네임 중복 체크 API (#24)
Browse files Browse the repository at this point in the history
* feat : user 닉네임 필드

* feat : 닉네임 통해 데이터 존재 유무 체크 메소드

* feat : 닉네임 중복 체크 유스케이스 구현 및 테스트

* feat : 닉네임 중복 체크 API
  • Loading branch information
dlswns2480 authored Jul 19, 2024
1 parent 2295172 commit 4789d34
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 9 deletions.
17 changes: 12 additions & 5 deletions adapters/in-web/src/main/kotlin/com/pokit/user/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import com.pokit.auth.config.ErrorOperation
import com.pokit.auth.model.PrincipalUser
import com.pokit.auth.model.toDomain
import com.pokit.user.dto.ApiSignUpRequest
import com.pokit.user.dto.response.CheckDuplicateNicknameResponse
import com.pokit.user.dto.response.SignUpResponse
import com.pokit.user.exception.UserErrorCode
import com.pokit.user.model.User
import com.pokit.user.port.`in`.UserUseCase
import io.swagger.v3.oas.annotations.Operation
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
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
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/v1/user")
Expand All @@ -34,4 +31,14 @@ class UserController(
val response = userUseCase.signUp(user, signUpRequest)
return ResponseEntity.ok(response)
}

@GetMapping("/duplicate/{nickname}")
@Operation(summary = "닉네임 중복 체크 API")
@ErrorOperation(UserErrorCode::class)
fun checkNickname(
@AuthenticationPrincipal principalUser: PrincipalUser,
@PathVariable("nickname") nickname: String
): ResponseEntity<CheckDuplicateNicknameResponse> {
return ResponseEntity.ok(userUseCase.checkDuplicateNickname(nickname))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ class UserAdapter(
userEntity?.registerInfo(user)
return userEntity?.toDomain()
}

override fun checkByNickname(nickname: String) = userRepository.existsByNickname(nickname)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ class UserEntity(
val role: Role,

@Column(name = "nickname")
var nickName: String = email,
var nickname: String = email
) {
companion object {
fun of(user: User) =
UserEntity(
email = user.email,
role = user.role,
nickname = user.nickName
)
}
}

fun UserEntity.toDomain() = User(id = this.id, email = this.email, role = this.role)
fun UserEntity.toDomain() = User(
id = this.id,
email = this.email,
role = this.role,
nickName = this.nickname
)

fun UserEntity.registerInfo(user: User) {
this.nickName = user.nickName
this.nickname = user.nickName
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository

interface UserRepository : JpaRepository<UserEntity, Long> {
fun findByEmail(email: String): UserEntity?

fun existsByNickname(nickname: String): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.pokit.user.port.`in`

import com.pokit.user.dto.request.SignUpRequest
import com.pokit.user.dto.response.CheckDuplicateNicknameResponse
import com.pokit.user.dto.response.SignUpResponse
import com.pokit.user.model.User

interface UserUseCase {
fun signUp(user: User, request: SignUpRequest): SignUpResponse

fun checkDuplicateNickname(nickname: String): CheckDuplicateNicknameResponse

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ interface UserPort {
fun loadById(id: Long): User?

fun register(user: User): User?

fun checkByNickname(nickname: String): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pokit.user.port.service

import com.pokit.common.exception.NotFoundCustomException
import com.pokit.user.dto.request.SignUpRequest
import com.pokit.user.dto.response.CheckDuplicateNicknameResponse
import com.pokit.user.dto.response.SignUpResponse
import com.pokit.user.exception.UserErrorCode
import com.pokit.user.model.User
Expand All @@ -26,4 +27,10 @@ class UserService(

return SignUpResponse(savedUser.id)
}

override fun checkDuplicateNickname(nickname: String)
: CheckDuplicateNicknameResponse {
val isDuplicate = userPort.checkByNickname(nickname)
return CheckDuplicateNicknameResponse(isDuplicate)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,27 @@ class UserServiceTest : BehaviorSpec({
}
}
}
}

Given("닉네임 중복 체크 시") {
val notDuplicateName = "injun"
val duplicateName = "jimin"

}
every { userPort.checkByNickname(notDuplicateName) } returns false
every { userPort.checkByNickname(duplicateName) } returns true

When("해당 닉네임의 유저가 존재하지 않으면") {
val response = userService.checkDuplicateNickname(notDuplicateName)
Then("false가 반환된다") {
response.isDuplicate shouldBe false
}
}

When("해당 닉네임의 유저가 존재하면 ") {
val response = userService.checkDuplicateNickname(duplicateName)
Then("true가 반환된다.") {
response.isDuplicate shouldBe true
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pokit.user.dto.response

data class CheckDuplicateNicknameResponse(
val isDuplicate: Boolean
)

0 comments on commit 4789d34

Please sign in to comment.