|
| 1 | +module Web.Controller.Comments where |
| 2 | + |
| 3 | +import Web.Controller.Prelude |
| 4 | +import Web.View.Comments.Index |
| 5 | +import Web.View.Comments.New |
| 6 | +import Web.View.Comments.Edit |
| 7 | +import Web.View.Comments.Show |
| 8 | + |
| 9 | +instance Controller CommentsController where |
| 10 | + action CommentsAction = do |
| 11 | + comments <- query @Comment |> fetch |
| 12 | + render IndexView { .. } |
| 13 | + |
| 14 | + action NewCommentAction { postId } = do |
| 15 | + let comment = newRecord |
| 16 | + |> set #postId postId |
| 17 | + post <- fetch postId |
| 18 | + render NewView { .. } |
| 19 | + |
| 20 | + action ShowCommentAction { commentId } = do |
| 21 | + comment <- fetch commentId |
| 22 | + render ShowView { .. } |
| 23 | + |
| 24 | + action EditCommentAction { commentId } = do |
| 25 | + comment <- fetch commentId |
| 26 | + render EditView { .. } |
| 27 | + |
| 28 | + action UpdateCommentAction { commentId } = do |
| 29 | + comment <- fetch commentId |
| 30 | + comment |
| 31 | + |> buildComment |
| 32 | + |> ifValid \case |
| 33 | + Left comment -> render EditView { .. } |
| 34 | + Right comment -> do |
| 35 | + comment <- comment |> updateRecord |
| 36 | + setSuccessMessage "Comment updated" |
| 37 | + redirectTo EditCommentAction { .. } |
| 38 | + |
| 39 | + action CreateCommentAction = do |
| 40 | + let comment = newRecord @Comment |
| 41 | + comment |
| 42 | + |> buildComment |
| 43 | + |> ifValid \case |
| 44 | + Left comment -> do |
| 45 | + post <- fetch comment.postId -- <---- NEW |
| 46 | + render NewView { .. } |
| 47 | + Right comment -> do |
| 48 | + comment <- comment |> createRecord |
| 49 | + setSuccessMessage "Comment created" |
| 50 | + redirectTo ShowPostAction { postId = comment.postId } |
| 51 | + |
| 52 | + |
| 53 | + action DeleteCommentAction { commentId } = do |
| 54 | + comment <- fetch commentId |
| 55 | + deleteRecord comment |
| 56 | + setSuccessMessage "Comment deleted" |
| 57 | + redirectTo CommentsAction |
| 58 | + |
| 59 | +buildComment comment = comment |
| 60 | + |> fill @'["postId", "author", "body"] |
0 commit comments