668f6ebdef4fc19c69078bf5f4759333b44abe16
[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.data.Sone
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.utils.isPOST
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11 import javax.inject.Inject
12
13 /**
14  * Page that lets the user edit the name and description of an album.
15  */
16 class EditAlbumPage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
17                 LoggedInPage("editAlbum.html", template, "Page.EditAlbum.Title", webInterface, loaders, templateRenderer) {
18
19         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 if (soneRequest.isPOST) {
21                         val album = soneRequest.core.getAlbum(soneRequest.httpRequest.getPartAsStringFailsafe("album", 36)) ?: throw RedirectException("invalid.html")
22                         album.takeUnless { it.sone.isLocal }?.run { throw RedirectException("noPermission.html") }
23                         if (soneRequest.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
24                                 album.parent?.moveAlbumUp(album)
25                                 soneRequest.core.touchConfiguration()
26                                 throw RedirectException("imageBrowser.html?album=${album.parent?.id}")
27                         } else if (soneRequest.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
28                                 album.parent?.moveAlbumDown(album)
29                                 soneRequest.core.touchConfiguration()
30                                 throw RedirectException("imageBrowser.html?album=${album.parent?.id}")
31                         } else {
32                                 try {
33                                         album.modify()
34                                                         .setTitle(soneRequest.httpRequest.getPartAsStringFailsafe("title", 100))
35                                                         .setDescription(soneRequest.httpRequest.getPartAsStringFailsafe("description", 1000))
36                                                         .update()
37                                 } catch (e: AlbumTitleMustNotBeEmpty) {
38                                         throw RedirectException("emptyAlbumTitle.html")
39                                 }
40                                 soneRequest.core.touchConfiguration()
41                                 throw RedirectException("imageBrowser.html?album=${album.id}")
42                         }
43                 }
44         }
45
46 }