9595a8c865555e6c88b67416211075d901d29f69
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / DeleteProfileFieldAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.parameters
5 import net.pterodactylus.sone.web.WebInterface
6 import net.pterodactylus.sone.web.page.FreenetRequest
7
8 /**
9  * AJAX page that lets the user delete a profile field.
10  */
11 class DeleteProfileFieldAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("deleteProfileField.ajax", webInterface) {
12
13         override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
14                         currentSone.profile.let { profile ->
15                                 request.parameters["field"]
16                                                 ?.let(profile::getFieldById)
17                                                 ?.let { field ->
18                                                         createSuccessJsonObject().also {
19                                                                 profile.removeField(field)
20                                                                 currentSone.profile = profile
21                                                                 webInterface.core.touchConfiguration()
22                                                         }
23                                                 } ?: createErrorJsonObject("invalid-field-id")
24                         }
25
26 }