From 00d56b1073dd1009112f3f1ea36e9946956b71e6 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Fri, 14 Dec 2018 00:07:47 +0100 Subject: [PATCH] added JSON view to comments --- src/Aggreact.hs | 2 +- src/Aggreact/Comments.hs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Aggreact.hs b/src/Aggreact.hs index 9ca2d64..f2c99d4 100644 --- a/src/Aggreact.hs +++ b/src/Aggreact.hs @@ -34,7 +34,7 @@ import Servant import Servant.HTML.Blaze type CommentAPI = - "comments" :> Capture "slug" Text :> Get '[HTML] CommentPage + "comments" :> Capture "slug" Text :> Get '[HTML,JSON] CommentPage :<|> "slugs" :> Get '[JSON] [Slug] :<|> "comments" :> ReqBody '[JSON, FormUrlEncoded] NewComment :> Post '[JSON] Comment diff --git a/src/Aggreact/Comments.hs b/src/Aggreact/Comments.hs index 1e993f2..956dd72 100644 --- a/src/Aggreact/Comments.hs +++ b/src/Aggreact/Comments.hs @@ -88,6 +88,12 @@ instance ( Serialize a put i = put (IxSet.toList i) get = fmap IxSet.fromList get +instance ( Ord a + , ToJSON a + , IxSet.Indexable a + , Typeable a) => ToJSON (IxSet.IxSet a) where + toJSON i = toJSON (IxSet.toList i) + -- * Comment data DecodeUUIDException = DecodeUUIDException deriving (Show) @@ -280,6 +286,9 @@ data CommentPage = , comments :: Comments } +instance ToJSON CommentPage where + toJSON cp = toJSON (comments cp) + instance H.ToMarkup CommentPage where toMarkup CommentPage {..} = do let roots = comments IxSet.@= ParentId Nothing