diff --git a/src/main/kotlin/dao/DB.kt b/src/main/kotlin/dao/DB.kt index badd697..d73d36d 100644 --- a/src/main/kotlin/dao/DB.kt +++ b/src/main/kotlin/dao/DB.kt @@ -101,17 +101,19 @@ class DB { } - private fun reorderElements(listId: Int, position: Int, itemId: Int? = null) { + private fun reorderElements(listId: Int, position: Int?, itemId: Int? = null) { transaction { var i = 0 ItemV2.find { ItemsV2.list eq listId and (ItemsV2.id neq itemId and (ItemsV2.checked eq false)) } .sortedBy { it.position } .forEach { - if (i == position) { + if (position != null && i == position) { + i++ + } + if (it.position != null) { + it.position = i i++ } - it.position = i - i++ } } } @@ -156,6 +158,7 @@ class DB { (ItemV2.find { ItemsV2.list eq listId and (ItemsV2.checked eq false) }.count() + 1).toInt() } item.position = p + reorderElements(listId, p, itemId) item.checked = body.checked } if (body.content != null) {