From 629908285fb4cacb2f0b792e749e86ae2c451e49 Mon Sep 17 00:00:00 2001 From: nek0 Date: Thu, 28 Aug 2014 18:08:56 +0200 Subject: [PATCH] album settings can now be changed --- Application.hs | 1 + Handler/AlbumSettings.hs | 81 ++++++++++++++++++++++++++++++++++ Handler/Profile.hs | 1 + config/models | 2 +- config/routes | 3 +- eidolon.cabal | 1 + templates/album.hamlet | 4 +- templates/albumSettings.hamlet | 7 +++ templates/profile.hamlet | 5 ++- 9 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 Handler/AlbumSettings.hs create mode 100644 templates/albumSettings.hamlet diff --git a/Application.hs b/Application.hs index b23b11f..1c5e05a 100644 --- a/Application.hs +++ b/Application.hs @@ -36,6 +36,7 @@ import Handler.Upload import Handler.NewAlbum import Handler.Album import Handler.Medium +import Handler.AlbumSettings -- This line actually creates our YesodDispatch instance. It is the second half -- of the call to mkYesodData which occurs in Foundation.hs. Please see the diff --git a/Handler/AlbumSettings.hs b/Handler/AlbumSettings.hs new file mode 100644 index 0000000..76fd355 --- /dev/null +++ b/Handler/AlbumSettings.hs @@ -0,0 +1,81 @@ +module Handler.AlbumSettings where + +import Import + +getAlbumSettingsR :: AlbumId -> Handler Html +getAlbumSettingsR 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 + (albumSettingsWidget, enctype) <- generateFormPost $ albumSettingsForm album albumId + defaultLayout $ do + $(widgetFile "albumSettings") + False -> do + setMessage "You must own this album to change its settings" + redirect $ AlbumR albumId + Nothing -> do + setMessage "You must be logged in to change settings" + redirect $ LoginR + Nothing -> do + setMessage "This album does not exist" + redirect $ HomeR + +postAlbumSettingsR :: AlbumId -> Handler Html +postAlbumSettingsR 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 + ((result, albumSettingsWidget), enctype) <- runFormPost $ albumSettingsForm album albumId + case result of + FormSuccess temp -> do + aId <- runDB $ update albumId + [ AlbumTitle =. albumTitle temp + , AlbumOwner =. albumOwner temp + , AlbumContent =. albumContent temp + , AlbumSamplePic =. albumSamplePic temp + ] + setMessage "Album settings changed succesfully" + redirect $ AlbumR albumId + _ -> do + setMessage "There was an error changing the settings" + redirect $ AlbumSettingsR albumId + False -> do + setMessage "You must own this album to change its settings" + redirect $ AlbumR albumId + Nothing -> do + setMessage "You must be logged in to change settings" + redirect $ LoginR + Nothing -> do + setMessage "This album does not exist" + redirect $ HomeR + +albumSettingsForm :: Album -> AlbumId -> Form Album +albumSettingsForm album albumId = renderDivs $ Album + <$> areq textField "Title" (Just $ albumTitle album) + <*> pure (albumOwner album) + <*> pure (albumContent album) + <*> aopt (selectField media) "Sample picture" (Just $ albumSamplePic album) + where + media = do + entities <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle] + optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumPath (entityVal med))) entities diff --git a/Handler/Profile.hs b/Handler/Profile.hs index f313bc5..0290334 100644 --- a/Handler/Profile.hs +++ b/Handler/Profile.hs @@ -2,6 +2,7 @@ module Handler.Profile where import Import import Data.Maybe +import qualified Data.Text as T getProfileR :: UserId -> Handler Html getProfileR ownerId = do diff --git a/config/models b/config/models index 85d4e66..77aa1ea 100644 --- a/config/models +++ b/config/models @@ -19,7 +19,7 @@ Album title Text owner UserId content [MediumId] - samplePic MediumId Maybe + samplePic FilePath Maybe deriving Medium title Text diff --git a/config/routes b/config/routes index 74aa89a..bad2a93 100644 --- a/config/routes +++ b/config/routes @@ -14,4 +14,5 @@ /newalbum NewAlbumR GET POST /album/#AlbumId AlbumR GET /medium/#MediumId MediumR GET -/album/#AlbumId/upload DirectUploadR GET POST +/album/#AlbumId/upload DirectUploadR GET POST +/album/#AlbumId/settings AlbumSettingsR GET POST diff --git a/eidolon.cabal b/eidolon.cabal index 26c49eb..d88f902 100644 --- a/eidolon.cabal +++ b/eidolon.cabal @@ -29,6 +29,7 @@ library Handler.NewAlbum Handler.Album Handler.Medium + Handler.AlbumSettings if flag(dev) || flag(library-only) cpp-options: -DDEVELOPMENT diff --git a/templates/album.hamlet b/templates/album.hamlet index eaaed3d..91987d8 100644 --- a/templates/album.hamlet +++ b/templates/album.hamlet @@ -1,9 +1,11 @@ -$newline never +$newline always

#{albumTitle album} by #{ownerName}
$if presence == True Upload image to this album +
+
Change settings of your album
$if null media This album is empty diff --git a/templates/albumSettings.hamlet b/templates/albumSettings.hamlet new file mode 100644 index 0000000..fffcd48 --- /dev/null +++ b/templates/albumSettings.hamlet @@ -0,0 +1,7 @@ +$newline always +

Album settings + +
+ ^{albumSettingsWidget} +
+ diff --git a/templates/profile.hamlet b/templates/profile.hamlet index 6b8e447..00bd9c1 100644 --- a/templates/profile.hamlet +++ b/templates/profile.hamlet @@ -8,7 +8,10 @@ $else $forall (Entity albumId album) <- userAlbs