From 9d33fea611607a4ce85c4e41669eced7000a4f04 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sat, 30 Aug 2014 00:19:52 +0200 Subject: [PATCH] albums now can be deleted --- Handler/AlbumSettings.hs | 63 ++++++++++++++++++++++++++++++++++ config/routes | 1 + templates/albumDelete.hamlet | 10 ++++++ templates/albumSettings.hamlet | 2 ++ 4 files changed, 76 insertions(+) create mode 100644 templates/albumDelete.hamlet diff --git a/Handler/AlbumSettings.hs b/Handler/AlbumSettings.hs index 76fd355..b682117 100644 --- a/Handler/AlbumSettings.hs +++ b/Handler/AlbumSettings.hs @@ -1,6 +1,9 @@ module Handler.AlbumSettings where import Import +import qualified Data.Text as T +import System.Directory +import System.FilePath getAlbumSettingsR :: AlbumId -> Handler Html getAlbumSettingsR albumId = do @@ -79,3 +82,63 @@ albumSettingsForm album albumId = renderDivs $ Album media = do entities <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle] 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 diff --git a/config/routes b/config/routes index bad2a93..df2e1be 100644 --- a/config/routes +++ b/config/routes @@ -16,3 +16,4 @@ /medium/#MediumId MediumR GET /album/#AlbumId/upload DirectUploadR GET POST /album/#AlbumId/settings AlbumSettingsR GET POST +/album/#AlbumId/delete AlbumDeleteR GET POST diff --git a/templates/albumDelete.hamlet b/templates/albumDelete.hamlet new file mode 100644 index 0000000..cc9d9f9 --- /dev/null +++ b/templates/albumDelete.hamlet @@ -0,0 +1,10 @@ +$newline always +

Delete Album #{albumTitle album} + +

+ Do you wish ti delete this album and all its containing images? + +

+