Replace delete profile field ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / DeleteProfileFieldAjaxPage.kt
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.kt
new file mode 100644 (file)
index 0000000..5e7c901
--- /dev/null
@@ -0,0 +1,27 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+
+/**
+ * AJAX page that lets the user delete a profile field.
+ */
+class DeleteProfileFieldAjaxPage(webInterface: WebInterface) : JsonPage("deleteProfileField.ajax", webInterface) {
+
+       override fun createJsonObject(request: FreenetRequest) =
+                       getCurrentSone(request.toadletContext)!!.let { currentSone ->
+                               currentSone.profile.let { profile ->
+                                       request.parameters["field"]
+                                                       ?.let(profile::getFieldById)
+                                                       ?.let { field ->
+                                                               createSuccessJsonObject().also {
+                                                                       profile.removeField(field)
+                                                                       currentSone.profile = profile
+                                                                       webInterface.core.touchConfiguration()
+                                                               }
+                                                       } ?: createErrorJsonObject("invalid-field-id")
+                               }
+                       }
+
+}