X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FMoveProfileFieldAjaxPage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FMoveProfileFieldAjaxPage.kt;h=e2b039b77efd6172f02d87f14ec5ddd990c6aeb6;hb=c141e4c4a691323f83c209ffa0df927e989aa2ce;hp=0000000000000000000000000000000000000000;hpb=81adf8a620dbe1ad0442575f2f2e2f4305e67bca;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.kt new file mode 100644 index 0000000..e2b039b --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.kt @@ -0,0 +1,41 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.Profile +import net.pterodactylus.sone.data.Profile.Field +import net.pterodactylus.sone.data.Sone +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 move a profile field up or down. + * + * @see net.pterodactylus.sone.data.Profile#moveFieldUp(Field) + * @see net.pterodactylus.sone.data.Profile#moveFieldDown(Field) + */ +class MoveProfileFieldAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("moveProfileField.ajax", webInterface) { + + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + currentSone.profile.let { profile -> + request.parameters["field"] + ?.let(profile::getFieldById) + ?.let { processField(currentSone, profile, it, request.parameters["direction"]) } + ?: createErrorJsonObject("invalid-field-id") + } + + private fun processField(currentSone: Sone, profile: Profile, field: Field, direction: String?) = + try { + when (direction) { + "up" -> profile.moveFieldUp(field) + "down" -> profile.moveFieldDown(field) + else -> null + }?.let { + currentSone.profile = profile + webInterface.core.touchConfiguration() + createSuccessJsonObject() + } ?: createErrorJsonObject("invalid-direction") + } catch (e: IllegalArgumentException) { + createErrorJsonObject("not-possible") + } + +}