{- This file is part of Vervis. - - Written in 2016 by fr33domlover . - - ♡ Copying is an act of love. Please copy, reuse and share. - - The author(s) have dedicated all copyright and related and neighboring - rights to this software to the public domain worldwide. This software is - distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along - with this software. If not, see - . -} module Vervis.Handler.Project ( getProjectsR , postProjectsR , getProjectNewR , getProjectR ) where import Prelude import Data.Maybe (fromMaybe) import Data.Text (Text) import Database.Persist import Text.Blaze.Html (Html) import Yesod.Auth (requireAuth) import Yesod.Core (defaultLayout) import Yesod.Core.Handler (redirect, setMessage) import Yesod.Form.Functions (runFormPost) import Yesod.Form.Types (FormResult (..)) import Yesod.Persist.Core (runDB, getBy404) import qualified Database.Esqueleto as E import Vervis.Form.Project import Vervis.Foundation import Vervis.Model import Vervis.Model.Ident import Vervis.Model.Repo import Vervis.Settings getProjectsR :: ShrIdent -> Handler Html getProjectsR ident = do projects <- runDB $ E.select $ E.from $ \ (sharer, project) -> do E.where_ $ sharer E.^. SharerIdent E.==. E.val ident E.&&. sharer E.^. SharerId E.==. project E.^. ProjectSharer E.orderBy [E.asc $ project E.^. ProjectIdent] return $ project E.^. ProjectIdent defaultLayout $(widgetFile "project/list") postProjectsR :: ShrIdent -> Handler Html postProjectsR ident = do Entity _pid person <- requireAuth let sid = personIdent person ((result, widget), enctype) <- runFormPost $ newProjectForm sid case result of FormSuccess project -> do runDB $ insert_ project setMessage "Project added." redirect HomeR FormMissing -> do setMessage "Field(s) missing" defaultLayout $(widgetFile "project/new") FormFailure _l -> do setMessage "Project creation failed, see below" defaultLayout $(widgetFile "project/new") getProjectNewR :: ShrIdent -> Handler Html getProjectNewR ident = do Entity _pid person <- requireAuth let sid = personIdent person ((_result, widget), enctype) <- runFormPost $ newProjectForm sid defaultLayout $(widgetFile "project/new") getProjectR :: ShrIdent -> PrjIdent -> Handler Html getProjectR shar proj = do (project, repos) <- runDB $ do Entity sid _s <- getBy404 $ UniqueSharer shar Entity pid p <- getBy404 $ UniqueProject proj sid rs <- selectList [RepoProject ==. Just pid] [Asc RepoIdent] return (p, rs) defaultLayout $(widgetFile "project/one")