From 4ba070d513103309219ce53a6c8f3b8d9c68320d Mon Sep 17 00:00:00 2001 From: aminecmi Date: Fri, 12 Aug 2022 22:20:35 +0200 Subject: [PATCH] Items position is nullable. --- src/main/kotlin/dao/DB.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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) {