1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2025-01-10 15:46:46 +09:00
vervis/src/Vervis/Handler/Group.hs

96 lines
3.1 KiB
Haskell
Raw Normal View History

2016-05-25 06:48:21 +09:00
{- This file is part of Vervis.
-
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- 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
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Vervis.Handler.Group
( getGroupsR
, postGroupsR
, getGroupNewR
, getGroupR
)
where
import Prelude
import Control.Monad.IO.Class (liftIO)
2016-05-25 16:24:34 +09:00
import Data.Maybe (fromMaybe)
2016-05-25 06:48:21 +09:00
import Data.Time.Clock (getCurrentTime)
import Database.Esqueleto
import Text.Blaze.Html (Html)
import Yesod.Core (defaultLayout, setMessage)
import Yesod.Core.Handler (redirect)
import Yesod.Form.Functions (runFormPost)
import Yesod.Form.Types (FormResult (..))
import Yesod.Persist.Core (runDB, getBy404)
import Vervis.Form.Group
import Vervis.Foundation
import Vervis.Model
2016-05-25 16:24:34 +09:00
import Vervis.Model.Ident (ShrIdent, shr2text)
2016-05-25 06:48:21 +09:00
import Vervis.Settings (widgetFile)
2016-05-25 16:24:34 +09:00
import Vervis.Widget.Sharer (groupLinkW, personLinkW)
2016-05-25 06:48:21 +09:00
getGroupsR :: Handler Html
getGroupsR = do
groups <- runDB $ select $ from $ \ (sharer, group) -> do
where_ $ sharer ^. SharerId ==. group ^. GroupIdent
orderBy [asc $ sharer ^. SharerIdent]
return sharer
defaultLayout $(widgetFile "group/list")
postGroupsR :: Handler Html
postGroupsR = do
((result, widget), enctype) <- runFormPost newGroupForm
case result of
FormSuccess ng -> do
now <- liftIO getCurrentTime
runDB $ do
let sharer = Sharer
{ sharerIdent = ngIdent ng
, sharerName = ngName ng
, sharerCreated = now
}
sid <- insert sharer
let group = Group
{ groupIdent = sid
}
insert_ group
redirect $ GroupR $ ngIdent ng
FormMissing -> do
setMessage "Field(s) missing"
defaultLayout $(widgetFile "group/new")
FormFailure _l -> do
setMessage "Group creation failed, see errors below"
defaultLayout $(widgetFile "group/new")
getGroupNewR :: Handler Html
getGroupNewR = do
((_result, widget), enctype) <- runFormPost newGroupForm
defaultLayout $(widgetFile "group/new")
getGroupR :: ShrIdent -> Handler Html
getGroupR shar = do
2016-05-25 16:24:34 +09:00
(group, members) <- runDB $ do
Entity sid s <- getBy404 $ UniqueSharer shar
Entity gid _g <- getBy404 $ UniqueGroup sid
ms <- select $ from $ \ (member, person, sharer) -> do
where_ $
member ^. GroupMemberGroup ==. val gid &&.
member ^. GroupMemberPerson ==. person ^. PersonId &&.
person ^. PersonIdent ==. sharer ^. SharerId
orderBy [asc $ sharer ^. SharerIdent]
return sharer
return (s, ms)
2016-05-25 06:48:21 +09:00
defaultLayout $(widgetFile "group/one")