From 30dd0fcea9c2f79d64b69b9025437d94ac5cabdd Mon Sep 17 00:00:00 2001 From: aminecmi Date: Fri, 12 Aug 2022 22:00:07 +0200 Subject: [PATCH] Items position is nullable. --- src/main/kotlin/MigrationsController.kt | 20 ++++++++++----- src/main/kotlin/dao/DB.kt | 20 +++++++-------- src/main/kotlin/dao/{Items.kt => ItemsV2.kt} | 16 ++++++------ src/main/kotlin/dao/Lists.kt | 2 +- src/main/kotlin/dao/old/Items.kt | 26 ++++++++++++++++++++ src/main/kotlin/server.kt | 7 ++++-- 6 files changed, 64 insertions(+), 27 deletions(-) rename src/main/kotlin/dao/{Items.kt => ItemsV2.kt} (64%) create mode 100644 src/main/kotlin/dao/old/Items.kt diff --git a/src/main/kotlin/MigrationsController.kt b/src/main/kotlin/MigrationsController.kt index a3407eb..ca7384c 100644 --- a/src/main/kotlin/MigrationsController.kt +++ b/src/main/kotlin/MigrationsController.kt @@ -1,13 +1,15 @@ import dao.* +import dao.old.Items import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.deleteAll import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.update class MigrationsController { private val migrations: Map Boolean> = mapOf( - "update-checked-null-position" to {updateNullPositionForChecked()}, - "update-unchecked-position" to {updatePositionForUnchecked()} + "move-items-to-new-table" to {moveItemsToNewTable()}, + "unchecked-position-change" to {updatePositionForUnchecked()} ) fun checkMigrationsToDo() { @@ -38,12 +40,18 @@ class MigrationsController { } } - private fun updateNullPositionForChecked() : Boolean { + private fun moveItemsToNewTable() : Boolean { try { transaction { - Items.update({ Items.checked eq true }) { - it[position] = null + Items.selectAll().forEach { + ItemV2.new { + list = it[Items.list] + content = it[Items.content] + position = if (it[Items.checked]) null else it[Items.position] + checked = it[Items.checked] + } } + Items.deleteAll() } } catch (e: Exception) { return false @@ -56,7 +64,7 @@ class MigrationsController { transaction { Lists.selectAll().forEach { var i = 0 - Item.find { Items.list eq it[Lists.id].value and (Items.checked eq false) } + ItemV2.find { ItemsV2.list eq it[Lists.id].value and (ItemsV2.checked eq false) } .sortedBy { it.position } .forEach { it.position = i diff --git a/src/main/kotlin/dao/DB.kt b/src/main/kotlin/dao/DB.kt index 8a279c2..badd697 100644 --- a/src/main/kotlin/dao/DB.kt +++ b/src/main/kotlin/dao/DB.kt @@ -24,7 +24,7 @@ class DB { val list = List.findById(resourceId) if (list != null) { - Item.find { Items.list eq list.id.value}.forEach { i -> i.delete() } + ItemV2.find { ItemsV2.list eq list.id.value}.forEach { i -> i.delete() } } list?.delete() ?: ctx.status(404) @@ -69,7 +69,7 @@ class DB { transaction { val listOpt = List.findById(resourceID) if (listOpt != null) { - val items = Item.find { Items.list eq resourceID } + val items = ItemV2.find { ItemsV2.list eq resourceID } .sortedBy { it.position } ctx.json(listOpt.toViewWithItems(items)) @@ -83,10 +83,10 @@ class DB { transaction { val listOpt = List.findById(listId) if (listOpt != null) { - val count = Item.find { Items.list eq listId and (Items.checked eq false) }.count() + val count = ItemV2.find { ItemsV2.list eq listId and (ItemsV2.checked eq false) }.count() val p = (count + 1) - var i = Item.new { + var i = ItemV2.new { list = listId content = c.content position = p.toInt() @@ -104,7 +104,7 @@ class DB { private fun reorderElements(listId: Int, position: Int, itemId: Int? = null) { transaction { var i = 0 - Item.find { Items.list eq listId and (Items.id neq itemId and (Items.checked eq false)) } + ItemV2.find { ItemsV2.list eq listId and (ItemsV2.id neq itemId and (ItemsV2.checked eq false)) } .sortedBy { it.position } .forEach { if (i == position) { @@ -120,11 +120,11 @@ class DB { transaction { val listOpt = List.findById(listId) if (listOpt != null) { - val item = Item.find { Items.list eq listId and (Items.id eq itemId) }.limit(1).firstOrNull() + val item = ItemV2.find { ItemsV2.list eq listId and (ItemsV2.id eq itemId) }.limit(1).firstOrNull() if (item != null) { item.delete() var i = 0 - Item.all() + ItemV2.all() .sortedBy { it.position } .forEach { it.position = i @@ -143,7 +143,7 @@ class DB { transaction { val listOpt = List.findById(listId) if (listOpt != null) { - val item = Item.find { Items.list eq listId and (Items.id eq itemId) }.limit(1).firstOrNull() + val item = ItemV2.find { ItemsV2.list eq listId and (ItemsV2.id eq itemId) }.limit(1).firstOrNull() if (item != null) { if (body.position != null) { reorderElements(listId, body.position, itemId) @@ -151,9 +151,9 @@ class DB { } if (body.checked != null) { var p: Int? = if (body.checked) { - (Item.find { Items.list eq listId and (Items.checked eq false) }.count() + 1).toInt() + null } else { - null + (ItemV2.find { ItemsV2.list eq listId and (ItemsV2.checked eq false) }.count() + 1).toInt() } item.position = p item.checked = body.checked diff --git a/src/main/kotlin/dao/Items.kt b/src/main/kotlin/dao/ItemsV2.kt similarity index 64% rename from src/main/kotlin/dao/Items.kt rename to src/main/kotlin/dao/ItemsV2.kt index 991217a..a457993 100644 --- a/src/main/kotlin/dao/Items.kt +++ b/src/main/kotlin/dao/ItemsV2.kt @@ -5,24 +5,24 @@ import org.jetbrains.exposed.dao.IntEntityClass import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.dao.id.IntIdTable -object Items : IntIdTable() { +object ItemsV2 : IntIdTable() { val list = integer("list") val content = varchar("content", 256) val position = integer("position").nullable() val checked = bool("checked").default(false) } -class Item(id: EntityID) : IntEntity(id) { - companion object : IntEntityClass(Items) +class ItemV2(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(ItemsV2) - var list by Items.list - var content by Items.content - var position by Items.position - var checked by Items.checked + var list by ItemsV2.list + var content by ItemsV2.content + var position by ItemsV2.position + var checked by ItemsV2.checked } -fun Item.toView(): ItemView { +fun ItemV2.toView(): ItemView { return ItemView(this.id.value, this.content, this.checked, this.position) } diff --git a/src/main/kotlin/dao/Lists.kt b/src/main/kotlin/dao/Lists.kt index d3c3c9e..da10ffd 100644 --- a/src/main/kotlin/dao/Lists.kt +++ b/src/main/kotlin/dao/Lists.kt @@ -20,7 +20,7 @@ fun List.toView(): ListView { return ListView(this.id.value, this.name) } -fun List.toViewWithItems(items: kotlin.collections.List): ListViewWithItems { +fun List.toViewWithItems(items: kotlin.collections.List): ListViewWithItems { return ListViewWithItems(this.id.value, this.name, items.map { it.toView() }) } diff --git a/src/main/kotlin/dao/old/Items.kt b/src/main/kotlin/dao/old/Items.kt new file mode 100644 index 0000000..8f7327f --- /dev/null +++ b/src/main/kotlin/dao/old/Items.kt @@ -0,0 +1,26 @@ +package dao.old + +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IntIdTable + +@Deprecated("Position should be nullable") +object Items : IntIdTable() { + val list = integer("list") + val content = varchar("content", 256) + val position = integer("position") + val checked = bool("checked").default(false) +} + +@Deprecated("Position should be nullable") +class Item(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(Items) + + var list by Items.list + var content by Items.content + var position by Items.position + var checked by Items.checked + + +} \ No newline at end of file diff --git a/src/main/kotlin/server.kt b/src/main/kotlin/server.kt index 4faefb3..aa78f08 100644 --- a/src/main/kotlin/server.kt +++ b/src/main/kotlin/server.kt @@ -1,4 +1,5 @@ import dao.* +import dao.old.Items import io.javalin.Javalin import io.javalin.apibuilder.ApiBuilder.* import io.javalin.core.security.Role @@ -34,11 +35,13 @@ fun main() { Database.connect("jdbc:sqlite:data.db", "org.sqlite.JDBC") transaction { addLogger(StdOutSqlLogger) - SchemaUtils.create (Migrations) - migrationsController.checkMigrationsToDo() SchemaUtils.create (Lists) SchemaUtils.create (Items) + SchemaUtils.create (ItemsV2) SchemaUtils.create (Keys) + SchemaUtils.create (Migrations) + migrationsController.checkMigrationsToDo() + } app.routes {