From 72ff0c80e40ca26f1846121bf84d30e2ac7a0c41 Mon Sep 17 00:00:00 2001 From: nek0 Date: Thu, 14 Aug 2014 02:26:02 +0200 Subject: [PATCH] album creation --- Handler/NewAlbum.hs | 34 ++++++++++++++++++++++++++++++++-- templates/newAlbum.hamlet | 6 ++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 templates/newAlbum.hamlet diff --git a/Handler/NewAlbum.hs b/Handler/NewAlbum.hs index 084e4ff..582c59f 100644 --- a/Handler/NewAlbum.hs +++ b/Handler/NewAlbum.hs @@ -1,9 +1,39 @@ module Handler.NewAlbum where import Import +import Data.Text +import System.Directory +import System.FilePath getNewAlbumR :: Handler Html -getNewAlbumR = error "Not yet implemented: getNewAlbumR" +getNewAlbumR = do + msu <- lookupSession "userId" + case msu of + Just tempUserId -> do + userId <- lift $ pure $ getUserIdFromText tempUserId + (albumWidget, enctype) <- generateFormPost (albumForm $ Key userId) + defaultLayout $ do + $(widgetFile "newAlbum") + Nothing -> do + setMessage $ [shamlet|
You need to be lgged in|]
+      redirect $ LoginR
 
 postNewAlbumR :: Handler Html
-postNewAlbumR = error "Not yet implemented: postNewAlbumR"
+postNewAlbumR = do
+  msu <- lookupSession "userId"
+  case msu of
+    Just tempUserId -> do
+      userId <- lift $ pure $ getUserIdFromText tempUserId
+      ((result, albumWidget), enctype) <- runFormPost (albumForm $ Key userId)
+      case result of
+        FormSuccess album -> do
+          albumId <- runDB $ insert album
+          liftIO $ createDirectory $ "static"  (unpack $ extractKey $ Key userId)  (unpack $ extractKey albumId)
+          setMessage $ [shamlet|
Album successfully created|]
+          redirect $ ProfileR $ Key userId
+
+albumForm :: UserId -> Form Album
+albumForm userId = renderDivs $ Album
+  <$> areq textField "Title" Nothing
+  <*> pure userId
+  <*> pure []
diff --git a/templates/newAlbum.hamlet b/templates/newAlbum.hamlet
new file mode 100644
index 0000000..7df753f
--- /dev/null
+++ b/templates/newAlbum.hamlet
@@ -0,0 +1,6 @@
+

Create new Album + +
+ ^{albumWidget} +
+