Skip to content

Commit

Permalink
posts: json list
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Aug 12, 2019
1 parent 78ef59c commit 53c0624
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions config/models.persistentmodels
@@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions config/routes
Expand Up @@ -15,3 +15,5 @@
/ LandingR GET POST
/posts PostsR GET POST
/posts/#PostId PostR DELETE

/api/posts ApiPostsR GET
1 change: 1 addition & 0 deletions src/Foundation.hs
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/Handler/Posts.hs
Expand Up @@ -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
Expand Down

0 comments on commit 53c0624

Please sign in to comment.