🎨 Clean up imports
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / EditProfileFieldPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.*
9 import javax.inject.*
10
11 /**
12  * Page that lets the user edit the name of a profile field.
13  */
14 @TemplatePath("/templates/editProfileField.html")
15 class EditProfileFieldPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
16                 LoggedInPage("editProfileField.html", "Page.EditProfileField.Title", webInterface, loaders, templateRenderer) {
17
18         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 currentSone.profile.let { profile ->
20                         if (soneRequest.isPOST) {
21                                 if (soneRequest.httpRequest.getPartAsStringFailsafe("cancel", 4) == "true") {
22                                         throw RedirectException("editProfile.html#profile-fields")
23                                 }
24                                 val field = profile.getFieldById(soneRequest.httpRequest.getPartAsStringFailsafe("field", 36)) ?: throw RedirectException("invalid.html")
25                                 soneRequest.httpRequest.getPartAsStringFailsafe("name", 256).let { name ->
26                                         try {
27                                                 if (name != field.name) {
28                                                         field.name = name
29                                                         currentSone.profile = profile
30                                                 }
31                                                 throw RedirectException("editProfile.html#profile-fields")
32                                         } catch (e: IllegalArgumentException) {
33                                                 templateContext["duplicateFieldName"] = true
34                                                 return
35                                         }
36                                 }
37                         }
38                         templateContext["field"] = profile.getFieldById(soneRequest.httpRequest.getParam("field")) ?: throw RedirectException("invalid.html")
39                 }
40         }
41
42 }