Simplify form handling

This commit is contained in:
Hubert Van De Walle 2020-07-19 01:53:18 +02:00
parent d419b4c72a
commit a11450cbcf
3 changed files with 20 additions and 24 deletions

View File

@ -14,7 +14,7 @@
{{ alerts.warning("Error", error) }} {{ alerts.warning("Error", error) }}
{% endif -%} {% endif -%}
<form method="post" enctype="multipart/form-data"> <form method="post">
{% import "components/forms.html" as forms %} {% import "components/forms.html" as forms %}
{{ forms.input({ {{ forms.input({

View File

@ -46,24 +46,12 @@ class NoteController(
suspend fun new(call: ApplicationCall) { suspend fun new(call: ApplicationCall) {
val template = templates.get("new.html") val template = templates.get("new.html")
if (call.request.httpMethod == HttpMethod.Get || if (call.request.httpMethod == HttpMethod.Get) return call.respondKorte(template)
!call.request.contentType().withoutParameters().match(MultiPart.FormData)
) {
call.respondKorte(template)
return
}
val multipart = call.receiveMultipart() val params = call.receiveParameters()
val part = multipart.readPart() val txt = params["markdown"] ?: return call.respondKorte(template)
if (part == null || part !is PartData.FormItem || part.name != "markdown") { val result = md.renderDocument(txt)
call.respondKorte(templates.get("error.html"), "error" to "null", statusCode = BadRequest)
return
}
val textAreaValue = part.value
val result = md.renderDocument(textAreaValue)
val doc = when (result) { val doc = when (result) {
is Error -> return call.respondKorte( is Error -> return call.respondKorte(
@ -77,7 +65,7 @@ class NoteController(
val note = Note { val note = Note {
this.title = doc.meta.title this.title = doc.meta.title
this.tags = doc.meta.tags this.tags = doc.meta.tags
this.markdown = textAreaValue this.markdown = txt
this.html = doc.html this.html = doc.html
} }

View File

@ -32,10 +32,14 @@ class UserController(
return return
} }
val parts = call.receiveMultipart().readAllParts() val params = call.receiveParameters()
val username = params["username"]
val password = params["password"]
val username = (parts.find { it.name == "username" } as PartData.FormItem).value if (username == null || password == null) {
val password = (parts.find { it.name == "password" } as PartData.FormItem).value call.respondKorte(template)
return
}
val user = userRepository.find(username) val user = userRepository.find(username)
@ -89,10 +93,14 @@ class UserController(
return return
} }
val parts = call.receiveMultipart().readAllParts() val params = call.receiveParameters()
val username = params["username"]
val password = params["password"]
val username = (parts.find { it.name == "username" } as PartData.FormItem).value if (username == null || password == null) {
val password = (parts.find { it.name == "password" } as PartData.FormItem).value call.respondKorte(template)
return
}
val validation = registerValidator.validate( val validation = registerValidator.validate(
User { User {