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) }}
{% endif -%}
<form method="post" enctype="multipart/form-data">
<form method="post">
{% import "components/forms.html" as forms %}
{{ forms.input({

View File

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

View File

@ -32,10 +32,14 @@ class UserController(
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
val password = (parts.find { it.name == "password" } as PartData.FormItem).value
if (username == null || password == null) {
call.respondKorte(template)
return
}
val user = userRepository.find(username)
@ -89,10 +93,14 @@ class UserController(
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
val password = (parts.find { it.name == "password" } as PartData.FormItem).value
if (username == null || password == null) {
call.respondKorte(template)
return
}
val validation = registerValidator.validate(
User {