diff --git a/src/main/kotlin/com/example/config/Database.kt b/src/main/kotlin/com/example/config/Database.kt index 1e6ee7b..fb95ab5 100644 --- a/src/main/kotlin/com/example/config/Database.kt +++ b/src/main/kotlin/com/example/config/Database.kt @@ -1,10 +1,23 @@ package com.example.config +import com.example.domain.CafeMenuTable +import com.example.domain.CafeOrderTable +import com.example.domain.CafeUserTable +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource import io.ktor.server.application.* import org.h2.tools.Server +import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.SchemaUtils +import org.jetbrains.exposed.sql.StdOutSqlLogger +import org.jetbrains.exposed.sql.addLogger +import org.jetbrains.exposed.sql.transactions.transaction +import javax.sql.DataSource fun Application.configureDatabase() { configureH2() + connectDatabase() + initData() } @@ -19,4 +32,29 @@ fun Application.configureH2() { h2Server.stop() application.environment.log.info("H2 server stopped. ${h2Server.url}") } +} + + +private fun connectDatabase() { + val config = + HikariConfig().apply { + jdbcUrl = "jdbc:h2:mem:cafedb" + driverClassName = "org.h2.Driver" + validate() + } + + val dataSource: DataSource = HikariDataSource(config) + Database.connect(dataSource) +} + +private fun initData() { + transaction { + addLogger(StdOutSqlLogger) + + SchemaUtils.create( + CafeMenuTable, + CafeUserTable, + CafeOrderTable + ) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/example/domain/Tables.kt b/src/main/kotlin/com/example/domain/Tables.kt new file mode 100644 index 0000000..372d7ab --- /dev/null +++ b/src/main/kotlin/com/example/domain/Tables.kt @@ -0,0 +1,29 @@ +package com.example.domain + +import com.example.shared.CafeMenuCategory +import com.example.shared.CafeOrderStatus +import com.example.shared.CafeUserRole +import org.jetbrains.exposed.dao.id.LongIdTable +import org.jetbrains.exposed.sql.javatime.datetime + +object CafeMenuTable : LongIdTable(name = "cafe_menu") { + val name = varchar("menu_name", length = 50) + val price = integer("price") + val category = enumerationByName("category", 10, CafeMenuCategory::class) + val image = text("image") +} + +object CafeUserTable : LongIdTable(name = "cafe_user") { + val nickname = varchar("nickname", length = 50) + val password = varchar("password", length = 100) + val roles = enumList("roles", CafeUserRole::class.java, 20) +} + +object CafeOrderTable : LongIdTable(name = "cafe_order") { + val orderCode = varchar("order_code", length = 50) + val cafeUserId = reference("cafe_user_id", CafeUserTable) + val cafeMenuId = reference("cafe_menu_id", CafeMenuTable) + val price = integer("price") + val status = enumerationByName("status", 10, CafeOrderStatus::class) + val orderedAt = datetime("ordered_at") +} \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 3e11d78..aadef5d 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -4,7 +4,7 @@ %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - +