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

changeset 184
e8eecee6aadf
child 209
c9c6abf167c7
equal deleted inserted replaced
183:61669abf277f 184:e8eecee6aadf
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 */
25
26 package de.uapcore.lightpit.servlet
27
28 import de.uapcore.lightpit.AbstractServlet
29 import de.uapcore.lightpit.HttpRequest
30 import de.uapcore.lightpit.LoggingTrait
31 import de.uapcore.lightpit.dao.DataAccessObject
32 import de.uapcore.lightpit.entities.User
33 import de.uapcore.lightpit.logger
34 import de.uapcore.lightpit.viewmodel.UserEditView
35 import de.uapcore.lightpit.viewmodel.UsersView
36 import javax.servlet.annotation.WebServlet
37
38 @WebServlet(urlPatterns = ["/users/*"])
39 class UsersServlet : AbstractServlet(), LoggingTrait {
40
41 init {
42 get("/", this::index)
43 get("/-/create", this::create)
44 get("/%userid/edit", this::edit)
45 post("/-/commit", this::commit)
46 }
47
48 private val list = "users"
49 private val form = "user-form"
50
51 fun index(http: HttpRequest, dao: DataAccessObject) {
52 with(http) {
53 view = UsersView(dao.listUsers())
54 render(list)
55 }
56 }
57
58 fun create(http: HttpRequest, dao: DataAccessObject) {
59 with(http) {
60 view = UserEditView(User(-1))
61 render(form)
62 }
63 }
64
65 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 }
81
82 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 }
88
89 val user = User(id)
90 with(user) {
91 username = http.param("username") ?: ""
92 givenname = http.param("givenname")
93 lastname = http.param("lastname")
94 mail = http.param("mail")
95 }
96
97 if (dao.findUserByName(user.username) != null) {
98 with(http) {
99 view = UserEditView(user).apply { errorText = "validation.username.unique" }
100 }
101 }
102
103 if (user.id > 0) {
104 logger().info("Update user ${user.username} with id ${user.id}.")
105 dao.updateUser(user)
106 } else {
107 logger().info("Insert user ${user.username}.")
108 dao.insertUser(user)
109 }
110 http.renderCommit("users/")
111 }
112 }

mercurial