src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt

Thu, 29 Dec 2022 14:50:35 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 29 Dec 2022 14:50:35 +0100
changeset 257
c1be672af7ff
parent 254
55ca6cafc3dd
permissions
-rw-r--r--

#164 add issue summary for developers

     1 /*
     2  * Copyright 2021 Mike Becker. All rights reserved.
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions are met:
     6  *
     7  * 1. Redistributions of source code must retain the above copyright
     8  * notice, this list of conditions and the following disclaimer.
     9  *
    10  * 2. Redistributions in binary form must reproduce the above copyright
    11  * notice, this list of conditions and the following disclaimer in the
    12  * documentation and/or other materials provided with the distribution.
    13  *
    14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    24  */
    26 package de.uapcore.lightpit.servlet
    28 import de.uapcore.lightpit.AbstractServlet
    29 import de.uapcore.lightpit.HttpRequest
    30 import de.uapcore.lightpit.ValidatedValue
    31 import de.uapcore.lightpit.ValidationError
    32 import de.uapcore.lightpit.dao.DataAccessObject
    33 import de.uapcore.lightpit.entities.User
    34 import de.uapcore.lightpit.viewmodel.UserEditView
    35 import de.uapcore.lightpit.viewmodel.UserViewData
    36 import de.uapcore.lightpit.viewmodel.UsersView
    37 import jakarta.servlet.annotation.WebServlet
    39 @WebServlet(urlPatterns = ["/users/*"])
    40 class UsersServlet : AbstractServlet() {
    42     init {
    43         get("/", this::index)
    44         get("/-/create", this::create)
    45         get("/%userid/edit", this::edit)
    46         post("/-/commit", this::commit)
    47     }
    49     private val list = "users"
    50     private val form = "user-form"
    52     private fun index(http: HttpRequest, dao: DataAccessObject) {
    53         with(http) {
    54             view = UsersView(dao.listUsers().map {
    55                 UserViewData(it, dao.collectIssueSummary(it))
    56             })
    57             render(list)
    58         }
    59     }
    61     private fun create(http: HttpRequest, dao: DataAccessObject) {
    62         with(http) {
    63             view = UserEditView(User(-1))
    64             render(form)
    65         }
    66     }
    68     private fun edit(http: HttpRequest, dao: DataAccessObject) {
    69         val id = http.pathParams["userid"]?.toIntOrNull()
    70         if (id == null) {
    71             http.response.sendError(404)
    72         } else {
    73             val user = dao.findUser(id)
    74             if (user == null) {
    75                 http.response.sendError(404)
    76             } else {
    77                 with(http) {
    78                     view = UserEditView(user)
    79                     render(form)
    80                 }
    81             }
    82         }
    83     }
    85     private fun commit(http: HttpRequest, dao: DataAccessObject) {
    86         val id = http.param("userid")?.toIntOrNull()
    87         if (id == null) {
    88             http.response.sendError(400)
    89             return
    90         }
    92         val user = User(id)
    93         with(user) {
    94             givenname = http.param("givenname")
    95             lastname = http.param("lastname")
    96             mail = http.param("mail")
    97         }
    99         if (user.id > 0) {
   100             dao.updateUser(user)
   101             http.renderCommit("users/")
   102         } else {
   103             val errorMessages = mutableListOf<String>()
   104             user.username = http.param("username", {
   105                 if (it == null) ValidationError("validation.username.null")
   106                 else if (dao.findUserByName(it) != null) ValidationError("validation.username.unique")
   107                 else ValidatedValue(it)
   108             }, "", errorMessages)
   110             if (errorMessages.isEmpty()) {
   111                 dao.insertUser(user)
   112                 http.renderCommit("users/")
   113             } else {
   114                 http.view = UserEditView(user).apply { this.errorMessages = errorMessages }
   115                 http.render(form)
   116             }
   117         }
   118     }
   119 }

mercurial