# HG changeset patch # User Mike Becker # Date 1627992488 -7200 # Node ID 37fbdcb422b707b85d52ee85a35844f58c996cd5 # Parent c9c6abf167c7fe5ff65ac31f37bc8571cba57a36 minor code changes diff -r c9c6abf167c7 -r 37fbdcb422b7 src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt --- 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 = request.getParameterValues(name) ?: emptyArray() - fun param(name: String, validator: (String?) -> (ValidationResult), errorMessages: MutableList): T? { + fun param(name: String, validator: (String?) -> (ValidationResult), + defaultValue: T, errorMessages: MutableList): T { return when (val result = validator(param(name))) { is ValidationError -> { errorMessages.add(i18n(result.message)) - null + defaultValue } is ValidatedValue -> { result.result diff -r c9c6abf167c7 -r 37fbdcb422b7 src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt --- 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 { - class NotFound : LookupResult() - data class Found(val elem: T?) : LookupResult() - } + private sealed interface LookupResult + private class NotFound : LookupResult + private data class Found(val elem: T?) : LookupResult private fun HttpRequest.lookupPathParam(paramName: String, list: List): LookupResult { 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? { 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 diff -r c9c6abf167c7 -r 37fbdcb422b7 src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt --- 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() - 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 {