From 3772cf39b0c0de52d8cb1017ebee5619a96383b1 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Wed, 5 Oct 2022 22:17:17 +0100 Subject: [PATCH] Add item endpoint Create item controller to link items to lists --- lib/app/item.ex | 5 ++++- lib/app_web/controllers/item_controller.ex | 15 +++++++++++++++ lib/app_web/live/app_live.html.heex | 4 ++++ lib/app_web/router.ex | 3 +++ lib/app_web/templates/item/edit.html.heex | 11 +++++++++++ lib/app_web/views/item_view.ex | 3 +++ 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/app_web/controllers/item_controller.ex create mode 100644 lib/app_web/templates/item/edit.html.heex create mode 100644 lib/app_web/views/item_view.ex diff --git a/lib/app/item.ex b/lib/app/item.ex index 5fdb282d..a156a2c5 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -3,6 +3,7 @@ defmodule App.Item do import Ecto.Changeset import Ecto.Query alias App.{Repo, Tag, ItemTag, Person} + alias App.List, as: L alias __MODULE__ schema "items" do @@ -11,12 +12,13 @@ defmodule App.Item do belongs_to :people, Person, references: :person_id, foreign_key: :person_id many_to_many(:tags, Tag, join_through: ItemTag, on_replace: :delete) + many_to_many(:lists, L, join_through: "items_lists", on_replace: :delete) timestamps() end @doc false - def changeset(item, attrs) do + def changeset(item, attrs \\ %{}) do item |> cast(attrs, [:person_id, :status, :text]) |> validate_required([:text, :person_id]) @@ -69,6 +71,7 @@ defmodule App.Item do Item |> Repo.get!(id) |> Repo.preload(tags: from(t in Tag, order_by: t.text)) + |> Repo.preload(lists: from(l in L, order_by: l.name)) end @doc """ diff --git a/lib/app_web/controllers/item_controller.ex b/lib/app_web/controllers/item_controller.ex new file mode 100644 index 00000000..505e8a58 --- /dev/null +++ b/lib/app_web/controllers/item_controller.ex @@ -0,0 +1,15 @@ +defmodule AppWeb.ItemController do + use AppWeb, :controller + alias App.{Item, List} + # plug :permission_tag when action in [:edit, :update, :delete] + + def edit(conn, %{"id" => id}) do + person_id = conn.assigns[:person][:id] || 0 + + item = Item.get_item!(id) + lists = List.list_person_lists(person_id) + + changeset = Item.changeset(item) + render(conn, "edit.html", item: item, lists: lists, changeset: changeset) + end +end diff --git a/lib/app_web/live/app_live.html.heex b/lib/app_web/live/app_live.html.heex index d013a74a..bb9f87f2 100644 --- a/lib/app_web/live/app_live.html.heex +++ b/lib/app_web/live/app_live.html.heex @@ -258,6 +258,10 @@ <%= live_patch tag.text, to: Routes.live_path(@socket, AppWeb.AppLive, %{filter_by: @filter, filter_by_tag: tag.text}), style: "background-color:#{tag.color}", class: " text-white font-bold py-1 px-2 rounded-full" %> <% end %> + +
+ <.a to={Routes.item_path(@socket, :edit, item.id)} class="" label="Edit lists" /> +
<% end %> <% end %> diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index f07266d1..706e3934 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -34,6 +34,9 @@ defmodule AppWeb.Router do resources "/lists", ListController, except: [:show] get "/login", AuthController, :login get "/logout", AuthController, :logout + + # edit item lists + resources "/items", ItemController, only: [:edit, :update] end # assign to conn the loggedin value used in templates diff --git a/lib/app_web/templates/item/edit.html.heex b/lib/app_web/templates/item/edit.html.heex new file mode 100644 index 00000000..a6f71f13 --- /dev/null +++ b/lib/app_web/templates/item/edit.html.heex @@ -0,0 +1,11 @@ +edit item + +<.form let={f} for={@changeset} action="" method="patch" class="py-3"> + <%= if @changeset.action do %> +
+

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

+
+ <% end %> + + <.button type="submit" label="Save" /> + diff --git a/lib/app_web/views/item_view.ex b/lib/app_web/views/item_view.ex new file mode 100644 index 00000000..84240e42 --- /dev/null +++ b/lib/app_web/views/item_view.ex @@ -0,0 +1,3 @@ +defmodule AppWeb.ItemView do + use AppWeb, :view +end