Skip to content

Commit 3bb7bf7

Browse files
committed
feat: Add basic Show Post Page layout
1 parent e1e5ec8 commit 3bb7bf7

File tree

10 files changed

+134
-17
lines changed

10 files changed

+134
-17
lines changed

lib/brasil_em_dados/blog.ex

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ defmodule BrasilEmDados.Blog do
3737
"""
3838
def get_post!(id), do: Repo.get!(Post, id)
3939

40+
def get_post_by_slug(slug) do
41+
Repo.get_by(Post, slug: slug)
42+
end
43+
4044
@doc """
4145
Creates a post.
4246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule BrasilEmDadosWeb.BlogLive.Show do
2+
use BrasilEmDadosWeb, :live_view
3+
alias BrasilEmDadosWeb.BlogLiveView
4+
alias BrasilEmDados.Blog
5+
6+
@impl true
7+
def mount(%{"slug" => slug}, _session, socket) do
8+
post = Blog.get_post_by_slug(slug)
9+
10+
{:ok, assign(socket, post: post, slug: slug)}
11+
end
12+
13+
@impl true
14+
def render(assigns) do
15+
Phoenix.View.render(BlogLiveView, "show.html", assigns)
16+
end
17+
end

lib/brasil_em_dados_web/live/index.ex

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule BrasilEmDadosWeb.BlogLive.Index do
2+
use BrasilEmDadosWeb, :live_view
3+
alias BrasilEmDadosWeb.BlogLiveView
4+
5+
@impl true
6+
def mount(_params, _session, socket) do
7+
{:ok, socket}
8+
end
9+
10+
@impl true
11+
def render(assigns) do
12+
Phoenix.View.render(BlogLiveView, "index.html", assigns)
13+
end
14+
end

lib/brasil_em_dados_web/router.ex

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ defmodule BrasilEmDadosWeb.Router do
2121
pipe_through :browser
2222

2323
live "/", PageLive, :index
24+
live "/blog", BlogLive.Index, :index
25+
live "/blog/:slug", BlogLive.Show, :show
2426
end
2527

2628
# Other scopes may use custom stacks.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Blog Home
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<div class="container mx-auto flex">
2+
<div class="w-2/3 px-5 mx-auto">
3+
<div class="mt-8 mb-6">
4+
<h1 class="text-4xl font-bold mb-1">
5+
<%= @post.title %>
6+
</h1>
7+
<div class="text-sm">
8+
November 13, 2020 · 5 min · Roger Vezaro
9+
</div>
10+
</div>
11+
<p class="leading-relaxed text-base mb-5">
12+
<a class="border-b border-black" href="https://spotify.com">Spotify Codes</a> <%= @post.body %>
13+
</p>
14+
<h2 class="text-2xl font-bold mt-6 mb-4">This is a subtitle</h2>
15+
<p class="leading-relaxed text-base mb-5">
16+
<%= @post.body %>
17+
</p>
18+
<p class="leading-relaxed text-base mb-5">
19+
<%= @post.body %>
20+
</p>
21+
<h2 class="text-2xl font-bold mt-6 mb-4">This is another subtitle</h2>
22+
<p class="leading-relaxed text-base mb-5 text-justify">
23+
<%= @post.body %>
24+
</p>
25+
</div>
26+
</div>

lib/brasil_em_dados_web/templates/layout/live.html.leex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<main role="main" class="container">
1+
<main role="main">
22
<p class="alert alert-info" role="alert"
33
phx-click="lv:clear-flash"
44
phx-value-key="info"><%= live_flash(@flash, :info) %></p>

lib/brasil_em_dados_web/templates/layout/root.html.leex

-16
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,6 @@
1010
<script defer phx-track-static type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
1111
</head>
1212
<body>
13-
<header>
14-
<section class="container">
15-
<nav role="navigation">
16-
<ul>
17-
<li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
18-
<%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
19-
<li><%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>
20-
<% end %>
21-
</ul>
22-
<%= render "_user_menu.html", assigns %>
23-
</nav>
24-
<a href="https://phoenixframework.org/" class="phx-logo">
25-
<img src="<%= Routes.static_path(@conn, "/images/phoenix.png") %>" alt="Phoenix Framework Logo"/>
26-
</a>
27-
</section>
28-
</header>
2913
<%= @inner_content %>
3014
</body>
3115
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule BrasilEmDadosWeb.BlogLiveView do
2+
use BrasilEmDadosWeb, :view
3+
end

test/brasil_em_dados/blog_test.exs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
defmodule BrasilEmDados.BlogTest do
2+
use BrasilEmDados.DataCase
3+
4+
alias BrasilEmDados.Blog
5+
6+
describe "posts" do
7+
alias BrasilEmDados.Blog.Post
8+
9+
@valid_attrs %{body: "some body", title: "some title"}
10+
@update_attrs %{body: "some updated body", title: "some updated title"}
11+
@invalid_attrs %{body: nil, title: nil}
12+
13+
def post_fixture(attrs \\ %{}) do
14+
{:ok, post} =
15+
attrs
16+
|> Enum.into(@valid_attrs)
17+
|> Blog.create_post()
18+
19+
post
20+
end
21+
22+
test "list_posts/0 returns all posts" do
23+
post = post_fixture()
24+
assert Blog.list_posts() == [post]
25+
end
26+
27+
test "get_post!/1 returns the post with given id" do
28+
post = post_fixture()
29+
assert Blog.get_post!(post.id) == post
30+
end
31+
32+
test "create_post/1 with valid data creates a post" do
33+
assert {:ok, %Post{} = post} = Blog.create_post(@valid_attrs)
34+
assert post.body == "some body"
35+
assert post.title == "some title"
36+
end
37+
38+
test "create_post/1 with invalid data returns error changeset" do
39+
assert {:error, %Ecto.Changeset{}} = Blog.create_post(@invalid_attrs)
40+
end
41+
42+
test "update_post/2 with valid data updates the post" do
43+
post = post_fixture()
44+
assert {:ok, %Post{} = post} = Blog.update_post(post, @update_attrs)
45+
assert post.body == "some updated body"
46+
assert post.title == "some updated title"
47+
end
48+
49+
test "update_post/2 with invalid data returns error changeset" do
50+
post = post_fixture()
51+
assert {:error, %Ecto.Changeset{}} = Blog.update_post(post, @invalid_attrs)
52+
assert post == Blog.get_post!(post.id)
53+
end
54+
55+
test "delete_post/1 deletes the post" do
56+
post = post_fixture()
57+
assert {:ok, %Post{}} = Blog.delete_post(post)
58+
assert_raise Ecto.NoResultsError, fn -> Blog.get_post!(post.id) end
59+
end
60+
61+
test "change_post/1 returns a post changeset" do
62+
post = post_fixture()
63+
assert %Ecto.Changeset{} = Blog.change_post(post)
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)