album settings can now be changed

This commit is contained in:
nek0 2014-08-28 18:08:56 +02:00
parent 339a8e5b0e
commit 629908285f
9 changed files with 101 additions and 4 deletions

View file

@ -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

81
Handler/AlbumSettings.hs Normal file
View file

@ -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

View file

@ -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

View file

@ -19,7 +19,7 @@ Album
title Text
owner UserId
content [MediumId]
samplePic MediumId Maybe
samplePic FilePath Maybe
deriving
Medium
title Text

View file

@ -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

View file

@ -29,6 +29,7 @@ library
Handler.NewAlbum
Handler.Album
Handler.Medium
Handler.AlbumSettings
if flag(dev) || flag(library-only)
cpp-options: -DDEVELOPMENT

View file

@ -1,9 +1,11 @@
$newline never
$newline always
<h3>#{albumTitle album}
by <a href=@{UserR ownerName}>#{ownerName}</a>
<br>
$if presence == True
<a href=@{DirectUploadR albumId}>Upload image to this album
<br>
<a href=@{AlbumSettingsR albumId}>Change settings of your album
<br>
$if null media
This album is empty

View file

@ -0,0 +1,7 @@
$newline always
<h3>Album settings
<form method="post" enctype=#{enctype}>
^{albumSettingsWidget}
<div>
<input type=submit value="Change settings">

View file

@ -8,7 +8,10 @@ $else
$forall (Entity albumId album) <- userAlbs
<div class="thumbnails">
<a href=@{AlbumR albumId}>
<img width="200px" src="/static/img/album.jpg"><br>
$if (albumSamplePic album) == Nothing
<img width="200px" src="/static/img/album.jpg"><br>
$else
<img width="200px" src=#{T.pack $ fromJust $ albumSamplePic album}><br>
#{albumTitle album}
$if null recentMedia