From 09433c76fc5cb335444fbf9260c0fa6d14930157 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Mon, 4 Dec 2023 00:54:16 +0000 Subject: [PATCH] Add price and filter homepage --- lib/wish/wishlist.ex | 13 +++++++++++++ lib/wish/wishlist/item.ex | 3 ++- lib/wish_web/live/home_live/index.ex | 9 ++++++++- lib/wish_web/live/item_live/form_component.ex | 1 + lib/wish_web/live/item_live/index.html.heex | 1 + lib/wish_web/live/item_live/show.html.heex | 1 + .../migrations/20231203201659_add_item_price.exs | 9 +++++++++ 7 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 priv/repo/migrations/20231203201659_add_item_price.exs diff --git a/lib/wish/wishlist.ex b/lib/wish/wishlist.ex index b550900..3780517 100644 --- a/lib/wish/wishlist.ex +++ b/lib/wish/wishlist.ex @@ -21,6 +21,19 @@ defmodule Wish.Wishlist do Repo.all(Item) end + @doc """ + Returns the list of unreceived items. + + ## Examples + + iex> list_available_items() + [%Item{}, ...] + + """ + def list_available_items do + Repo.all(from i in Item, where: not i.received) + end + @doc """ Gets a single item. diff --git a/lib/wish/wishlist/item.ex b/lib/wish/wishlist/item.ex index 341c38b..037eb5e 100644 --- a/lib/wish/wishlist/item.ex +++ b/lib/wish/wishlist/item.ex @@ -9,6 +9,7 @@ defmodule Wish.Wishlist.Item do field :received, :boolean, default: false field :desire, :integer field :image_url, :string + field :price, :integer timestamps(type: :utc_datetime) end @@ -16,7 +17,7 @@ defmodule Wish.Wishlist.Item do @doc false def changeset(item, attrs) do item - |> cast(attrs, [:title, :description, :url, :received, :desire, :image_url]) + |> cast(attrs, [:title, :description, :url, :price, :received, :desire, :image_url]) |> validate_required([:url]) end diff --git a/lib/wish_web/live/home_live/index.ex b/lib/wish_web/live/home_live/index.ex index b9fdefe..04c5788 100644 --- a/lib/wish_web/live/home_live/index.ex +++ b/lib/wish_web/live/home_live/index.ex @@ -12,7 +12,14 @@ defmodule WishWeb.HomeLive.Index do _ -> false end - {:ok, assign(socket, :items, Wishlist.list_items()) |> assign(:grid, grid?)} + items = + if socket.assigns.current_user do + Wishlist.list_items() + else + Wishlist.list_available_items() + end + + {:ok, assign(socket, :items, items) |> assign(:grid, grid?)} end @impl true diff --git a/lib/wish_web/live/item_live/form_component.ex b/lib/wish_web/live/item_live/form_component.ex index 913fff4..f8bb55e 100644 --- a/lib/wish_web/live/item_live/form_component.ex +++ b/lib/wish_web/live/item_live/form_component.ex @@ -21,6 +21,7 @@ defmodule WishWeb.ItemLive.FormComponent do > <.input field={@form[:title]} type="text" label="Title" /> <.input field={@form[:description]} type="text" label="Description" /> + <.input field={@form[:price]} type="number" min="0" step="any" label="Price" /> <.input field={@form[:url]} type="text" label="Url" /> <.input field={@form[:image_url]} type="text" label="Image URL" /> <.input field={@form[:desire]} type="number" label="Desire" /> diff --git a/lib/wish_web/live/item_live/index.html.heex b/lib/wish_web/live/item_live/index.html.heex index c63b98f..f58ce22 100644 --- a/lib/wish_web/live/item_live/index.html.heex +++ b/lib/wish_web/live/item_live/index.html.heex @@ -14,6 +14,7 @@ > <:col :let={{_id, item}} label="Title"><%= item.title %> <:col :let={{_id, item}} label="Description"><%= item.description %> + <:col :let={{_id, item}} label="Price"><%= item.price %> <:col :let={{_id, item}} label="Desire"><%= item.desire %> <:action :let={{id, item}}> <.link diff --git a/lib/wish_web/live/item_live/show.html.heex b/lib/wish_web/live/item_live/show.html.heex index 0aefa42..7b228f9 100644 --- a/lib/wish_web/live/item_live/show.html.heex +++ b/lib/wish_web/live/item_live/show.html.heex @@ -6,6 +6,7 @@ <.simple_form for={@form} id="item-form" phx-change="validate" phx-submit="save"> <.input field={@form[:title]} type="text" label="Title" /> <.input field={@form[:description]} type="text" label="Description" /> + <.input field={@form[:price]} type="number" min="0" step="any" label="Price" /> <.input field={@form[:url]} type="text" label="Url" /> <.input field={@form[:image_url]} type="text" label="Image URL" /> <.input field={@form[:desire]} type="number" label="Desire" /> diff --git a/priv/repo/migrations/20231203201659_add_item_price.exs b/priv/repo/migrations/20231203201659_add_item_price.exs new file mode 100644 index 0000000..8a96cfe --- /dev/null +++ b/priv/repo/migrations/20231203201659_add_item_price.exs @@ -0,0 +1,9 @@ +defmodule Wish.Repo.Migrations.AddItemPrice do + use Ecto.Migration + + def change do + alter table("items") do + add :price, :integer + end + end +end