From cf2c3f652fc0f00262b739250fa68d951256b02f Mon Sep 17 00:00:00 2001 From: nek0 Date: Sun, 7 Sep 2014 06:57:53 +0200 Subject: [PATCH] forgot creation and deletion of certein references --- Handler/MediumSettings.hs | 7 +++++++ Handler/NewAlbum.hs | 8 ++++++++ Helper.hs | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/Handler/MediumSettings.hs b/Handler/MediumSettings.hs index b29402e..14897a9 100644 --- a/Handler/MediumSettings.hs +++ b/Handler/MediumSettings.hs @@ -122,6 +122,13 @@ postMediumDeleteR mediumId = do confirm <- lookupPostParam "confirm" case confirm of Just "confirm" -> do + -- delete references first + albumId <- return $ mediumAlbum medium + album <- runDB $ getJust albumId + mediaList <- return $ albumContent album + newMediaList <- return $ removeItem mediumId mediaList + -- update reference List + runDB $ update albumId [AlbumContent =. newMediaList] liftIO $ removeFile (normalise $ tail $ mediumPath medium) runDB $ delete mediumId setMessage "Medium succesfully deleted" diff --git a/Handler/NewAlbum.hs b/Handler/NewAlbum.hs index 185993e..0a20048 100644 --- a/Handler/NewAlbum.hs +++ b/Handler/NewAlbum.hs @@ -27,8 +27,16 @@ postNewAlbumR = do ((result, albumWidget), enctype) <- runFormPost (albumForm userId) case result of FormSuccess album -> do + -- Put album in Database albumId <- runDB $ insert album + -- add album reference in user + user <- runDB $ getJust userId + albumList <- return $ userAlbums user + newAlbumList <- return $ albumId : albumList + runDB $ update userId [UserAlbums =. newAlbumList] + -- create folder liftIO $ createDirectory $ "static" "data" (unpack $ extractKey userId) (unpack $ extractKey albumId) + -- outro setMessage $ "Album successfully created" redirect $ ProfileR userId diff --git a/Helper.hs b/Helper.hs index b5c4f05..c1ab53c 100644 --- a/Helper.hs +++ b/Helper.hs @@ -10,6 +10,7 @@ module Helper , tagField , sendMail , generateString + , removeItem ) where @@ -108,3 +109,9 @@ sendMail toEmail subject body = generateString :: IO T.Text generateString = (toHex . B.pack . take 16 . randoms) <$> newStdGen + +removeItem :: Eq a => a -> [a] -> [a] +removeItem _ [] = [] +removeItem x (y:ys) + | x == y = removeItem x ys + | otherwise = y : removeItem x ys