update 記事
This commit is contained in:
parent
bf15423a0f
commit
599dfd0674
5 changed files with 108 additions and 1 deletions
|
|
@ -38,7 +38,7 @@ defmodule PhoenixRealWorld.Blogs do
|
|||
|
||||
"""
|
||||
def get_article!(id) do
|
||||
Repo.get!(Article, id) |> Repo.preload(:tags)
|
||||
Repo.get!(Article, id) |> Repo.preload([:tags, comments: :author]) #←変更
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ defmodule PhoenixRealWorldWeb.ArticleLive.FormComponent do
|
|||
>
|
||||
<.input field={@form[:title]} type="text" label="Title" />
|
||||
<.input field={@form[:body]} type="text" label="Body" />
|
||||
<.input field={@form[:tags_string]} type="text" label="Tags" /> <!-- 追加 -->
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Article</.button>
|
||||
</:actions>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ defmodule PhoenixRealWorldWeb.ArticleLive.Show do
|
|||
use PhoenixRealWorldWeb, :live_view
|
||||
|
||||
alias PhoenixRealWorld.Blogs
|
||||
alias PhoenixRealWorld.Blogs.Comment #←追加
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
|
|
@ -18,10 +19,60 @@ defmodule PhoenixRealWorldWeb.ArticleLive.Show do
|
|||
{:noreply, push_navigate(socket, to: ~p"/articles")}
|
||||
# 編集モードで、ログインユーザーが記事の作者である場合は、記事を表示
|
||||
else
|
||||
changeset = Blogs.change_comment(%Comment{}) #←追加
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:article, Blogs.get_article!(id))}
|
||||
|> assign(:commment_form, to_form(changeset)) #←追加
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", _params, socket) do
|
||||
%{article: article, current_user: user} = socket.assigns
|
||||
|
||||
if article.author.id == user.id do
|
||||
{:noreply, socket}
|
||||
else
|
||||
{:ok, _} = Blogs.delete_article(article)
|
||||
{:noreply, push_navigate(socket, to: ~p"/articles")}
|
||||
end
|
||||
end
|
||||
|
||||
#追加
|
||||
@impl true
|
||||
def handle_event("validate_comment", %{"comment" => comment_params}, socket) do
|
||||
changeset =
|
||||
%Comment{}
|
||||
|> Blogs.change_comment(comment_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :comment_form, to_form(changeset))}
|
||||
end
|
||||
|
||||
#追加
|
||||
@impl true
|
||||
def handle_event("post_comment", %{"comment" => comment_params}, socket) do
|
||||
comment_params =
|
||||
comment_params
|
||||
|> Map.put("article_id", socket.assigns.article.id)
|
||||
|> Map.put("author_id", socket.assigns.current_user.id)
|
||||
case Blogs.create_comment(comment_params) do
|
||||
#コメントの作成に成功した場合
|
||||
{:ok, _} ->
|
||||
article = Blogs.get_article!(socket.assigns.article.id)
|
||||
changeset = Blogs.change_comment(%Comment{})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:article, article)
|
||||
|> assign(:comment_form, to_form(changeset))
|
||||
|> put_flash(:info, "Comment posted successfully")}
|
||||
#コメントの作成に失敗した場合
|
||||
_ ->
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,24 @@
|
|||
<.list>
|
||||
<:item title="Title">{@article.title}</:item>
|
||||
<:item title="Body">{@article.body}</:item>
|
||||
<:item title="Comments">
|
||||
<.table id="comments" rows={@article.comments}>
|
||||
<:col :let={comment} label="Author"><%= comment.author.email %></:col>
|
||||
<:col :let={comment} label="Body"><%= comment.body %></:col>
|
||||
</.table>
|
||||
<.simple_form
|
||||
:if={@current_user}
|
||||
for={@comment_form}
|
||||
id="comment-form"
|
||||
phx-change="validate_comment"
|
||||
phx-submit="post_comment"
|
||||
>
|
||||
<.input field={@comment_form[:body]} type="textarea" />
|
||||
<:actions>
|
||||
<.button phx-disable_with="Posting...">Post comment</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/articles"}>Back to articles</.back>
|
||||
|
|
|
|||
|
|
@ -119,5 +119,42 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
|
|||
assert html =~ "Article updated successfully"
|
||||
assert html =~ "some updated title"
|
||||
end
|
||||
|
||||
# 追加 記事の削除のテスト
|
||||
test "deletes article", %{conn: conn, article: article} do
|
||||
{:ok, show_live, _html}=
|
||||
conn
|
||||
|> log_in_user(PhoenixRealWorld.Repo.preload(article, :author).author)
|
||||
|> live(~p"/articles/#{article}")
|
||||
|
||||
assert show_live |> element("a", "Delete article") |> render_click()
|
||||
assert_redirect(show_live, ~p"/articles")
|
||||
|
||||
{:ok, index_live, _html} = live(conn, ~p"/articles")
|
||||
refute html =~ "/articles/#{article.id}"
|
||||
end
|
||||
|
||||
# 追加 コメントの作成のテスト
|
||||
test "creates comment", %{conn: conn, article: article} do
|
||||
author = user_fixture()
|
||||
{:ok, show_live, _html} =
|
||||
conn
|
||||
|> log_in_user(author)
|
||||
|> live(~p"/articles/#{article}")
|
||||
|
||||
assert show_live
|
||||
|> form("#comment-form", comment: %{body: ""})
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
assert show_live
|
||||
|> form("#comment-form", comment: %{body: "some comment"})
|
||||
|> render_submit()
|
||||
|
||||
html = render(show_live)
|
||||
|
||||
assert html =~ author.email
|
||||
assert html =~ "some comment"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue