2015-01-18 19:44:41 +00:00
|
|
|
-- eidolon -- A simple gallery in Haskell and Yesod
|
|
|
|
-- Copyright (C) 2015 Amedeo Molnár
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published
|
|
|
|
-- by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
2015-01-21 09:00:18 +00:00
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-18 19:44:41 +00:00
|
|
|
|
2014-08-13 22:49:37 +00:00
|
|
|
module Handler.NewAlbum where
|
|
|
|
|
|
|
|
import Import
|
2015-10-19 06:32:47 +00:00
|
|
|
import Data.Text as T
|
2014-08-14 00:26:02 +00:00
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
2014-08-13 22:49:37 +00:00
|
|
|
|
|
|
|
getNewAlbumR :: Handler Html
|
2014-08-14 00:26:02 +00:00
|
|
|
getNewAlbumR = do
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- lift $ pure $ getUserIdFromText tempUserId
|
2015-10-19 06:32:47 +00:00
|
|
|
(albumWidget, enctype) <- generateFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
albumForm userId
|
2016-08-31 19:37:20 +00:00
|
|
|
defaultLayout $ do
|
2014-09-24 21:03:18 +00:00
|
|
|
setTitle "Eidolon :: Create new Album"
|
2014-08-14 00:26:02 +00:00
|
|
|
$(widgetFile "newAlbum")
|
|
|
|
Nothing -> do
|
2014-08-26 02:37:56 +00:00
|
|
|
setMessage "You need to be logged in"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect LoginR
|
2014-08-13 22:49:37 +00:00
|
|
|
|
|
|
|
postNewAlbumR :: Handler Html
|
2014-08-14 00:26:02 +00:00
|
|
|
postNewAlbumR = do
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
2015-09-14 16:54:46 +00:00
|
|
|
let userId = getUserIdFromText tempUserId
|
2015-10-19 06:32:47 +00:00
|
|
|
((result, _), _) <- runFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
albumForm userId
|
2014-08-14 00:26:02 +00:00
|
|
|
case result of
|
|
|
|
FormSuccess album -> do
|
2014-09-07 04:57:53 +00:00
|
|
|
-- Put album in Database
|
2014-08-14 00:26:02 +00:00
|
|
|
albumId <- runDB $ insert album
|
2014-09-07 04:57:53 +00:00
|
|
|
-- add album reference in user
|
|
|
|
user <- runDB $ getJust userId
|
2015-09-14 16:54:46 +00:00
|
|
|
let albumList = userAlbums user
|
|
|
|
let newAlbumList = albumId : albumList
|
2014-09-07 04:57:53 +00:00
|
|
|
runDB $ update userId [UserAlbums =. newAlbumList]
|
|
|
|
-- create folder
|
2015-09-14 16:54:46 +00:00
|
|
|
liftIO $ createDirectory $ "static" </> "data" </> unpack (extractKey userId) </> unpack (extractKey albumId)
|
2014-09-07 04:57:53 +00:00
|
|
|
-- outro
|
2015-09-14 16:54:46 +00:00
|
|
|
setMessage "Album successfully created"
|
2014-08-15 12:50:03 +00:00
|
|
|
redirect $ ProfileR userId
|
2014-09-07 05:03:06 +00:00
|
|
|
_ -> do
|
|
|
|
setMessage "There was an error creating the album"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect NewAlbumR
|
2014-09-07 05:03:06 +00:00
|
|
|
Nothing -> do
|
|
|
|
setMessage "You must be logged in to create albums"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect LoginR
|
2014-08-14 00:26:02 +00:00
|
|
|
|
2015-10-19 06:32:47 +00:00
|
|
|
albumForm :: UserId -> AForm Handler Album
|
|
|
|
albumForm userId = Album
|
|
|
|
<$> areq textField (bfs ("Title" :: T.Text)) Nothing
|
2014-08-14 00:26:02 +00:00
|
|
|
<*> pure userId
|
|
|
|
<*> pure []
|
2014-12-06 03:39:24 +00:00
|
|
|
<*> pure []
|
2014-08-15 11:24:14 +00:00
|
|
|
<*> pure Nothing
|
2015-10-19 06:32:47 +00:00
|
|
|
<* bootstrapSubmit ("Create album" :: BootstrapSubmit Text)
|