Skip to content
Permalink
Browse files

posts: join powered by esqueleto

  • Loading branch information...
3v0k4 committed Jul 21, 2019
1 parent 2378194 commit 78ef59c6e6718dbce83ea2802cb70335bb4cca33
Showing with 34 additions and 8 deletions.
  1. +1 −0 package.yaml
  2. +27 −3 src/Handler/Posts.hs
  3. +6 −5 templates/posts.hamlet
@@ -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.
@@ -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 =
@@ -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
@@ -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.
You can’t perform that action at this time.