src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt

changeset 292
703591e739f4
parent 284
671c1c8fbf1c
child 311
bf67e0ff7131
--- a/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt	Mon Oct 30 10:06:22 2023 +0100
+++ b/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt	Mon Oct 30 14:44:36 2023 +0100
@@ -26,6 +26,7 @@
 package de.uapcore.lightpit
 
 import de.uapcore.lightpit.dao.DataAccessObject
+import de.uapcore.lightpit.entities.HasNode
 import de.uapcore.lightpit.viewmodel.NavMenu
 import de.uapcore.lightpit.viewmodel.View
 import jakarta.servlet.http.HttpServletRequest
@@ -38,6 +39,14 @@
 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit
 typealias PathParameters = Map<String, String>
 
+sealed class OptionalPathInfo<in T : HasNode>(info: T) {
+    class Specific<T: HasNode>(val elem: T) : OptionalPathInfo<T>(elem)
+    data object All : OptionalPathInfo<HasNode>(object : HasNode { override val node = "-"})
+    data object None : OptionalPathInfo<HasNode>(object : HasNode { override val node = "~"})
+    data object NotFound : OptionalPathInfo<HasNode>(object : HasNode { override val node = ""})
+    val node = info.node
+}
+
 sealed interface ValidationResult<T>
 class ValidationError<T>(val message: String): ValidationResult<T>
 class ValidatedValue<T>(val result: T): ValidationResult<T>
@@ -173,6 +182,18 @@
         }
     }
 
+
+    fun <T : HasNode> lookupPathParam(paramName: String, list: List<T>): OptionalPathInfo<T> {
+        return when (val node = this.pathParams[paramName]) {
+            null -> OptionalPathInfo.All
+            "-" -> OptionalPathInfo.All
+            "~" -> OptionalPathInfo.None
+            else -> list.find { it.node == node }
+                ?.let { OptionalPathInfo.Specific(it) }
+                ?: OptionalPathInfo.NotFound
+        }
+    }
+
     val body: String by lazy {
         request.reader.lineSequence().joinToString("\n")
     }

mercurial