Tue, 03 Aug 2021 14:08:08 +0200
minor code changes
1.1 --- a/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt Tue Aug 03 13:41:32 2021 +0200 1.2 +++ b/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt Tue Aug 03 14:08:08 2021 +0200 1.3 @@ -159,11 +159,12 @@ 1.4 fun param(name: String): String? = request.getParameter(name) 1.5 fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray() 1.6 1.7 - fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), errorMessages: MutableList<String>): T? { 1.8 + fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), 1.9 + defaultValue: T, errorMessages: MutableList<String>): T { 1.10 return when (val result = validator(param(name))) { 1.11 is ValidationError -> { 1.12 errorMessages.add(i18n(result.message)) 1.13 - null 1.14 + defaultValue 1.15 } 1.16 is ValidatedValue -> { 1.17 result.result
2.1 --- a/src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt Tue Aug 03 13:41:32 2021 +0200 2.2 +++ b/src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt Tue Aug 03 14:08:08 2021 +0200 2.3 @@ -103,21 +103,20 @@ 2.4 selectedComponent 2.5 ) 2.6 2.7 - sealed class LookupResult<T> { 2.8 - class NotFound<T> : LookupResult<T>() 2.9 - data class Found<T>(val elem: T?) : LookupResult<T>() 2.10 - } 2.11 + private sealed interface LookupResult<T> 2.12 + private class NotFound<T> : LookupResult<T> 2.13 + private data class Found<T>(val elem: T?) : LookupResult<T> 2.14 2.15 private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> { 2.16 val node = pathParams[paramName] 2.17 return if (node == null || node == "-") { 2.18 - LookupResult.Found(null) 2.19 + Found(null) 2.20 } else { 2.21 val result = list.find { it.node == node } 2.22 if (result == null) { 2.23 - LookupResult.NotFound() 2.24 + NotFound() 2.25 } else { 2.26 - LookupResult.Found(result) 2.27 + Found(result) 2.28 } 2.29 } 2.30 } 2.31 @@ -147,7 +146,7 @@ 2.32 2.33 private fun feedPath(project: Project) = "feed/${project.node}/issues.rss" 2.34 2.35 - data class PathInfos( 2.36 + private data class PathInfos( 2.37 val projectInfo: ProjectInfo, 2.38 val version: Version?, 2.39 val component: Component? 2.40 @@ -164,20 +163,20 @@ 2.41 } 2.42 2.43 val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) { 2.44 - is LookupResult.NotFound -> { 2.45 + is NotFound -> { 2.46 http.response.sendError(404) 2.47 return null 2.48 } 2.49 - is LookupResult.Found -> { 2.50 + is Found -> { 2.51 result.elem 2.52 } 2.53 } 2.54 val component = when (val result = http.lookupPathParam("component", projectInfo.components)) { 2.55 - is LookupResult.NotFound -> { 2.56 + is NotFound -> { 2.57 http.response.sendError(404) 2.58 return null 2.59 } 2.60 - is LookupResult.Found -> { 2.61 + is Found -> { 2.62 result.elem 2.63 } 2.64 } 2.65 @@ -231,7 +230,6 @@ 2.66 } 2.67 2.68 private fun projectCommit(http: HttpRequest, dao: DataAccessObject) { 2.69 - // TODO: replace defaults with throwing validator exceptions 2.70 val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply { 2.71 name = http.param("name") ?: "" 2.72 node = http.param("node") ?: "" 2.73 @@ -288,11 +286,11 @@ 2.74 2.75 val version: Version 2.76 when (val result = http.lookupPathParam("version", projectInfo.versions)) { 2.77 - is LookupResult.NotFound -> { 2.78 + is NotFound -> { 2.79 http.response.sendError(404) 2.80 return 2.81 } 2.82 - is LookupResult.Found -> { 2.83 + is Found -> { 2.84 version = result.elem ?: Version(-1, projectInfo.project.id) 2.85 } 2.86 } 2.87 @@ -310,17 +308,23 @@ 2.88 } 2.89 } 2.90 2.91 - private fun versionCommit(http: HttpRequest, dao: DataAccessObject) { 2.92 + private fun obtainIdAndProject(http: HttpRequest, dao:DataAccessObject): Pair<Int, Project>? { 2.93 val id = http.param("id")?.toIntOrNull() 2.94 val projectid = http.param("projectid")?.toIntOrNull() ?: -1 2.95 val project = dao.findProject(projectid) 2.96 - if (id == null || project == null) { 2.97 + return if (id == null || project == null) { 2.98 http.response.sendError(400) 2.99 - return 2.100 + null 2.101 + } else { 2.102 + Pair(id, project) 2.103 } 2.104 + } 2.105 2.106 - // TODO: replace defaults with throwing validator exceptions 2.107 - val version = Version(id, projectid).apply { 2.108 + private fun versionCommit(http: HttpRequest, dao: DataAccessObject) { 2.109 + val idParams = obtainIdAndProject(http, dao) ?: return 2.110 + val (id, project) = idParams 2.111 + 2.112 + val version = Version(id, project.id).apply { 2.113 name = http.param("name") ?: "" 2.114 node = http.param("node") ?: "" 2.115 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 2.116 @@ -372,11 +376,11 @@ 2.117 2.118 val component: Component 2.119 when (val result = http.lookupPathParam("component", projectInfo.components)) { 2.120 - is LookupResult.NotFound -> { 2.121 + is NotFound -> { 2.122 http.response.sendError(404) 2.123 return 2.124 } 2.125 - is LookupResult.Found -> { 2.126 + is Found -> { 2.127 component = result.elem ?: Component(-1, projectInfo.project.id) 2.128 } 2.129 } 2.130 @@ -395,16 +399,10 @@ 2.131 } 2.132 2.133 private fun componentCommit(http: HttpRequest, dao: DataAccessObject) { 2.134 - val id = http.param("id")?.toIntOrNull() 2.135 - val projectid = http.param("projectid")?.toIntOrNull() ?: -1 2.136 - val project = dao.findProject(projectid) 2.137 - if (id == null || project == null) { 2.138 - http.response.sendError(400) 2.139 - return 2.140 - } 2.141 + val idParams = obtainIdAndProject(http, dao) ?: return 2.142 + val (id, project) = idParams 2.143 2.144 - // TODO: replace defaults with throwing validator exceptions 2.145 - val component = Component(id, projectid).apply { 2.146 + val component = Component(id, project.id).apply { 2.147 name = http.param("name") ?: "" 2.148 node = http.param("node") ?: "" 2.149 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 2.150 @@ -506,8 +504,6 @@ 2.151 return 2.152 } 2.153 2.154 - // TODO: throw validator exception instead of using a default 2.155 - 2.156 val commentId = http.param("commentid")?.toIntOrNull() ?: -1 2.157 if (commentId > 0) { 2.158 val comment = dao.findComment(commentId) 2.159 @@ -533,7 +529,6 @@ 2.160 2.161 private fun issueCommit(http: HttpRequest, dao: DataAccessObject) { 2.162 withPathInfo(http, dao)?.run { 2.163 - // TODO: throw validator exception instead of using defaults 2.164 val issue = Issue( 2.165 http.param("id")?.toIntOrNull() ?: -1, 2.166 project
3.1 --- a/src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt Tue Aug 03 13:41:32 2021 +0200 3.2 +++ b/src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt Tue Aug 03 14:08:08 2021 +0200 3.3 @@ -96,15 +96,14 @@ 3.4 http.renderCommit("users/") 3.5 } else { 3.6 val errorMessages = mutableListOf<String>() 3.7 - val username = http.param("username", { 3.8 + user.username = http.param("username", { 3.9 if (it == null) ValidationError("validation.username.null") 3.10 else if (dao.findUserByName(it) != null) ValidationError("validation.username.unique") 3.11 else ValidatedValue(it) 3.12 - }, errorMessages) 3.13 + }, "", errorMessages) 3.14 3.15 - if (username != null) { 3.16 - logger().info("Insert user ${username}.") 3.17 - user.username = username 3.18 + if (errorMessages.isEmpty()) { 3.19 + logger().info("Insert user ${user.username}.") 3.20 dao.insertUser(user) 3.21 http.renderCommit("users/") 3.22 } else {