added a slug list route

This commit is contained in:
Yann Esposito (Yogsototh) 2018-12-13 23:51:04 +01:00
parent fa66dd0464
commit 08985373c5
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
2 changed files with 13 additions and 0 deletions

View file

@ -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

View file

@ -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)