Skip to content
Permalink
Browse files

posts: json list custom

  • Loading branch information...
3v0k4 committed Aug 12, 2019
1 parent 53c0624 commit 70e71484dd979fe43adf6295f4f6110e974a3214
Showing with 22 additions and 1 deletion.
  1. +22 −1 src/Handler/Posts.hs
@@ -39,6 +39,26 @@ getPostsR = do
emptyLayout $ do
$(widgetFile "posts")

data PostData = PostData (Entity Post, Entity User)

instance ToJSON PostData where
toJSON (PostData (postEntity, userEntity)) =
let
post = entityVal postEntity
postId = entityKey postEntity
user = entityVal userEntity
userId = entityKey userEntity
in
object
[ "id" .= postId
, "title" .= postTitle post
, "text" .= postText post
, "user" .= object
[ "id" .= userId
, "username" .= userIdent user
]
]

getApiPostsR :: Handler Value
getApiPostsR = do
userId <- fmap fst $ requireAuthPair
@@ -49,7 +69,8 @@ getApiPostsR = do
E.on $ post ^. PostUserId E.==. user ^. UserId
E.orderBy [E.desc (post ^. PostId)]
return (post, user)
return $ object [ "posts" .= allPosts ]
let allPosts' = PostData <$> allPosts
return $ object [ "posts" .= allPosts' ]

postPostsR :: Handler Html
postPostsR = do

0 comments on commit 70e7148

Please sign in to comment.
You can’t perform that action at this time.