diff --git a/lib/phoenixRealWorld/blogs.ex b/lib/phoenixRealWorld/blogs.ex index af8ec27..04191a9 100644 --- a/lib/phoenixRealWorld/blogs.ex +++ b/lib/phoenixRealWorld/blogs.ex @@ -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 """ diff --git a/lib/phoenixRealWorld_web/live/article_live/form_component.ex b/lib/phoenixRealWorld_web/live/article_live/form_component.ex index 37a0045..78498f2 100644 --- a/lib/phoenixRealWorld_web/live/article_live/form_component.ex +++ b/lib/phoenixRealWorld_web/live/article_live/form_component.ex @@ -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 diff --git a/lib/phoenixRealWorld_web/live/article_live/show.ex b/lib/phoenixRealWorld_web/live/article_live/show.ex index 2d73106..e770bf2 100644 --- a/lib/phoenixRealWorld_web/live/article_live/show.ex +++ b/lib/phoenixRealWorld_web/live/article_live/show.ex @@ -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 diff --git a/lib/phoenixRealWorld_web/live/article_live/show.html.heex b/lib/phoenixRealWorld_web/live/article_live/show.html.heex index 4c0b8dd..2a7da6d 100644 --- a/lib/phoenixRealWorld_web/live/article_live/show.html.heex +++ b/lib/phoenixRealWorld_web/live/article_live/show.html.heex @@ -17,6 +17,24 @@ <.list> <:item title="Title">{@article.title} <:item title="Body">{@article.body} + <:item title="Comments"> + <.table id="comments" rows={@article.comments}> + <:col :let={comment} label="Author"><%= comment.author.email %> + <:col :let={comment} label="Body"><%= comment.body %> + + <.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 + + + <.back navigate={~p"/articles"}>Back to articles diff --git a/test/phoenixRealWorld_web/live/article_live_test.exs b/test/phoenixRealWorld_web/live/article_live_test.exs index eea39b6..00e1f80 100644 --- a/test/phoenixRealWorld_web/live/article_live_test.exs +++ b/test/phoenixRealWorld_web/live/article_live_test.exs @@ -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