🎨 Replace PageToadletFactory with Kotlin version
[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 import javax.inject.Inject
9
10 /**
11  * AJAX page that lets the user rename a profile field.
12  */
13 class EditProfileFieldAjaxPage @Inject constructor(webInterface: WebInterface) :
14                 LoggedInJsonPage("editProfileField.ajax", webInterface) {
15
16         override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
17                         currentSone.profile.let { profile ->
18                                 request.parameters["field"]!!
19                                                 .let(profile::getFieldById)
20                                                 ?.let { field ->
21                                                         request.parameters["name"]!!.trim().let { newName ->
22                                                                 newName.isBlank().ifFalse {
23                                                                         try {
24                                                                                 field.name = newName
25                                                                                 createSuccessJsonObject().also {
26                                                                                         currentSone.profile = profile
27                                                                                 }
28                                                                         } catch (_: IllegalArgumentException) {
29                                                                                 createErrorJsonObject("duplicate-field-name")
30                                                                         }
31                                                                 }
32                                                         } ?: createErrorJsonObject("invalid-parameter-name")
33                                                 } ?: createErrorJsonObject("invalid-field-id")
34                         }
35
36 }