🚧 Add Loaders to all template-using pages
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / CreateAlbumPage.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.text.TextFilter
7 import net.pterodactylus.sone.utils.isPOST
8 import net.pterodactylus.sone.web.WebInterface
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.template.Template
11 import net.pterodactylus.util.template.TemplateContext
12 import javax.inject.Inject
13
14 /**
15  * Page that lets the user create a new album.
16  */
17 class CreateAlbumPage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders):
18                 LoggedInPage("createAlbum.html", template, "Page.CreateAlbum.Title", webInterface, loaders) {
19
20         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
21                 if (soneRequest.isPOST) {
22                         val name = soneRequest.httpRequest.getPartAsStringFailsafe("name", 64).trim()
23                         if (name.isEmpty()) {
24                                 templateContext["nameMissing"] = true
25                                 return
26                         }
27                         val description = soneRequest.httpRequest.getPartAsStringFailsafe("description", 256).trim()
28                         val parentId = soneRequest.httpRequest.getPartAsStringFailsafe("parent", 36)
29                         val parent = if (parentId == "") currentSone.rootAlbum else soneRequest.core.getAlbum(parentId)
30                         val album = soneRequest.core.createAlbum(currentSone, parent)
31                         try {
32                                 album.modify().apply {
33                                         setTitle(name)
34                                         setDescription(TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), description))
35                                 }.update()
36                         } catch (e: AlbumTitleMustNotBeEmpty) {
37                                 throw RedirectException("emptyAlbumTitle.html")
38                         }
39                         soneRequest.core.touchConfiguration()
40                         throw RedirectException("imageBrowser.html?album=${album.id}")
41                 }
42         }
43
44 }