Pass AppDB action instead of DiscussionId

Passing `AppDB DiscussionId` from ticket handlers to the actual
discussion handlers allows the DB queries to run in a single
transaction.
This commit is contained in:
fr33domlover 2016-05-19 22:40:54 +00:00
parent a56a7575fe
commit aa3d332b14
5 changed files with 36 additions and 29 deletions

View file

@ -35,8 +35,9 @@ import Data.Tree.Local (sortForestOn)
import Vervis.Foundation import Vervis.Foundation
import Vervis.Model import Vervis.Model
getMessages :: DiscussionId -> Handler [(Entity Message, Sharer)] getMessages :: AppDB DiscussionId -> Handler [(Entity Message, Sharer)]
getMessages did = fmap (map $ second entityVal) $ runDB $ getMessages getdid = fmap (map $ second entityVal) $ runDB $ do
did <- getdid
select $ from $ \ (message, person, sharer) -> do select $ from $ \ (message, person, sharer) -> do
where_ $ where_ $
message ^. MessageRoot ==. val did &&. message ^. MessageRoot ==. val did &&.
@ -67,5 +68,5 @@ sortByTime = sortForestOn $ messageCreated . fst
-- | Get the tree of messages in a given discussion, with siblings sorted from -- | Get the tree of messages in a given discussion, with siblings sorted from
-- old to new. -- old to new.
getDiscussionTree :: DiscussionId -> Handler (Forest (Message, Sharer)) getDiscussionTree :: AppDB DiscussionId -> Handler (Forest (Message, Sharer))
getDiscussionTree did = sortByTime . discussionTree <$> getMessages did getDiscussionTree getdid = sortByTime . discussionTree <$> getMessages getdid

View file

@ -35,17 +35,18 @@ import Yesod.Form.Types (FormResult (..))
import Yesod.Persist.Core (runDB, get404, getBy404) import Yesod.Persist.Core (runDB, get404, getBy404)
import Vervis.Form.Discussion import Vervis.Form.Discussion
import Vervis.Foundation (App, Handler) import Vervis.Foundation (App, Handler, AppDB)
import Vervis.Model import Vervis.Model
import Vervis.Settings (widgetFile) import Vervis.Settings (widgetFile)
import Vervis.Widget.Discussion import Vervis.Widget.Discussion
getDiscussion :: (Int -> Route App) -> DiscussionId -> Handler Html getDiscussion :: (Int -> Route App) -> AppDB DiscussionId -> Handler Html
getDiscussion reply did = defaultLayout $ discussionW did reply getDiscussion reply getdid = defaultLayout $ discussionW getdid reply
getMessage :: (Int -> Route App) -> DiscussionId -> Int -> Handler Html getMessage :: (Int -> Route App) -> AppDB DiscussionId -> Int -> Handler Html
getMessage reply did num = do getMessage reply getdid num = do
(msg, shr) <- runDB $ do (msg, shr) <- runDB $ do
did <- getdid
Entity _mid m <- getBy404 $ UniqueMessage did num Entity _mid m <- getBy404 $ UniqueMessage did num
p <- get404 $ messageAuthor m p <- get404 $ messageAuthor m
s <- get404 $ personIdent p s <- get404 $ personIdent p
@ -56,11 +57,12 @@ getMessage reply did num = do
getReply getReply
:: (Int -> Route App) :: (Int -> Route App)
-> (Int -> Route App) -> (Int -> Route App)
-> DiscussionId -> AppDB DiscussionId
-> Int -> Int
-> Handler Html -> Handler Html
getReply replyG replyP did num = do getReply replyG replyP getdid num = do
(msg, shr) <- runDB $ do (msg, shr) <- runDB $ do
did <- getdid
Entity _mid m <- getBy404 $ UniqueMessage did num Entity _mid m <- getBy404 $ UniqueMessage did num
p <- get404 $ messageAuthor m p <- get404 $ messageAuthor m
s <- get404 $ personIdent p s <- get404 $ personIdent p
@ -73,16 +75,17 @@ postReply
:: (Int -> Route App) :: (Int -> Route App)
-> (Int -> Route App) -> (Int -> Route App)
-> (Int -> Route App) -> (Int -> Route App)
-> DiscussionId -> AppDB DiscussionId
-> Int -> Int
-> Handler Html -> Handler Html
postReply replyG replyP after did cnum = do postReply replyG replyP after getdid cnum = do
((result, widget), enctype) <- runFormPost newMessageForm ((result, widget), enctype) <- runFormPost newMessageForm
now <- liftIO getCurrentTime now <- liftIO getCurrentTime
case result of case result of
FormSuccess nm -> do FormSuccess nm -> do
author <- requireAuthId author <- requireAuthId
mnum <- runDB $ do mnum <- runDB $ do
did <- getdid
(parent, next) <- do (parent, next) <- do
discussion <- get404 did discussion <- get404 did
Entity mid _message <- getBy404 $ UniqueMessage did cnum Entity mid _message <- getBy404 $ UniqueMessage did cnum
@ -103,6 +106,7 @@ postReply replyG replyP after did cnum = do
FormMissing -> do FormMissing -> do
setMessage "Field(s) missing." setMessage "Field(s) missing."
(msg, shr) <- runDB $ do (msg, shr) <- runDB $ do
did <- getdid
Entity _mid m <- getBy404 $ UniqueMessage did cnum Entity _mid m <- getBy404 $ UniqueMessage did cnum
p <- get404 $ messageAuthor m p <- get404 $ messageAuthor m
s <- get404 $ personIdent p s <- get404 $ personIdent p
@ -111,6 +115,7 @@ postReply replyG replyP after did cnum = do
FormFailure _l -> do FormFailure _l -> do
setMessage "Message submission failed, see errors below." setMessage "Message submission failed, see errors below."
(msg, shr) <- runDB $ do (msg, shr) <- runDB $ do
did <- getdid
Entity _mid m <- getBy404 $ UniqueMessage did cnum Entity _mid m <- getBy404 $ UniqueMessage did cnum
p <- get404 $ messageAuthor m p <- get404 $ messageAuthor m
s <- get404 $ personIdent p s <- get404 $ personIdent p

