Items position is nullable.

This commit is contained in:
aminecmi 2022-08-12 22:00:07 +02:00
parent 3dc988573d
commit 30dd0fcea9
6 changed files with 64 additions and 27 deletions

View File

@ -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<String, () -> 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

View File

@ -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

View File

@ -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<Int>) : IntEntity(id) {
companion object : IntEntityClass<Item>(Items)
class ItemV2(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ItemV2>(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)
}

View File

@ -20,7 +20,7 @@ fun List.toView(): ListView {
return ListView(this.id.value, this.name)
}
fun List.toViewWithItems(items: kotlin.collections.List<Item>): ListViewWithItems {
fun List.toViewWithItems(items: kotlin.collections.List<ItemV2>): ListViewWithItems {
return ListViewWithItems(this.id.value, this.name, items.map { it.toView() })
}

View File

@ -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<Int>) : IntEntity(id) {
companion object : IntEntityClass<Item>(Items)
var list by Items.list
var content by Items.content
var position by Items.position
var checked by Items.checked
}

View File

@ -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 {