Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/kotlin/com/example/domain/BaseModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.domain

interface BaseModel {
var id: Long?
}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/example/domain/CrudRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.domain

interface CrudRepository<DOMAIN : BaseModel> {
fun create(domain: DOMAIN): DOMAIN
fun findAll(): List<DOMAIN>
fun read(id: Long): DOMAIN?
fun update(domain: DOMAIN): DOMAIN
fun delete(domain: DOMAIN)
fun delete(id: Long)
}
62 changes: 62 additions & 0 deletions src/main/kotlin/com/example/domain/ExposedCrudRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.domain

import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update


interface ExposedCrudRepository<TABLE : LongIdTable, DOMAIN : BaseModel> : CrudRepository<DOMAIN> {
val table: TABLE
override fun create(domain: DOMAIN): DOMAIN = dbQuery {
val id = table.insertAndGetId(toRow(domain))
domain.id = id.value
domain
}

override fun findAll(): List<DOMAIN> = dbQuery {
table.selectAll().map { toDomain(it) }
}

override fun read(id: Long): DOMAIN? = dbQuery {
table.selectAll().where { table.id eq id }
.map { toDomain(it) }
.singleOrNull()
}

override fun update(domain: DOMAIN): DOMAIN = dbQuery {
table.update(
where = { table.id eq domain.id },
body = updateRow(domain)
)
domain
}

override fun delete(domain: DOMAIN) = dbQuery {
table.deleteWhere { table.id eq domain.id }
return@dbQuery
}

override fun delete(id: Long) = dbQuery {
table.deleteWhere { table.id eq id }
return@dbQuery
}

fun toRow(domain: DOMAIN): TABLE.(InsertStatement<EntityID<Long>>) -> Unit
fun toDomain(row: ResultRow): DOMAIN
fun updateRow(domain: DOMAIN): TABLE.(UpdateStatement) -> Unit

private fun <T> dbQuery(block: () -> T): T = transaction {
addLogger(StdOutSqlLogger)
block()
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/com/example/domain/model/CafeMenu.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.domain.model

import com.example.domain.BaseModel
import com.example.shared.CafeMenuCategory
import kotlinx.serialization.Serializable

Expand All @@ -9,5 +10,5 @@ data class CafeMenu(
val price: Int,
val category: CafeMenuCategory,
val image: String,
var id: Long? = null,
)
override var id: Long? = null,
): BaseModel
19 changes: 19 additions & 0 deletions src/main/kotlin/com/example/domain/model/CafeOrder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.domain.model

import com.example.shared.CafeOrderStatus
import com.example.domain.BaseModel
import com.example.shared.LocalDateTimeSerializer
import kotlinx.serialization.Serializable
import java.time.LocalDateTime

@Serializable
data class CafeOrder(
val orderCode: String,
val cafeMenuId: Long,
val cafeUserId: Long,
val price: Int,
var status: CafeOrderStatus,
@Serializable(with = LocalDateTimeSerializer::class)
val orderedAt: LocalDateTime,
override var id: Long? = null,
) : BaseModel
14 changes: 14 additions & 0 deletions src/main/kotlin/com/example/domain/model/CafeUser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.domain.model


import com.example.shared.CafeUserRole
import com.example.domain.BaseModel
import kotlinx.serialization.Serializable

@Serializable
data class CafeUser(
val nickname: String,
val password: String,
val roles: List<CafeUserRole>,
override var id: Long? = null,
) : BaseModel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.domain.repository

import com.example.domain.CafeMenuTable
import com.example.domain.ExposedCrudRepository
import com.example.domain.model.CafeMenu
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement

class CafeMenuRepository(
override val table: CafeMenuTable,
) : ExposedCrudRepository<CafeMenuTable, CafeMenu> {
override fun toRow(domain: CafeMenu): CafeMenuTable.(InsertStatement<EntityID<Long>>) -> Unit = {
if (domain.id != null) {
it[id] = domain.id!!
}
it[name] = domain.name
it[price] = domain.price
it[category] = domain.category
it[image] = domain.image
}

override fun toDomain(row: ResultRow): CafeMenu {
return CafeMenu(
name = row[CafeMenuTable.name],
price = row[CafeMenuTable.price],
category = row[CafeMenuTable.category],
image = row[CafeMenuTable.image]
).apply {
id = row[CafeMenuTable.id].value
}
}

override fun updateRow(domain: CafeMenu): CafeMenuTable.(UpdateStatement) -> Unit = {
it[name] = domain.name
it[price] = domain.price
it[category] = domain.category
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.domain.repository

import com.example.domain.CafeOrderTable
import com.example.domain.ExposedCrudRepository
import com.example.domain.model.CafeOrder
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement

class CafeOrderRepository(
override val table: CafeOrderTable,
) : ExposedCrudRepository<CafeOrderTable, CafeOrder> {
override fun toRow(domain: CafeOrder): CafeOrderTable.(InsertStatement<EntityID<Long>>) -> Unit = {
if (domain.id != null) {
it[id] = domain.id!!
}
it[orderCode] = domain.orderCode
it[cafeUserId] = domain.cafeUserId
it[cafeMenuId] = domain.cafeMenuId
it[price] = domain.price
it[status] = domain.status
it[orderedAt] = domain.orderedAt
}

override fun toDomain(row: ResultRow): CafeOrder {
return CafeOrder(
orderCode = row[CafeOrderTable.orderCode],
cafeMenuId = row[CafeOrderTable.cafeMenuId].value,
cafeUserId = row[CafeOrderTable.cafeUserId].value,
price = row[CafeOrderTable.price],
status = row[CafeOrderTable.status],
orderedAt = row[CafeOrderTable.orderedAt],
).apply {
id = row[CafeOrderTable.id].value
}
}

override fun updateRow(domain: CafeOrder): CafeOrderTable.(UpdateStatement) -> Unit = {
it[orderCode] = domain.orderCode
it[cafeUserId] = domain.cafeUserId
it[cafeMenuId] = domain.cafeMenuId
it[price] = domain.price
it[status] = domain.status
it[orderedAt] = domain.orderedAt
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.domain.repository

import com.example.domain.CafeUserTable
import com.example.domain.ExposedCrudRepository
import com.example.domain.model.CafeUser
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement

class CafeUserRepository(
override val table: CafeUserTable,
) : ExposedCrudRepository<CafeUserTable, CafeUser> {

override fun toRow(domain: CafeUser): CafeUserTable.(InsertStatement<EntityID<Long>>) -> Unit = {
if (domain.id != null) {
it[id] = domain.id!!
}
it[nickname] = domain.nickname
it[password] = domain.password
it[roles] = domain.roles
}

override fun toDomain(row: ResultRow): CafeUser {
return CafeUser(
nickname = row[CafeUserTable.nickname],
password = row[CafeUserTable.password],
roles = row[CafeUserTable.roles],
).apply {
id = row[CafeUserTable.id].value
}
}

override fun updateRow(domain: CafeUser): CafeUserTable.(UpdateStatement) -> Unit = {
it[nickname] = domain.nickname
it[password] = domain.password
it[roles] = domain.roles
}
}