Skip to content

Commit d0a1636

Browse files
committed
part 5 markdown rendering
1 parent 79d1ba0 commit d0a1636

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE posts ADD COLUMN created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL;
2+
CREATE INDEX posts_created_at_index ON posts (created_at);

Application/Schema.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
CREATE TABLE posts (
33
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
44
title TEXT NOT NULL,
5-
body TEXT NOT NULL
5+
body TEXT NOT NULL,
6+
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
67
);
8+
CREATE INDEX posts_created_at_index ON posts (created_at);

Web/Controller/Posts.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import Web.View.Posts.Show
88

99
instance Controller PostsController where
1010
action PostsAction = do
11-
posts <- query @Post |> fetch
11+
posts <- query @Post
12+
|> orderByDesc #createdAt
13+
|> fetch
1214
render IndexView { .. }
1315

1416
action NewPostAction = do
@@ -53,3 +55,4 @@ instance Controller PostsController where
5355

5456
buildPost post = post
5557
|> fill @'["title", "body"]
58+
|> validateField #title nonEmpty

Web/View/Posts/Index.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ instance View IndexView where
1515
<th>Post</th>
1616
<th></th>
1717
<th></th>
18-
<th></th>
1918
</tr>
2019
</thead>
2120
<tbody>{forEach posts renderPost}</tbody>
@@ -31,8 +30,7 @@ instance View IndexView where
3130
renderPost :: Post -> Html
3231
renderPost post = [hsx|
3332
<tr>
34-
<td>{post}</td>
35-
<td><a href={ShowPostAction post.id}>Show</a></td>
33+
<td><a href={ShowPostAction post.id}>{post.title}</a></td>
3634
<td><a href={EditPostAction post.id} class="text-muted">Edit</a></td>
3735
<td><a href={DeletePostAction post.id} class="js-delete text-muted">Delete</a></td>
3836
</tr>

Web/View/Posts/Show.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
module Web.View.Posts.Show where
22
import Web.View.Prelude
3+
import qualified Text.MMark as MMark
4+
35

46
data ShowView = ShowView { post :: Post }
57

68
instance View ShowView where
79
html ShowView { .. } = [hsx|
810
{breadcrumb}
9-
<h1>Show Post</h1>
10-
<p>{post}</p>
11+
<h1>{post.title}</h1>
12+
<p>{post.createdAt |> timeAgo}</p>
13+
<div>{post.body |> renderMarkdown}</div>
14+
1115

1216
|]
1317
where
1418
breadcrumb = renderBreadcrumb
1519
[ breadcrumbLink "Posts" PostsAction
1620
, breadcrumbText "Show Post"
17-
]
21+
]
22+

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
base
2929
wai
3030
text
31+
mmark
3132

3233
# Uncomment on local development for testing
3334
# hspec

0 commit comments

Comments
 (0)