9655815fa911bfd0cbd87373d5fb4bb32377f7f5
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / DeleteProfileFieldPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.isPOST
5 import net.pterodactylus.sone.web.WebInterface
6 import net.pterodactylus.sone.web.page.*
7 import net.pterodactylus.util.template.Template
8 import net.pterodactylus.util.template.TemplateContext
9 import javax.inject.Inject
10
11 /**
12  * Page that lets the user confirm the deletion of a profile field.
13  */
14 class DeleteProfileFieldPage @Inject constructor(template: Template, webInterface: WebInterface):
15                 LoggedInPage("deleteProfileField.html", template, "Page.DeleteProfileField.Title", webInterface) {
16
17         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
18                 if (soneRequest.isPOST) {
19                         val field = currentSone.profile.getFieldById(soneRequest.httpRequest.getPartAsStringFailsafe("field", 36)) ?: throw RedirectException("invalid.html")
20                         if (soneRequest.httpRequest.getPartAsStringFailsafe("confirm", 4) == "true") {
21                                 currentSone.profile = currentSone.profile.apply { removeField(field) }
22                         }
23                         throw RedirectException("editProfile.html#profile-fields")
24                 }
25                 val field = currentSone.profile.getFieldById(soneRequest.httpRequest.getParam("field")) ?: throw RedirectException("invalid.html")
26                 templateContext["field"] = field
27         }
28
29 }