From 08985373c52df72ea20cb7f81ea6e8ba7b4ebf8f Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Thu, 13 Dec 2018 23:51:04 +0100 Subject: [PATCH] added a slug list route --- src/Aggreact.hs | 5 +++++ src/Aggreact/Comments.hs | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Aggreact.hs b/src/Aggreact.hs index bb35006..9ca2d64 100644 --- a/src/Aggreact.hs +++ b/src/Aggreact.hs @@ -35,6 +35,7 @@ import Servant.HTML.Blaze type CommentAPI = "comments" :> Capture "slug" Text :> Get '[HTML] CommentPage + :<|> "slugs" :> Get '[JSON] [Slug] :<|> "comments" :> ReqBody '[JSON, FormUrlEncoded] NewComment :> Post '[JSON] Comment data Conf = Conf { port :: Int } @@ -45,11 +46,15 @@ showComments db s = do now <- liftIO getCurrentTime return CommentPage { url = s, viewTime = now, comments = cs } +listSlugs :: DB Comments -> Handler [Slug] +listSlugs = liftIO . slugs + api :: Proxy CommentAPI api = Proxy server :: DB Comments -> Server CommentAPI server db = showComments db + :<|> listSlugs db :<|> liftIO . createNewComment db app :: DB Comments -> Application diff --git a/src/Aggreact/Comments.hs b/src/Aggreact/Comments.hs index 120c1b5..1e993f2 100644 --- a/src/Aggreact/Comments.hs +++ b/src/Aggreact/Comments.hs @@ -40,6 +40,7 @@ module Aggreact.Comments -- * Operations , initDB , createNewComment + , slugs , getCommentsBySlug , getCommentsByParentId ) where @@ -199,6 +200,9 @@ createComment = modify . IxSet.insert updateComment :: Comment -> Update Comments () updateComment comment = modify (IxSet.updateIx (id comment) comment) +getSlugs :: Query Comments [Slug] +getSlugs = (fmap fst . IxSet.groupBy) <$> ask + commentById :: Id -> Query Comments (Maybe Comment) commentById cid = do comments <- ask @@ -219,6 +223,7 @@ $(makeAcidic ''Comments [ 'createComment , 'commentById , 'commentsByParentId , 'commentsBySlug + , 'getSlugs ]) -- * Operations @@ -233,6 +238,9 @@ createNewComment db (NewComment pid s txt uid) = do Acid.update db (UpdateComment newComment) return newComment +slugs :: DB Comments -> IO [Slug] +slugs db = Acid.query db GetSlugs + getCommentsBySlug :: DB Comments -> Slug -> IO Comments getCommentsBySlug db s = Acid.query db (CommentsBySlug s)