Skip to content
Permalink
Browse files

posts: json list

  • Loading branch information...
3v0k4 committed Aug 12, 2019
1 parent 78ef59c commit 53c06240c8b41438f35475284588e67950ff8800
Showing with 17 additions and 2 deletions.
  1. +2 −2 config/models.persistentmodels
  2. +2 −0 config/routes
  3. +1 −0 src/Foundation.hs
  4. +12 −0 src/Handler/Posts.hs
@@ -1,7 +1,7 @@
-- By default this file is used by `persistFileWith` in Model.hs (which is imported by Foundation.hs)
-- Syntax for this file here: https://github.com/yesodweb/persistent/blob/master/docs/Persistent-entity-syntax.md

User
User json
ident Text
password Text Maybe
UniqueUser ident
@@ -16,7 +16,7 @@ Comment json -- Adding "json" causes ToJSON and FromJSON instances to be derived
userId UserId Maybe
deriving Eq
deriving Show
Post
Post json
title Text
text Textarea
userId UserId
@@ -15,3 +15,5 @@
/ LandingR GET POST
/posts PostsR GET POST
/posts/#PostId PostR DELETE

/api/posts ApiPostsR GET
@@ -176,6 +176,7 @@ instance Yesod App where
isAuthorized LandingR _ = return Authorized

isAuthorized PostsR _ = isAuthenticated
isAuthorized ApiPostsR _ = isAuthenticated
isAuthorized (PostR postId) _ = isOwner postId

-- This function creates static content files in the static folder
@@ -39,6 +39,18 @@ getPostsR = do
emptyLayout $ do
$(widgetFile "posts")

getApiPostsR :: Handler Value
getApiPostsR = do
userId <- fmap fst $ requireAuthPair
(widget, enctype) <- generateFormPost $ postForm userId
allPosts <- runDB
$ E.select
$ E.from $ \(post `E.InnerJoin` user) -> do
E.on $ post ^. PostUserId E.==. user ^. UserId
E.orderBy [E.desc (post ^. PostId)]
return (post, user)
return $ object [ "posts" .= allPosts ]

postPostsR :: Handler Html
postPostsR = do
userId <- fmap fst $ requireAuthPair

0 comments on commit 53c0624

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