Replace like page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / EditAlbumPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
4 import net.pterodactylus.sone.utils.isPOST
5 import net.pterodactylus.sone.web.pages.SoneTemplatePage
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10
11 /**
12  * Page that lets the user edit the name and description of an album.
13  */
14 class EditAlbumPage(template: Template, webInterface: WebInterface):
15                 SoneTemplatePage("editAlbum.html", template, "Page.EditAlbum.Title", webInterface, true) {
16
17         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
18                 if (request.isPOST) {
19                         val album = webInterface.core.getAlbum(request.httpRequest.getPartAsStringFailsafe("album", 36)) ?: throw RedirectException("invalid.html")
20                         album.takeUnless { it.sone.isLocal }?.run { throw RedirectException("noPermission.html") }
21                         if (request.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
22                                 album.parent?.moveAlbumUp(album)
23                                 webInterface.core.touchConfiguration()
24                                 throw RedirectException("imageBrowser.html?album=${album.parent?.id}")
25                         } else if (request.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
26                                 album.parent?.moveAlbumDown(album)
27                                 webInterface.core.touchConfiguration()
28                                 throw RedirectException("imageBrowser.html?album=${album.parent?.id}")
29                         } else {
30                                 try {
31                                         album.modify()
32                                                         .setTitle(request.httpRequest.getPartAsStringFailsafe("title", 100))
33                                                         .setDescription(request.httpRequest.getPartAsStringFailsafe("description", 1000))
34                                                         .update()
35                                 } catch (e: AlbumTitleMustNotBeEmpty) {
36                                         throw RedirectException("emptyAlbumTitle.html")
37                                 }
38                                 webInterface.core.touchConfiguration()
39                                 throw RedirectException("imageBrowser.html?album=${album.id}")
40                         }
41                 }
42         }
43
44 }