minor code changes

Tue, 03 Aug 2021 14:08:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 03 Aug 2021 14:08:08 +0200
changeset 210
37fbdcb422b7
parent 209
c9c6abf167c7
child 211
8066895cc57e

minor code changes

src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt file | annotate | diff | comparison | revisions
src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt file | annotate | diff | comparison | revisions
src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt file | annotate | diff | comparison | revisions
--- a/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt	Tue Aug 03 13:41:32 2021 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt	Tue Aug 03 14:08:08 2021 +0200
@@ -159,11 +159,12 @@
     fun param(name: String): String? = request.getParameter(name)
     fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray()
 
-    fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), errorMessages: MutableList<String>): T? {
+    fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>),
+                  defaultValue: T, errorMessages: MutableList<String>): T {
         return when (val result = validator(param(name))) {
             is ValidationError -> {
                 errorMessages.add(i18n(result.message))
-                null
+                defaultValue
             }
             is ValidatedValue -> {
                 result.result
--- a/src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt	Tue Aug 03 13:41:32 2021 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt	Tue Aug 03 14:08:08 2021 +0200
@@ -103,21 +103,20 @@
             selectedComponent
         )
 
-    sealed class LookupResult<T> {
-        class NotFound<T> : LookupResult<T>()
-        data class Found<T>(val elem: T?) : LookupResult<T>()
-    }
+    private sealed interface LookupResult<T>
+    private class NotFound<T> : LookupResult<T>
+    private data class Found<T>(val elem: T?) : LookupResult<T>
 
     private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> {
         val node = pathParams[paramName]
         return if (node == null || node == "-") {
-            LookupResult.Found(null)
+            Found(null)
         } else {
             val result = list.find { it.node == node }
             if (result == null) {
-                LookupResult.NotFound()
+                NotFound()
             } else {
-                LookupResult.Found(result)
+                Found(result)
             }
         }
     }
@@ -147,7 +146,7 @@
 
     private fun feedPath(project: Project) = "feed/${project.node}/issues.rss"
 
-    data class PathInfos(
+    private data class PathInfos(
         val projectInfo: ProjectInfo,
         val version: Version?,
         val component: Component?
@@ -164,20 +163,20 @@
         }
 
         val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) {
-            is LookupResult.NotFound -> {
+            is NotFound -> {
                 http.response.sendError(404)
                 return null
             }
-            is LookupResult.Found -> {
+            is Found -> {
                 result.elem
             }
         }
         val component = when (val result = http.lookupPathParam("component", projectInfo.components)) {
-            is LookupResult.NotFound -> {
+            is NotFound -> {
                 http.response.sendError(404)
                 return null
             }
-            is LookupResult.Found -> {
+            is Found -> {
                 result.elem
             }
         }
@@ -231,7 +230,6 @@
     }
 
     private fun projectCommit(http: HttpRequest, dao: DataAccessObject) {
-        // TODO: replace defaults with throwing validator exceptions
         val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply {
             name = http.param("name") ?: ""
             node = http.param("node") ?: ""
@@ -288,11 +286,11 @@
 
         val version: Version
         when (val result = http.lookupPathParam("version", projectInfo.versions)) {
-            is LookupResult.NotFound -> {
+            is NotFound -> {
                 http.response.sendError(404)
                 return
             }
-            is LookupResult.Found -> {
+            is Found -> {
                 version = result.elem ?: Version(-1, projectInfo.project.id)
             }
         }
@@ -310,17 +308,23 @@
         }
     }
 
-    private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
+    private fun obtainIdAndProject(http: HttpRequest, dao:DataAccessObject): Pair<Int, Project>? {
         val id = http.param("id")?.toIntOrNull()
         val projectid = http.param("projectid")?.toIntOrNull() ?: -1
         val project = dao.findProject(projectid)
-        if (id == null || project == null) {
+        return if (id == null || project == null) {
             http.response.sendError(400)
-            return
+            null
+        } else {
+            Pair(id, project)
         }
+    }
 
-        // TODO: replace defaults with throwing validator exceptions
-        val version = Version(id, projectid).apply {
+    private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
+        val idParams = obtainIdAndProject(http, dao) ?: return
+        val (id, project) = idParams
+
+        val version = Version(id, project.id).apply {
             name = http.param("name") ?: ""
             node = http.param("node") ?: ""
             ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
@@ -372,11 +376,11 @@
 
         val component: Component
         when (val result = http.lookupPathParam("component", projectInfo.components)) {
-            is LookupResult.NotFound -> {
+            is NotFound -> {
                 http.response.sendError(404)
                 return
             }
-            is LookupResult.Found -> {
+            is Found -> {
                 component = result.elem ?: Component(-1, projectInfo.project.id)
             }
         }
@@ -395,16 +399,10 @@
     }
 
     private fun componentCommit(http: HttpRequest, dao: DataAccessObject) {
-        val id = http.param("id")?.toIntOrNull()
-        val projectid = http.param("projectid")?.toIntOrNull() ?: -1
-        val project = dao.findProject(projectid)
-        if (id == null || project == null) {
-            http.response.sendError(400)
-            return
-        }
+        val idParams = obtainIdAndProject(http, dao) ?: return
+        val (id, project) = idParams
 
-        // TODO: replace defaults with throwing validator exceptions
-        val component = Component(id, projectid).apply {
+        val component = Component(id, project.id).apply {
             name = http.param("name") ?: ""
             node = http.param("node") ?: ""
             ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
@@ -506,8 +504,6 @@
                 return
             }
 
-            // TODO: throw validator exception instead of using a default
-
             val commentId = http.param("commentid")?.toIntOrNull() ?: -1
             if (commentId > 0) {
                 val comment = dao.findComment(commentId)
@@ -533,7 +529,6 @@
 
     private fun issueCommit(http: HttpRequest, dao: DataAccessObject) {
         withPathInfo(http, dao)?.run {
-            // TODO: throw validator exception instead of using defaults
             val issue = Issue(
                 http.param("id")?.toIntOrNull() ?: -1,
                 project
--- a/src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt	Tue Aug 03 13:41:32 2021 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt	Tue Aug 03 14:08:08 2021 +0200
@@ -96,15 +96,14 @@
             http.renderCommit("users/")
         } else {
             val errorMessages = mutableListOf<String>()
-            val username = http.param("username", {
+            user.username = http.param("username", {
                 if (it == null) ValidationError("validation.username.null")
                 else if (dao.findUserByName(it) != null) ValidationError("validation.username.unique")
                 else ValidatedValue(it)
-            }, errorMessages)
+            }, "", errorMessages)
 
-            if (username != null) {
-                logger().info("Insert user ${username}.")
-                user.username = username
+            if (errorMessages.isEmpty()) {
+                logger().info("Insert user ${user.username}.")
                 dao.insertUser(user)
                 http.renderCommit("users/")
             } else {

mercurial