Skip to content

Commit

Permalink
Add item endpoint
Browse files Browse the repository at this point in the history
Create item controller to link items to lists
  • Loading branch information
SimonLab committed Oct 5, 2022
1 parent 32e98de commit 3772cf3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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 """
Expand Down
15 changes: 15 additions & 0 deletions lib/app_web/controllers/item_controller.ex
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>

<div class="ml-2 my-2">
<.a to={Routes.item_path(@socket, :edit, item.id)} class="" label="Edit lists" />
</div>
<% end %>
<% end %><!-- end for item <- @items -->
</ul>
Expand Down
3 changes: 3 additions & 0 deletions lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/app_web/templates/item/edit.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
edit item

<.form let={f} for={@changeset} action="" method="patch" class="py-3">
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<.button type="submit" label="Save" />
</.form>
3 changes: 3 additions & 0 deletions lib/app_web/views/item_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AppWeb.ItemView do
use AppWeb, :view
end

0 comments on commit 3772cf3

Please sign in to comment.