src/main/kotlin/de/uapcore/lightpit/viewmodel/PathInfos.kt

Mon, 30 Oct 2023 14:44:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Oct 2023 14:44:36 +0100
changeset 292
703591e739f4
permissions
-rw-r--r--

add possibility to show issues w/o version or component - fixes #335

universe@292 1 /*
universe@292 2 * Copyright 2023 Mike Becker. All rights reserved.
universe@292 3 *
universe@292 4 * Redistribution and use in source and binary forms, with or without
universe@292 5 * modification, are permitted provided that the following conditions are met:
universe@292 6 *
universe@292 7 * 1. Redistributions of source code must retain the above copyright
universe@292 8 * notice, this list of conditions and the following disclaimer.
universe@292 9 *
universe@292 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@292 11 * notice, this list of conditions and the following disclaimer in the
universe@292 12 * documentation and/or other materials provided with the distribution.
universe@292 13 *
universe@292 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@292 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@292 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
universe@292 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
universe@292 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
universe@292 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
universe@292 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
universe@292 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
universe@292 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
universe@292 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@292 24 */
universe@292 25
universe@292 26 package de.uapcore.lightpit.viewmodel
universe@292 27
universe@292 28 import de.uapcore.lightpit.HttpRequest
universe@292 29 import de.uapcore.lightpit.OptionalPathInfo
universe@292 30 import de.uapcore.lightpit.dao.DataAccessObject
universe@292 31 import de.uapcore.lightpit.entities.Component
universe@292 32 import de.uapcore.lightpit.entities.Version
universe@292 33
universe@292 34 data class PathInfos(
universe@292 35 val projectInfo: ProjectInfo,
universe@292 36 val versionInfo: OptionalPathInfo<Version>,
universe@292 37 val componentInfo: OptionalPathInfo<Component>
universe@292 38 ) {
universe@292 39 val issuesHref by lazyOf("projects/${projectInfo.project.node}/issues/${versionInfo.node}/${componentInfo.node}/")
universe@292 40 }
universe@292 41
universe@292 42 private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? {
universe@292 43 val pathParam = http.pathParams["project"] ?: return null
universe@292 44 val project = dao.findProjectByNode(pathParam) ?: return null
universe@292 45
universe@292 46 val versions: List<Version> = dao.listVersions(project)
universe@292 47 val components: List<Component> = dao.listComponents(project)
universe@292 48
universe@292 49 return ProjectInfo(
universe@292 50 project,
universe@292 51 versions,
universe@292 52 components,
universe@292 53 dao.collectIssueSummary(project)
universe@292 54 )
universe@292 55 }
universe@292 56
universe@292 57 fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? {
universe@292 58 val projectInfo = obtainProjectInfo(http, dao)
universe@292 59 if (projectInfo == null) {
universe@292 60 http.response.sendError(404)
universe@292 61 return null
universe@292 62 }
universe@292 63
universe@292 64 val version = http.lookupPathParam("version", projectInfo.versions)
universe@292 65 val component = http.lookupPathParam("component", projectInfo.components)
universe@292 66
universe@292 67 if (version == OptionalPathInfo.NotFound || component == OptionalPathInfo.NotFound) {
universe@292 68 http.response.sendError(404)
universe@292 69 return null
universe@292 70 }
universe@292 71
universe@292 72 return PathInfos(projectInfo, version, component)
universe@292 73 }

mercurial