Vadik Sirekanyan пре 5 година
родитељ
комит
273516c1ed

+ 6 - 0
build.gradle.kts

@@ -10,12 +10,18 @@ version = "0.1"
 
 repositories {
     mavenCentral()
+    jcenter {
+        content {
+            includeGroup("org.jetbrains.exposed")
+        }
+    }
 }
 
 dependencies {
     implementation("org.telegram:telegrambots:4.9.1")
     implementation("io.ktor:ktor-client-cio:1.4.0")
     implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
+    implementation("org.jetbrains.exposed:exposed-jdbc:0.28.1")
     implementation("org.postgresql:postgresql:42.2.18")
     implementation("org.slf4j:slf4j-simple:1.7.30")
     testImplementation("junit:junit:4.13")

+ 19 - 49
src/main/kotlin/com/sirekanyan/andersrobot/repository/CityRepository.kt

@@ -1,21 +1,8 @@
 package com.sirekanyan.andersrobot.repository
 
-import java.sql.DriverManager
-import java.sql.ResultSet
-
-private const val CREATE_TABLE = """
-    create table if not exists cities (
-        chat bigint,
-        city bigint,
-        primary key (chat, city)
-    );
-    """
-private const val SELECT_CITIES =
-    "select city from cities where chat = ?"
-private const val INSERT_CITY =
-    "insert into cities (chat, city) values (?, ?) on conflict do nothing"
-private const val DELETE_CITY =
-    "delete from cities where chat = ? and city = ?"
+import com.sirekanyan.andersrobot.repository.table.Cities
+import org.jetbrains.exposed.sql.*
+import org.jetbrains.exposed.sql.transactions.transaction
 
 interface CityRepository {
     fun getCities(chat: Long): List<Long>
@@ -25,44 +12,27 @@ interface CityRepository {
 
 class CityRepositoryImpl(url: String) : CityRepository {
 
-    private val connection by lazy {
-        DriverManager.getConnection(url).also {
-            it.createStatement().execute(CREATE_TABLE)
+    init {
+        Database.connect(url)
+        transaction {
+            SchemaUtils.create(Cities)
         }
     }
 
-    override fun getCities(chat: Long): List<Long> =
-        connection.prepareStatement(SELECT_CITIES)
-            .run {
-                setLong(1, chat)
-                executeQuery()
-            }
-            .map {
-                getLong(1)
-            }
-
-    override fun putCity(chat: Long, city: Long): Boolean =
-        connection.prepareStatement(INSERT_CITY)
-            .run {
-                setLong(1, chat)
-                setLong(2, city)
-                executeUpdate() == 1
-            }
-
-    override fun deleteCity(chat: Long, city: Long): Boolean =
-        connection.prepareStatement(DELETE_CITY)
-            .run {
-                setLong(1, chat)
-                setLong(2, city)
-                executeUpdate() == 1
-            }
+    override fun getCities(chat: Long): List<Long> = transaction {
+        Cities.select { Cities.chat eq chat }.map { it[Cities.city] }
+    }
 
-    private fun <T> ResultSet.map(transform: ResultSet.() -> T): List<T> {
-        val list = mutableListOf<T>()
-        while (next()) {
-            list.add(transform())
+    override fun putCity(chat: Long, city: Long): Boolean = transaction {
+        Cities.insertIgnore {
+            it[Cities.chat] = chat
+            it[Cities.city] = city
         }
-        return list
+        true
+    }
+
+    override fun deleteCity(chat: Long, city: Long): Boolean = transaction {
+        Cities.deleteWhere { (Cities.chat eq chat) and (Cities.city eq city) } != 0
     }
 
 }

+ 13 - 0
src/main/kotlin/com/sirekanyan/andersrobot/repository/table/Cities.kt

@@ -0,0 +1,13 @@
+package com.sirekanyan.andersrobot.repository.table
+
+import org.jetbrains.exposed.sql.Table
+
+object Cities : Table() {
+
+    val chat = long("chat")
+    val city = long("city")
+
+    override val primaryKey: PrimaryKey =
+        PrimaryKey(chat, city)
+
+}