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

Mon, 09 Aug 2021 16:25:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 09 Aug 2021 16:25:50 +0200
changeset 215
028792eda9b7
parent 210
37fbdcb422b7
child 247
e71ae69c68c0
permissions
-rw-r--r--

#156 fixes auto-selection overriding issue data

     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.*
    29 import de.uapcore.lightpit.dao.DataAccessObject
    30 import de.uapcore.lightpit.entities.User
    31 import de.uapcore.lightpit.viewmodel.UserEditView
    32 import de.uapcore.lightpit.viewmodel.UsersView
    33 import javax.servlet.annotation.WebServlet
    35 @WebServlet(urlPatterns = ["/users/*"])
    36 class UsersServlet : AbstractServlet(), LoggingTrait {
    38     init {
    39         get("/", this::index)
    40         get("/-/create", this::create)
    41         get("/%userid/edit", this::edit)
    42         post("/-/commit", this::commit)
    43     }
    45     private val list = "users"
    46     private val form = "user-form"
    48     private fun index(http: HttpRequest, dao: DataAccessObject) {
    49         with(http) {
    50             view = UsersView(dao.listUsers())
    51             render(list)
    52         }
    53     }
    55     private fun create(http: HttpRequest, dao: DataAccessObject) {
    56         with(http) {
    57             view = UserEditView(User(-1))
    58             render(form)
    59         }
    60     }
    62     private fun edit(http: HttpRequest, dao: DataAccessObject) {
    63         val id = http.pathParams["userid"]?.toIntOrNull()
    64         if (id == null) {
    65             http.response.sendError(404)
    66         } else {
    67             val user = dao.findUser(id)
    68             if (user == null) {
    69                 http.response.sendError(404)
    70             } else {
    71                 with(http) {
    72                     view = UserEditView(user)
    73                     render(form)
    74                 }
    75             }
    76         }
    77     }
    79     private fun commit(http: HttpRequest, dao: DataAccessObject) {
    80         val id = http.param("userid")?.toIntOrNull()
    81         if (id == null) {
    82             http.response.sendError(400)
    83             return
    84         }
    86         val user = User(id)
    87         with(user) {
    88             givenname = http.param("givenname")
    89             lastname = http.param("lastname")
    90             mail = http.param("mail")
    91         }
    93         if (user.id > 0) {
    94             logger().info("Update user with id ${user.id}.")
    95             dao.updateUser(user)
    96             http.renderCommit("users/")
    97         } else {
    98             val errorMessages = mutableListOf<String>()
    99             user.username = http.param("username", {
   100                 if (it == null) ValidationError("validation.username.null")
   101                 else if (dao.findUserByName(it) != null) ValidationError("validation.username.unique")
   102                 else ValidatedValue(it)
   103             }, "", errorMessages)
   105             if (errorMessages.isEmpty()) {
   106                 logger().info("Insert user ${user.username}.")
   107                 dao.insertUser(user)
   108                 http.renderCommit("users/")
   109             } else {
   110                 http.view = UserEditView(user).apply { this.errorMessages = errorMessages }
   111                 http.render(form)
   112             }
   113         }
   114     }
   115 }

mercurial