albums now can be deleted

This commit is contained in:
nek0 2014-08-30 00:19:52 +02:00
parent 629908285f
commit 9d33fea611
4 changed files with 76 additions and 0 deletions

View file

@ -1,6 +1,9 @@
module Handler.AlbumSettings where module Handler.AlbumSettings where
import Import import Import
import qualified Data.Text as T
import System.Directory
import System.FilePath
getAlbumSettingsR :: AlbumId -> Handler Html getAlbumSettingsR :: AlbumId -> Handler Html
getAlbumSettingsR albumId = do getAlbumSettingsR albumId = do
@ -79,3 +82,63 @@ albumSettingsForm album albumId = renderDivs $ Album
media = do media = do
entities <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle] entities <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle]
optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumPath (entityVal med))) entities optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumPath (entityVal med))) entities
getAlbumDeleteR :: AlbumId -> Handler Html
getAlbumDeleteR albumId = do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
ownerId <- return $ albumOwner album
owner <- runDB $ getJust ownerId
ownerName <- return $ userName owner
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
presence <- return (userId == ownerId)
case presence of
True -> do
defaultLayout $ do
$(widgetFile "albumDelete")
False -> do
setMessage "You must own this album to delete it"
redirect $ AlbumR albumId
Nothing -> do
setMessage "You must be logged in to delete albums"
redirect $ LoginR
Nothing -> do
setMessage "This album does not exist"
redirect $ HomeR
postAlbumDeleteR :: AlbumId -> Handler Html
postAlbumDeleteR albumId = do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
ownerId <- return $ albumOwner album
owner <- runDB $ getJust ownerId
ownerName <- return $ userName owner
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
presence <- return (userId == ownerId)
case presence of
True -> do
confirm <- lookupPostParam "confirm"
case confirm of
Just "confirm" -> do
mapM (\a -> runDB $ delete a) (albumContent album)
runDB $ delete albumId
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> (T.unpack $ extractKey userId) </> (T.unpack $ extractKey albumId)
setMessage "Album deleted succesfully"
redirect $ HomeR
False -> do
setMessage "You must own this album to delete it"
redirect $ AlbumR albumId
Nothing -> do
setMessage "You must be logged in to delete albums"
redirect $ LoginR
Nothing -> do
setMessage "This album does not exist"
redirect $ HomeR

View file

@ -16,3 +16,4 @@
/medium/#MediumId MediumR GET /medium/#MediumId MediumR GET
/album/#AlbumId/upload DirectUploadR GET POST /album/#AlbumId/upload DirectUploadR GET POST
/album/#AlbumId/settings AlbumSettingsR GET POST /album/#AlbumId/settings AlbumSettingsR GET POST
/album/#AlbumId/delete AlbumDeleteR GET POST

View file

@ -0,0 +1,10 @@
$newline always
<h3>Delete Album #{albumTitle album}
<p>
Do you wish ti delete this album and all its containing images?
<form method="POST" action=@{AlbumDeleteR albumId}>
<label for="confirm">Really delete this album
<input type="checkbox" id="confirm" name="confirm" value="confirm" required>
<input id="delete" type="submit" value="Delete album">

View file

@ -5,3 +5,5 @@ $newline always
^{albumSettingsWidget} ^{albumSettingsWidget}
<div> <div>
<input type=submit value="Change settings"> <input type=submit value="Change settings">
<a href=@{AlbumDeleteR albumId}>Delete this album