Skip to content

Commit

Permalink
refactor: (#20) Remove UUID legary
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed May 2, 2023
1 parent d533adc commit 74ca93a
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 59 deletions.
9 changes: 4 additions & 5 deletions src/main/kotlin/com/dsm/domain/auth/token/GenerateToken.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import java.time.LocalDateTime
import java.util.Date
import java.util.UUID

/**
*
Expand All @@ -30,7 +29,7 @@ class JwtGenerator(
private val refreshTokenRepository: RefreshTokenRepository
) : TokenProvider {

private suspend fun generateRefreshToken(studentId: UUID): String {
private suspend fun generateRefreshToken(studentId: Int): String {
val token: String = JWT.create()
.withSubject(JWT_SUBJECT)
.withJWTId(JWT_REFRESH)
Expand All @@ -48,18 +47,18 @@ class JwtGenerator(
return token
}

private fun generateAccessToken(studentId: UUID): String {
private fun generateAccessToken(studentId: Int): String {
return JWT.create()
.withSubject(JWT_SUBJECT)
.withJWTId(JWT_ACCESS)
.withAudience(SecurityProperties.audience)
.withIssuer(SecurityProperties.issuer)
.withClaim(JWT_STUDENT_ID, studentId.toString())
.withClaim(JWT_STUDENT_ID, studentId)
.withExpiresAt(Date(System.currentTimeMillis() + SecurityProperties.accessExpiredMillis))
.sign(Algorithm.HMAC256(SecurityProperties.secret))
}

override suspend fun generateToken(studentId: UUID): TokenResult {
override suspend fun generateToken(studentId: Int): TokenResult {
val accessToken: String = generateAccessToken(studentId)
val refreshToken: String = generateRefreshToken(studentId)

Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/com/dsm/domain/auth/token/TokenProvider.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.dsm.domain.auth.token

import java.util.UUID

/**
*
* 토큰을 제공하는 TokenProvider
Expand All @@ -10,5 +8,5 @@ import java.util.UUID
* @date 2023/03/20
**/
interface TokenProvider {
suspend fun generateToken(studentId: UUID): TokenResult
suspend fun generateToken(studentId: Int): TokenResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.dsm.persistence.repository.AuthenticateStudentRepository
import com.dsm.persistence.repository.StudentRepository
import com.dsm.plugins.database.dbQuery
import kotlinx.serialization.Serializable
import java.util.UUID

/**
*
Expand All @@ -24,17 +23,17 @@ class RegisterStudent(
private val tokenProvider: TokenProvider
) {
suspend operator fun invoke(request: Request): TokenResult = dbQuery {
val studentId: UUID = registerStudentAccount(request)
val studentId: Int = registerStudentAccount(request)
return@dbQuery tokenProvider.generateToken(studentId)
}

private suspend fun registerStudentAccount(request: Request): UUID {
private suspend fun registerStudentAccount(request: Request): Int {
val authenticate: AuthenticateStudent = authenticateStudentRepository.findByNumber(request.number)
?: throw AuthenticateStudentException.UnknownNumber()

authenticate(request.name)

val studentId: UUID = studentRepository.insert(Student.register(
val studentId: Int = studentRepository.insert(Student.register(
name = request.name,
number = authenticate.number,
sex = authenticate.sex,
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/com/dsm/domain/mission/usecase/PostMission.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.dsm.persistence.entity.Mission
import com.dsm.persistence.repository.MissionRepository
import com.dsm.plugins.database.dbQuery
import java.time.LocalDateTime
import java.util.UUID

/**
*
Expand All @@ -18,13 +17,13 @@ class PostMission(
private val missionRepository: MissionRepository
) {

suspend operator fun invoke(request: Request, studentId: UUID) : Unit = dbQuery {
suspend operator fun invoke(request: Request, studentId: Int) : Unit = dbQuery {
if (missionRepository.existsByStudentId(studentId)) {
throw MissionException.AlreadyPosted()
}

missionRepository.insert(Mission.doPost(
studentId = studentId,
orderId = studentId,
stuff = request.stuff,
deadline = request.deadline,
price = request.price
Expand Down
21 changes: 10 additions & 11 deletions src/main/kotlin/com/dsm/persistence/entity/Mission.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.dsm.persistence.entity

import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.javatime.datetime
import java.time.LocalDateTime
import java.util.UUID

/**
*
Expand All @@ -14,9 +13,9 @@ import java.util.UUID
* @author Chokyunghyeon
* @date 2023/03/16
**/
object MissionTable : UUIDTable("tbl_mission") {
val student: Column<EntityID<UUID>> = reference("student_id", StudentTable).uniqueIndex()
val deliveryman: Column<EntityID<UUID>?> = reference("delivery_man_id", StudentTable).nullable()
object MissionTable : IntIdTable("tbl_mission") {
val order: Column<EntityID<Int>> = reference("order_id", StudentTable).uniqueIndex()
val shipper: Column<EntityID<Int>?> = reference("shipper_id", StudentTable).nullable()
val stuff: Column<String> = varchar("stuff", Mission.STUFF_MAX_LENGTH)
val deadline: Column<LocalDateTime> = datetime("deadline")
val price: Column<Long> = long("price")
Expand All @@ -32,9 +31,9 @@ enum class DeliveryState {
}

data class Mission(
val id: UUID = UUID(0, 0),
val studentId: UUID,
val deliverymanId: UUID?,
val id: Int = 0,
val orderId: Int,
val shipperId: Int?,
val stuff: String,
val deadline: LocalDateTime,
val price: Long,
Expand All @@ -44,13 +43,13 @@ data class Mission(
internal companion object {
const val STUFF_MAX_LENGTH: Int = 50

fun doPost(studentId: UUID, stuff: String, deadline: LocalDateTime, price: Long): Mission = Mission(
studentId = studentId,
fun doPost(orderId: Int, stuff: String, deadline: LocalDateTime, price: Long): Mission = Mission(
orderId = orderId,
stuff = stuff,
deadline = deadline,
state = DeliveryState.POSTING,
price = price,
deliverymanId = null
shipperId = null
)
}
}
5 changes: 1 addition & 4 deletions src/main/kotlin/com/dsm/persistence/entity/RefreshToken.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.dsm.persistence.entity

import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import java.util.UUID

/**
*
Expand All @@ -15,8 +13,7 @@ import java.util.UUID
data class RefreshToken(
val token: String,

@Contextual
val studentId: UUID,
val studentId: Int,

val expiredMillis: Long
)
7 changes: 3 additions & 4 deletions src/main/kotlin/com/dsm/persistence/entity/Student.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.dsm.persistence.entity

import com.dsm.exception.StudentException
import com.dsm.plugins.PasswordFormatter
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import java.util.UUID

/**
*
Expand All @@ -13,7 +12,7 @@ import java.util.UUID
* @author Chokyunghyeon
* @date 2023/03/16
**/
object StudentTable : UUIDTable("tbl_student") {
object StudentTable : IntIdTable("tbl_student") {
val name: Column<String> = varchar("name", Student.NAME_MAX_LENGTH)
val number: Column<Int> = integer("school_number")
val sex: Column<Sex> = enumerationByName("sex", Sex.VALUE_MAX_LENGTH)
Expand All @@ -29,7 +28,7 @@ enum class Sex {
}

data class Student(
val id: UUID = UUID(0, 0),
val id: Int = 0,
val name: String,
val number: Int,
val sex: Sex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.dsm.persistence.repository.StudentRepository
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.select
import java.util.UUID

/**
*
Expand All @@ -25,7 +24,7 @@ class StudentQueryFactory : StudentRepository {
sex = row[StudentTable.sex]
)

override suspend fun findById(id: UUID): Student? = StudentTable
override suspend fun findById(id: Int): Student? = StudentTable
.select { StudentTable.id eq id }
.singleOrNull()
?.let(::toEntity)
Expand All @@ -40,12 +39,12 @@ class StudentQueryFactory : StudentRepository {
.singleOrNull()
?.let(::toEntity)

override suspend fun existsById(id: UUID): Boolean = StudentTable
override suspend fun existsById(id: Int): Boolean = StudentTable
.select { StudentTable.id eq id }
.limit(1)
.empty().not()

override suspend fun insert(student: Student): UUID = StudentTable.insertAndGetId {
override suspend fun insert(student: Student): Int = StudentTable.insertAndGetId {
it[name] = student.name
it[number] = student.number
it[password] = student.password
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dsm.persistence.repository

import com.dsm.persistence.entity.DeliveryState
import com.dsm.persistence.entity.Mission
import java.util.UUID

/**
*
Expand All @@ -12,10 +12,12 @@ import java.util.UUID
**/
interface MissionRepository {

suspend fun findById(id: UUID) : Mission?
suspend fun findById(id: Int) : Mission?

suspend fun existsByStudentId(studentId: UUID) : Boolean
suspend fun existsByStudentId(studentId: Int) : Boolean

suspend fun insert(mission: Mission) : Mission

suspend fun findAllByStatus(status: DeliveryState): List<Mission>

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dsm.persistence.repository

import com.dsm.persistence.entity.Student
import java.util.UUID

/**
*
Expand All @@ -11,13 +10,13 @@ import java.util.UUID
* @date 2023/03/22
**/
interface StudentRepository {
suspend fun findById(id: UUID): Student?
suspend fun findById(id: Int): Student?

suspend fun findByNumber(number: Int): Student?

suspend fun findByName(name: String): Student?

suspend fun existsById(id: UUID): Boolean
suspend fun existsById(id: Int): Boolean

suspend fun insert(student: Student): UUID
suspend fun insert(student: Student): Int
}
8 changes: 0 additions & 8 deletions src/main/kotlin/com/dsm/plugins/Negotiating.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.dsm.plugins
import com.fasterxml.jackson.core.util.DefaultIndenter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.deser.std.UUIDDeserializer
import com.fasterxml.jackson.databind.ser.std.UUIDSerializer
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer
Expand All @@ -21,7 +19,6 @@ import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.UUID


/**
Expand Down Expand Up @@ -57,11 +54,6 @@ fun Application.configureNegotiating() {
LocalTime::class,
LocalTimeDeserializer(DateTimeFormatter.ISO_TIME)
)
addSerializer(UUIDSerializer())
addDeserializer(
UUID::class,
UUIDDeserializer()
)
})
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/com/dsm/plugins/Security.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import io.ktor.server.auth.jwt.jwt
import io.ktor.server.auth.principal
import io.ktor.server.config.ApplicationConfig
import org.koin.ktor.ext.getKoin
import java.util.UUID
import kotlin.properties.Delegates

/**
Expand Down Expand Up @@ -45,10 +44,10 @@ fun Application.configureSecurity() {

validate { credential ->
dbQuery {
val studentId: String = credential.payload.getClaim(JwtGenerator.JWT_STUDENT_ID).asString()
val studentId: Int = credential.payload.getClaim(JwtGenerator.JWT_STUDENT_ID).asInt()
?: return@dbQuery null

return@dbQuery if (studentRepository.existsById(studentId.let(UUID::fromString))) {
return@dbQuery if (studentRepository.existsById(studentId)) {
JWTPrincipal(credential.payload)
} else {
null
Expand All @@ -63,12 +62,11 @@ fun Application.configureSecurity() {
}
}

fun ApplicationCall.currentUserId() : UUID {
fun ApplicationCall.currentUserId() : Int {
val principal: JWTPrincipal = principal<JWTPrincipal>()
?: throw DomainException.InternalServerError("JWT Principal Undefined")

return principal.payload.getClaim(JwtGenerator.JWT_STUDENT_ID).asString()
.let(UUID::fromString)
return principal.payload.getClaim(JwtGenerator.JWT_STUDENT_ID).asInt()
}

object SecurityProperties {
Expand Down

0 comments on commit 74ca93a

Please sign in to comment.