From da4289b9744b1176182f2b168440d13860c319dd Mon Sep 17 00:00:00 2001 From: SimonLab Date: Mon, 21 Nov 2022 12:38:51 +0000 Subject: [PATCH 1/2] Allow user to create new tag Display form for creating new tags --- lib/app_web/controllers/tag_controller.ex | 20 ++++++++++++++++++++ lib/app_web/templates/tag/edit.html.heex | 4 +++- lib/app_web/templates/tag/form.html.heex | 2 +- lib/app_web/templates/tag/index.html.heex | 9 ++++++++- lib/app_web/templates/tag/new.html.heex | 12 ++++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 lib/app_web/templates/tag/new.html.heex diff --git a/lib/app_web/controllers/tag_controller.ex b/lib/app_web/controllers/tag_controller.ex index de7ed212..965b5361 100644 --- a/lib/app_web/controllers/tag_controller.ex +++ b/lib/app_web/controllers/tag_controller.ex @@ -10,6 +10,26 @@ defmodule AppWeb.TagController do render(conn, "index.html", tags: tags) end + def new(conn, _params) do + changeset = Tag.changeset(%Tag{}) + render(conn, "new.html", changeset: changeset) + end + + def create(conn, %{"tag" => tag_params}) do + person_id = conn.assigns[:person][:id] || 0 + tag_params = Map.put(tag_params, "person_id", person_id) + + case Tag.create_tag(tag_params) do + {:ok, _tag} -> + conn + |> put_flash(:info, "Tag created successfully.") + |> redirect(to: Routes.tag_path(conn, :index)) + + {:error, changeset} -> + render(conn, "new.html", changeset: changeset) + end + end + def edit(conn, %{"id" => id}) do tag = Tag.get_tag!(id) changeset = Tag.changeset(tag) diff --git a/lib/app_web/templates/tag/edit.html.heex b/lib/app_web/templates/tag/edit.html.heex index e63c63ad..40793263 100644 --- a/lib/app_web/templates/tag/edit.html.heex +++ b/lib/app_web/templates/tag/edit.html.heex @@ -3,7 +3,9 @@ <%= render( "form.html", - Map.put(assigns, :action, Routes.tag_path(@conn, :update, @tag)) + assigns + |> Map.put(:action, Routes.tag_path(@conn, :update, @tag)) + |> Map.put(:method, "patch") ) %> <.a to={Routes.tag_path(@conn, :index)} class="" label="Back to tags" /> diff --git a/lib/app_web/templates/tag/form.html.heex b/lib/app_web/templates/tag/form.html.heex index 4eac2a72..0f9d2302 100644 --- a/lib/app_web/templates/tag/form.html.heex +++ b/lib/app_web/templates/tag/form.html.heex @@ -1,4 +1,4 @@ -<.form :let={f} for={@changeset} action={@action} method="patch" class="py-3"> +<.form :let={f} for={@changeset} action={@action} method={@method} class="py-3"> <%= if @changeset.action do %>

Oops, something went wrong! Please check the errors below.

diff --git a/lib/app_web/templates/tag/index.html.heex b/lib/app_web/templates/tag/index.html.heex index 5e68776c..756d3ba1 100644 --- a/lib/app_web/templates/tag/index.html.heex +++ b/lib/app_web/templates/tag/index.html.heex @@ -1,6 +1,7 @@ <.h2 class="text-center mt-3">Listing Tags + <.container> - <.table> + <.table class="my-4"> <.tr> <.th>Name @@ -31,4 +32,10 @@ <% end %> + <.button + link_type="a" + to={Routes.tag_path(@conn, :new)} + label="Create Tag" + class="my-2" + /> diff --git a/lib/app_web/templates/tag/new.html.heex b/lib/app_web/templates/tag/new.html.heex new file mode 100644 index 00000000..8478473d --- /dev/null +++ b/lib/app_web/templates/tag/new.html.heex @@ -0,0 +1,12 @@ +<.container> + <.h2 class="text-center mt-3">New Tag + + <%= render( + "form.html", + assigns + |> Map.put(:action, Routes.tag_path(@conn, :create)) + |> Map.put(:method, "post") + ) %> + + <.a to={Routes.tag_path(@conn, :index)} class="" label="Back to tags" /> + From 8d1fbe1f72fd43796b5fad55337aef6cef46028c Mon Sep 17 00:00:00 2001 From: SimonLab Date: Mon, 21 Nov 2022 13:05:03 +0000 Subject: [PATCH 2/2] Add test for creating tags Add tests for post and new endpoint --- .../controllers/tag_controller_test.exs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/app_web/controllers/tag_controller_test.exs b/test/app_web/controllers/tag_controller_test.exs index 96c5cd0c..5695bcc3 100644 --- a/test/app_web/controllers/tag_controller_test.exs +++ b/test/app_web/controllers/tag_controller_test.exs @@ -31,6 +31,39 @@ defmodule AppWeb.TagControllerTest do end end + describe "new tag" do + test "renders form for creating a tag", %{conn: conn} do + conn = + conn + |> assign(:person, %{id: 1}) + |> get(Routes.tag_path(conn, :new)) + + assert html_response(conn, 200) =~ "New Tag" + end + end + + describe "create tag" do + test "redirects to show when data is valid", %{conn: conn} do + conn = + conn + |> assign(:person, %{id: 1}) + |> post(Routes.tag_path(conn, :create), + tag: %{text: "new tag", color: "#FCA5A5"} + ) + + assert redirected_to(conn) == Routes.tag_path(conn, :index) + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = + conn + |> assign(:person, %{id: 1}) + |> post(Routes.tag_path(conn, :create), tag: @invalid_attrs) + + assert html_response(conn, 200) =~ "New Tag" + end + end + describe "edit tag" do setup [:create_tag]