Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
posts: join powered by esqueleto
  • Loading branch information
3v0k4 committed Jul 21, 2019
1 parent 2378194 commit 78ef59c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.yaml
Expand Up @@ -42,6 +42,7 @@ dependencies:
- case-insensitive
- wai
- foreign-store
- esqueleto

# The library contains all of our application code. The executable
# defined below is just a thin wrapper.
Expand Down
30 changes: 27 additions & 3 deletions src/Handler/Posts.hs
Expand Up @@ -8,7 +8,9 @@

module Handler.Posts where

import Import
import Import
import qualified Database.Esqueleto as E
import Database.Esqueleto ((^.))

postForm :: UserId -> Form Post
postForm userId =
Expand All @@ -22,15 +24,37 @@ getPostsR :: Handler Html
getPostsR = do
userId <- fmap fst $ requireAuthPair
(widget, enctype) <- generateFormPost $ postForm userId
allPosts :: [Entity Post] <- runDB $ selectList [] [ Desc PostId ]
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 ^. PostId
, post ^. PostUserId
, post ^. PostTitle
, post ^. PostText
, user ^. UserIdent
)
emptyLayout $ do
$(widgetFile "posts")

postPostsR :: Handler Html
postPostsR = do
userId <- fmap fst $ requireAuthPair
((result, widget), enctype) <- runFormPost $ postForm userId
allPosts :: [Entity Post] <- runDB $ selectList [] [ Desc PostId ]
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 ^. PostId
, post ^. PostUserId
, post ^. PostTitle
, post ^. PostText
, user ^. UserIdent
)
case result of
FormSuccess post -> do
_ <- runDB $ insert post
Expand Down
11 changes: 6 additions & 5 deletions templates/posts.hamlet
Expand Up @@ -9,12 +9,13 @@
<button>Post

<ul>
$forall post <- allPosts
$forall (E.Value postId, E.Value postUserId, E.Value title, E.Value text, E.Value author) <- allPosts
<li>
<h2> #{postTitle $ entityVal post}
<p> #{postText $ entityVal post}
<form method=post action=@{PostR $ entityKey post}?_method=DELETE>
$if userId == (postUserId $ entityVal post)
<h2> #{title}
<p> #{text}
<p> #{author}
<form method=post action=@{PostR postId}?_method=DELETE>
$if userId == postUserId
<button>Delete
$else
<p>

0 comments on commit 78ef59c

Please sign in to comment.