Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
[#28] authCode 복합키로 설정
Browse files Browse the repository at this point in the history
- 복합키 미지원 확인 spring-projects/spring-data-relational#574
  • Loading branch information
youngvly committed Nov 14, 2021
1 parent 4456124 commit 5a5fc81
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service

@Service
class AuthCodeCommand(
class AuthCodeCommandService(
val authCodeRepository: AuthCodeRepository,
val mailService: MailService,
val userRepository: DefaultUserRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import org.springframework.jdbc.core.RowMapper

data class AuthCode(
@Id
val id: Int? = null,
// TODO 복합키 구현가능한지 확인.
@NotBlank
val purpose: Purpose,
@Id
@NotBlank
val userId: String,
@NotBlank
Expand All @@ -40,7 +39,6 @@ data class AuthCode(
class AuthCodeRowMapper : RowMapper<AuthCode> {
override fun mapRow(rs: ResultSet, rowNum: Int): AuthCode? {
return AuthCode(
id = rs.getInt("id"),
purpose = Purpose.valueOf(rs.getString("purpose")),
userId = rs.getString("user_id"),
code = rs.getString("code"),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.sns.user.component.authcode.repositories

import com.sns.user.component.authcode.domain.AuthCode
import com.sns.user.component.authcode.domain.Purpose
import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.NoRepositoryBean

@NoRepositoryBean
interface AuthCodeRepository : CrudRepository<AuthCode, Int> {
interface AuthCodeRepository {
fun save(authCode: AuthCode): AuthCode
fun findByUserIdAndPurpose(userId: String, purpose: Purpose): AuthCode?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,40 @@ package com.sns.user.component.authcode.repositories

import com.sns.user.component.authcode.domain.AuthCode
import com.sns.user.component.authcode.domain.Purpose
import org.springframework.data.repository.CrudRepository
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import org.springframework.stereotype.Repository

@Repository
class DefaultAuthCodeRepository(
val jdbcTemplate: JdbcTemplate,
val authCodeCrudRepository: AuthCodeCrudRepository
) : AuthCodeRepository, CrudRepository<AuthCode, Int> by authCodeCrudRepository {
val jdbcTemplate: NamedParameterJdbcTemplate,
) : AuthCodeRepository {

override fun findByUserIdAndPurpose(userId: String, purpose: Purpose): AuthCode? = jdbcTemplate.queryForObject(
"""
SELECT id,user_id,`code`,created_at,purpose
FROM auth_code
WHERE user_id = ? AND purpose = ?
ORDER BY id DESC
SELECT user_id,`code`,created_at,purpose
FROM auth_code
WHERE user_id = :userId AND purpose = :purpose
LIMIT 1
""".trimIndent(),
AuthCode.MAPPER, userId, purpose.name,
mutableMapOf(
"userId" to userId,
"purpose" to purpose.name,
),
AuthCode.MAPPER,
)

override fun save(authCode: AuthCode): AuthCode {
jdbcTemplate.update(
"""
REPLACE INTO auth_code (user_id, `code`, created_at, purpose)
VALUES (:userId, :code, NOW(), :purpose)
""".trimIndent(),
mutableMapOf(
"userId" to authCode.userId,
"purpose" to authCode.purpose.name,
"code" to authCode.code,
),
)
return authCode
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.sns.user.component.user.listeners

import com.sns.commons.annotation.CustomEventListener
import com.sns.user.component.authcode.application.AuthCodeCommand
import com.sns.user.component.authcode.application.AuthCodeCommandService
import com.sns.user.component.user.events.UserActivatedEvent
import com.sns.user.component.user.events.UserCreatedEvent

@CustomEventListener
class UserStatusListener(val authCodeCommand: AuthCodeCommand) {
class UserStatusListener(val authCodeCommandService: AuthCodeCommandService) {
// 인증 전, 기초 가입만 마친 상태
fun onCreated(createdEvent: UserCreatedEvent) {
val user = createdEvent.user
authCodeCommand.create(user.id)
authCodeCommandService.create(user.id)
}

fun onActivated(activatedEvent: UserActivatedEvent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sns.user.endpoints.user

import com.sns.commons.utils.ifTrue
import com.sns.user.component.authcode.application.AuthCodeCommand
import com.sns.user.component.authcode.application.AuthCodeCommandService
import com.sns.user.component.authcode.domain.Purpose
import com.sns.user.component.user.application.UserCommandService
import com.sns.user.component.user.application.UserQueryService
Expand Down Expand Up @@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController
@Tag(name = SwaggerTag.SIGN_UP)
@RequestMapping("/api")
class SignUpController(
val authCodeCommand: AuthCodeCommand,
val authCodeCommandService: AuthCodeCommandService,
val userQueryService: UserQueryService,
val userCommandService: UserCommandService
) {
Expand All @@ -56,7 +56,7 @@ class SignUpController(
@ResponseStatus(HttpStatus.CREATED)
@PutMapping("/v1/sign-up/verifications/auth-code/ids/{userId}")
fun createAuthenticationCode(@PathVariable userId: String) {
authCodeCommand.create(userId)
authCodeCommandService.create(userId)
}

@ApiResponse(
Expand All @@ -66,7 +66,7 @@ class SignUpController(
@ResponseStatus(HttpStatus.OK)
@PostMapping("/v1/sign-up/verifications/auth-code/ids/{userId}")
fun verifyAuthenticationCode(@PathVariable userId: String, @RequestBody code: String): ResponseEntity<Boolean> {
return authCodeCommand.verify(userId, Purpose.SIGN_UP, code)
return authCodeCommandService.verify(userId, Purpose.SIGN_UP, code)
.ifTrue { userCommandService.activate(userId) }
.let {
ResponseEntity.ok(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.data.repository.findByIdOrNull

class AuthCodeCommandMockTest() {
class AuthCodeCommandServiceMockTest() {
@MockK
private lateinit var authCodeRepository: AuthCodeRepository

Expand All @@ -28,7 +28,7 @@ class AuthCodeCommandMockTest() {
private lateinit var userRepository: DefaultUserRepository

@InjectMockKs
private lateinit var authCodeCommand: AuthCodeCommand
private lateinit var authCodeCommandService: AuthCodeCommandService

@BeforeEach
internal fun setUp() {
Expand All @@ -40,7 +40,7 @@ class AuthCodeCommandMockTest() {

@Test
fun create() {
val authCode = authCodeCommand.create("id")
val authCode = authCodeCommandService.create("id")

verify { authCodeRepository.save(eq(authCode)) }
verify { mailService.sendSignUpAuthCodeMail(any(), any()) }
Expand All @@ -51,7 +51,7 @@ class AuthCodeCommandMockTest() {
fun verify_null() {
every { authCodeRepository.findByUserIdAndPurpose(ofType(String::class), ofType(Purpose::class)) } returns null

authCodeCommand.verify("userId", Purpose.SIGN_UP, "123") isEqualTo false
authCodeCommandService.verify("userId", Purpose.SIGN_UP, "123") isEqualTo false
}

@DisplayName("정상 케이스인 경우, 인증에 성공해야한다.")
Expand All @@ -60,7 +60,7 @@ class AuthCodeCommandMockTest() {
val authCode = AuthCode.createSignUp("userId")
every { authCodeRepository.findByUserIdAndPurpose(ofType(String::class), ofType(Purpose::class)) } returns authCode

authCodeCommand.verify("userId", Purpose.SIGN_UP, authCode.code) isEqualTo true
authCodeCommandService.verify("userId", Purpose.SIGN_UP, authCode.code) isEqualTo true
}

@DisplayName("인증 코드가 다른 경우, 인증에 실패해야한다.")
Expand All @@ -69,6 +69,6 @@ class AuthCodeCommandMockTest() {
val authCode = AuthCode.createSignUp("userId")
every { authCodeRepository.findByUserIdAndPurpose(ofType(String::class), ofType(Purpose::class)) } returns authCode

authCodeCommand.verify("userId", Purpose.SIGN_UP, "different") isEqualTo false
authCodeCommandService.verify("userId", Purpose.SIGN_UP, "different") isEqualTo false
}
}
2 changes: 1 addition & 1 deletion user-api/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring:
active: test
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
url: jdbc:h2:mem:testdb;MODE=MYSQL
username: root
password: test
sql:
Expand Down
4 changes: 2 additions & 2 deletions user-api/src/test/resources/db/users/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ CREATE TABLE IF NOT EXISTS `user`

CREATE TABLE IF NOT EXISTS `auth_code`
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'user.id',
user_id VARCHAR(50) NOT NULL COMMENT 'user.id',
purpose VARCHAR(50) NOT NULL COMMENT '사용 목적',
code VARCHAR(50) NOT NULL COMMENT '인증 코드',
created_at DATETIME NOT NULL COMMENT '생성 시각'
created_at DATETIME NOT NULL COMMENT '생성 시각',
PRIMARY KEY (`user_id`, purpose)
);

0 comments on commit 5a5fc81

Please sign in to comment.