Use method from JsonPage
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / EditProfileFieldAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.ifFalse
5 import net.pterodactylus.sone.utils.parameters
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8
9 /**
10  * AJAX page that lets the user rename a profile field.
11  */
12 class EditProfileFieldAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("editProfileField.ajax", webInterface) {
13
14         override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
15                         currentSone.profile.let { profile ->
16                                 request.parameters["field"]!!
17                                                 .let(profile::getFieldById)
18                                                 ?.let { field ->
19                                                         request.parameters["name"]!!.trim().let { newName ->
20                                                                 newName.isBlank().ifFalse {
21                                                                         try {
22                                                                                 field.name = newName
23                                                                                 createSuccessJsonObject().also {
24                                                                                         currentSone.profile = profile
25                                                                                 }
26                                                                         } catch (_: IllegalArgumentException) {
27                                                                                 createErrorJsonObject("duplicate-field-name")
28                                                                         }
29                                                                 }
30                                                         } ?: createErrorJsonObject("invalid-parameter-name")
31                                                 } ?: createErrorJsonObject("invalid-field-id")
32                         }
33
34 }