View file

@ -192,33 +192,34 @@ selectDiscussionId shar proj tnum = do
return $ ticketDiscuss ticket return $ ticketDiscuss ticket
getTicketDiscussionR :: Text -> Text -> Int -> Handler Html getTicketDiscussionR :: Text -> Text -> Int -> Handler Html
getTicketDiscussionR shar proj num = do getTicketDiscussionR shar proj num =
did <- runDB $ selectDiscussionId shar proj num getDiscussion
getDiscussion (TicketReplyR shar proj num) did (TicketReplyR shar proj num)
(selectDiscussionId shar proj num)
getTicketMessageR :: Text -> Text -> Int -> Int -> Handler Html getTicketMessageR :: Text -> Text -> Int -> Int -> Handler Html
getTicketMessageR shar proj tnum cnum = do getTicketMessageR shar proj tnum cnum =
did <- runDB $ selectDiscussionId shar proj tnum getMessage
getMessage (TicketReplyR shar proj tnum) did cnum (TicketReplyR shar proj tnum)
(selectDiscussionId shar proj tnum)
cnum
postTicketMessageR :: Text -> Text -> Int -> Int -> Handler Html postTicketMessageR :: Text -> Text -> Int -> Int -> Handler Html
postTicketMessageR shar proj tnum cnum = do postTicketMessageR shar proj tnum cnum =
did <- runDB $ selectDiscussionId shar proj tnum
postReply postReply
(TicketReplyR shar proj tnum) (TicketReplyR shar proj tnum)
(TicketMessageR shar proj tnum) (TicketMessageR shar proj tnum)
(const $ TicketR shar proj tnum) (const $ TicketR shar proj tnum)
did (selectDiscussionId shar proj tnum)
cnum cnum
getTicketTopReplyR :: Text -> Text -> Int -> Handler Html getTicketTopReplyR :: Text -> Text -> Int -> Handler Html
getTicketTopReplyR shar proj num = error "Not implemented yet" getTicketTopReplyR shar proj num = error "Not implemented yet"
getTicketReplyR :: Text -> Text -> Int -> Int -> Handler Html getTicketReplyR :: Text -> Text -> Int -> Int -> Handler Html
getTicketReplyR shar proj tnum cnum = do getTicketReplyR shar proj tnum cnum =
did <- runDB $ selectDiscussionId shar proj tnum
getReply getReply
(TicketReplyR shar proj tnum) (TicketReplyR shar proj tnum)
(TicketMessageR shar proj tnum) (TicketMessageR shar proj tnum)
did (selectDiscussionId shar proj tnum)
cnum cnum

View file

@ -61,9 +61,9 @@ messageTreeW reply cReplies now t = go t
^{go tree} ^{go tree}
|] |]
discussionW :: DiscussionId -> (Int -> Route App) -> Widget discussionW :: AppDB DiscussionId -> (Int -> Route App) -> Widget
discussionW did reply = do discussionW getdid reply = do
forest <- handlerToWidget $ getDiscussionTree did forest <- handlerToWidget $ getDiscussionTree getdid
cReplies <- newIdent cReplies <- newIdent
now <- liftIO getCurrentTime now <- liftIO getCurrentTime
toWidget $(cassiusFile "templates/discussion/widget/tree.cassius") toWidget $(cassiusFile "templates/discussion/widget/tree.cassius")

View file

@ -37,4 +37,4 @@ $# <http://creativecommons.org/publicdomain/zero/1.0/>.
<h3>Discussion <h3>Discussion
^{discussionW (ticketDiscuss ticket) (TicketReplyR shar proj num)} ^{discussionW (return $ ticketDiscuss ticket) (TicketReplyR shar proj num)}