universe@292: /* universe@292: * Copyright 2023 Mike Becker. All rights reserved. universe@292: * universe@292: * Redistribution and use in source and binary forms, with or without universe@292: * modification, are permitted provided that the following conditions are met: universe@292: * universe@292: * 1. Redistributions of source code must retain the above copyright universe@292: * notice, this list of conditions and the following disclaimer. universe@292: * universe@292: * 2. Redistributions in binary form must reproduce the above copyright universe@292: * notice, this list of conditions and the following disclaimer in the universe@292: * documentation and/or other materials provided with the distribution. universe@292: * universe@292: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@292: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@292: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE universe@292: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE universe@292: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL universe@292: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR universe@292: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER universe@292: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, universe@292: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE universe@292: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. universe@292: */ universe@292: universe@292: package de.uapcore.lightpit.viewmodel universe@292: universe@292: import de.uapcore.lightpit.HttpRequest universe@292: import de.uapcore.lightpit.OptionalPathInfo universe@292: import de.uapcore.lightpit.dao.DataAccessObject universe@292: import de.uapcore.lightpit.entities.Component universe@292: import de.uapcore.lightpit.entities.Version universe@292: universe@292: data class PathInfos( universe@292: val projectInfo: ProjectInfo, universe@292: val versionInfo: OptionalPathInfo, universe@292: val componentInfo: OptionalPathInfo universe@292: ) { universe@292: val issuesHref by lazyOf("projects/${projectInfo.project.node}/issues/${versionInfo.node}/${componentInfo.node}/") universe@292: } universe@292: universe@292: private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? { universe@292: val pathParam = http.pathParams["project"] ?: return null universe@292: val project = dao.findProjectByNode(pathParam) ?: return null universe@292: universe@292: val versions: List = dao.listVersions(project) universe@292: val components: List = dao.listComponents(project) universe@292: universe@292: return ProjectInfo( universe@292: project, universe@292: versions, universe@292: components, universe@292: dao.collectIssueSummary(project) universe@292: ) universe@292: } universe@292: universe@292: fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? { universe@292: val projectInfo = obtainProjectInfo(http, dao) universe@292: if (projectInfo == null) { universe@292: http.response.sendError(404) universe@292: return null universe@292: } universe@292: universe@292: val version = http.lookupPathParam("version", projectInfo.versions) universe@292: val component = http.lookupPathParam("component", projectInfo.components) universe@292: universe@292: if (version == OptionalPathInfo.NotFound || component == OptionalPathInfo.NotFound) { universe@292: http.response.sendError(404) universe@292: return null universe@292: } universe@292: universe@292: return PathInfos(projectInfo, version, component) universe@292: }