diff -r bbf4eb9a71f8 -r bf67e0ff7131 src/main/kotlin/de/uapcore/lightpit/servlet/IssuesServlet.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/kotlin/de/uapcore/lightpit/servlet/IssuesServlet.kt Mon Aug 05 18:40:47 2024 +0200 @@ -0,0 +1,102 @@ +package de.uapcore.lightpit.servlet + +import de.uapcore.lightpit.AbstractServlet +import de.uapcore.lightpit.HttpRequest +import de.uapcore.lightpit.dao.DataAccessObject +import de.uapcore.lightpit.entities.Issue +import de.uapcore.lightpit.logic.* +import de.uapcore.lightpit.viewmodel.* +import jakarta.servlet.annotation.WebServlet + +@WebServlet(urlPatterns = ["/issues/*"]) +class IssuesServlet : AbstractServlet() { + + private val pathInfos = PathInfosOnlyIssues("issues/") + + init { + get("/", this::issues) + + get("/%issue", this::issue) + get("/%issue/edit", this::issueForm) + post("/%issue/comment", this::issueComment) + post("/%issue/relation", this::issueRelation) + get("/%issue/removeRelation", this::issueRemoveRelation) + post("/%issue/commit", this::issueCommit) + } + + private fun issues(http: HttpRequest, dao: DataAccessObject) { + val filter = IssueFilter(http, dao) + val needRelationsMap = filter.onlyBlocker + val relationsMap = if (needRelationsMap) dao.getIssueRelationMap(filter.includeDone) else emptyMap() + + val issues = dao.listIssues(filter.includeDone) + .sortedWith(IssueSorter(filter.sortPrimary, filter.sortSecondary, filter.sortTertiary)) + .filter(issueFilterFunction(filter, relationsMap, http.remoteUser ?: "")) + + with(http) { + pageTitle = i18n("issues") + view = IssueOverview(issues, filter) + styleSheets = listOf("projects") + javascript = "issue-overview" + render("issues") + } + } + + private fun issue(http: HttpRequest, dao: DataAccessObject) { + val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) + if (issue == null) { + http.response.sendError(404) + return + } + renderIssueView(http, dao, issue, pathInfos) + } + + private fun issueForm(http: HttpRequest, dao: DataAccessObject) { + val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) + if (issue == null) { + http.response.sendError(404) + return + } + + with(http) { + view = IssueEditView( + issue, + dao.listVersions(issue.project), + dao.listComponents(issue.project), + dao.listUsers(), + issue.project + ) + styleSheets = listOf("projects") + javascript = "issue-editor" + render("issue-form") + } + } + + private fun issueComment(http: HttpRequest, dao: DataAccessObject) { + commitIssueComment(http, dao, pathInfos) + } + + private fun issueCommit(http: HttpRequest, dao: DataAccessObject) { + val reference = http.param("id")?.toIntOrNull()?.let(dao::findIssue) + if (reference == null) { + logger.warn("Cannot create issues while not in a project context.") + http.response.sendError(404) + return + } + val issue = Issue(reference.id, reference.project) + processIssueForm(issue, reference, http, dao) + if (http.param("save") != null) { + http.renderCommit("${pathInfos.issuesHref}${issue.id}") + } else { + http.renderCommit(pathInfos.issuesHref) + } + } + + private fun issueRelation(http: HttpRequest, dao: DataAccessObject) { + addIssueRelation(http, dao, pathInfos) + } + + private fun issueRemoveRelation(http: HttpRequest, dao: DataAccessObject) { + removeIssueRelation(http, dao, pathInfos) + } +} \ No newline at end of file