Wed, 28 Dec 2022 13:21:30 +0100
#233 migrate to Jakarta EE and update dependencies
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.UsersView
36 import jakarta.servlet.annotation.WebServlet
38 @WebServlet(urlPatterns = ["/users/*"])
39 class UsersServlet : AbstractServlet() {
41 init {
42 get("/", this::index)
43 get("/-/create", this::create)
44 get("/%userid/edit", this::edit)
45 post("/-/commit", this::commit)
46 }
48 private val list = "users"
49 private val form = "user-form"
51 private fun index(http: HttpRequest, dao: DataAccessObject) {
52 with(http) {
53 view = UsersView(dao.listUsers())
54 render(list)
55 }
56 }
58 private fun create(http: HttpRequest, dao: DataAccessObject) {
59 with(http) {
60 view = UserEditView(User(-1))
61 render(form)
62 }
63 }
65 private fun edit(http: HttpRequest, dao: DataAccessObject) {
66 val id = http.pathParams["userid"]?.toIntOrNull()
67 if (id == null) {
68 http.response.sendError(404)
69 } else {
70 val user = dao.findUser(id)
71 if (user == null) {
72 http.response.sendError(404)
73 } else {
74 with(http) {
75 view = UserEditView(user)
76 render(form)
77 }
78 }
79 }
80 }
82 private fun commit(http: HttpRequest, dao: DataAccessObject) {
83 val id = http.param("userid")?.toIntOrNull()
84 if (id == null) {
85 http.response.sendError(400)
86 return
87 }
89 val user = User(id)
90 with(user) {
91 givenname = http.param("givenname")
92 lastname = http.param("lastname")
93 mail = http.param("mail")
94 }
96 if (user.id > 0) {
97 dao.updateUser(user)
98 http.renderCommit("users/")
99 } else {
100 val errorMessages = mutableListOf<String>()
101 user.username = http.param("username", {
102 if (it == null) ValidationError("validation.username.null")
103 else if (dao.findUserByName(it) != null) ValidationError("validation.username.unique")
104 else ValidatedValue(it)
105 }, "", errorMessages)
107 if (errorMessages.isEmpty()) {
108 dao.insertUser(user)
109 http.renderCommit("users/")
110 } else {
111 http.view = UserEditView(user).apply { this.errorMessages = errorMessages }
112 http.render(form)
113 }
114 }
115 }
116 }