From 13e3c6b9cd195f33204944b7b00f5939da382c02 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Wed, 5 Oct 2022 21:08:21 +0100 Subject: [PATCH] Add list controller Create list controller to add and edit lists --- lib/app/list.ex | 12 ++++ lib/app_web/controllers/list_controller.ex | 75 +++++++++++++++++++++ lib/app_web/router.ex | 1 + lib/app_web/templates/layout/root.html.heex | 5 +- lib/app_web/templates/list/edit.html.heex | 8 +++ lib/app_web/templates/list/form.html.heex | 14 ++++ lib/app_web/templates/list/index.html.heex | 26 +++++++ lib/app_web/templates/list/new.html.heex | 8 +++ lib/app_web/templates/tag/edit.html.heex | 2 +- lib/app_web/templates/tag/form.html.heex | 4 +- lib/app_web/views/list_view.ex | 3 + 11 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 lib/app_web/controllers/list_controller.ex create mode 100644 lib/app_web/templates/list/edit.html.heex create mode 100644 lib/app_web/templates/list/form.html.heex create mode 100644 lib/app_web/templates/list/index.html.heex create mode 100644 lib/app_web/templates/list/new.html.heex create mode 100644 lib/app_web/views/list_view.ex diff --git a/lib/app/list.ex b/lib/app/list.ex index 9c267868..98bc42f7 100644 --- a/lib/app/list.ex +++ b/lib/app/list.ex @@ -1,6 +1,7 @@ defmodule App.List do use Ecto.Schema import Ecto.Changeset + import Ecto.Query alias App.{Item, Person, Repo} alias __MODULE__ @@ -29,9 +30,20 @@ defmodule App.List do def get_list!(id), do: Repo.get!(List, id) + def list_person_lists(person_id) do + List + |> where(person_id: ^person_id) + |> order_by(:name) + |> Repo.all() + end + def update_list(%List{} = list, attrs) do list |> List.changeset(attrs) |> Repo.update() end + + def delete_list(%List{} = list) do + Repo.delete(list) + end end diff --git a/lib/app_web/controllers/list_controller.ex b/lib/app_web/controllers/list_controller.ex new file mode 100644 index 00000000..0250e0b5 --- /dev/null +++ b/lib/app_web/controllers/list_controller.ex @@ -0,0 +1,75 @@ +defmodule AppWeb.ListController do + use AppWeb, :controller + alias App.List + plug :permission_list when action in [:edit, :update, :delete] + + def index(conn, _params) do + person_id = conn.assigns[:person][:id] || 0 + lists = List.list_person_lists(person_id) + + render(conn, "index.html", lists: lists) + end + + def new(conn, _params) do + changeset = List.changeset(%List{}) + render(conn, "new.html", changeset: changeset) + end + + def create(conn, %{"list" => list_params}) do + person_id = conn.assigns[:person][:id] || 0 + list_params = Map.put(list_params, "person_id", person_id) + + case List.create_list(list_params) do + {:ok, _list} -> + conn + |> put_flash(:info, "List created successfully.") + |> redirect(to: Routes.list_path(conn, :index)) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "new.html", changeset: changeset) + end + end + + def edit(conn, %{"id" => id}) do + list = List.get_list!(id) + changeset = List.changeset(list) + render(conn, "edit.html", list: list, changeset: changeset) + end + + def update(conn, %{"id" => id, "list" => list_params}) do + list = List.get_list!(id) + + case List.update_list(list, list_params) do + {:ok, _list} -> + conn + |> put_flash(:info, "List updated successfully.") + |> redirect(to: Routes.list_path(conn, :index)) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "edit.html", list: list, changeset: changeset) + end + end + + def delete(conn, %{"id" => id}) do + list = List.get_list!(id) + {:ok, _list} = List.delete_list(list) + + conn + |> put_flash(:info, "list deleted successfully.") + |> redirect(to: Routes.list_path(conn, :index)) + end + + defp permission_list(conn, _opts) do + list = List.get_list!(conn.params["id"]) + person_id = conn.assigns[:person][:id] || 0 + + if list.person_id == person_id do + conn + else + conn + |> put_flash(:info, "You can't access that page") + |> redirect(to: "/tags") + |> halt() + end + end +end diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index 5cd023e6..f07266d1 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -31,6 +31,7 @@ defmodule AppWeb.Router do pipe_through [:check_profile_name] live "/", AppLive resources "/tags", TagController, except: [:show] + resources "/lists", ListController, except: [:show] get "/login", AuthController, :login get "/logout", AuthController, :logout end diff --git a/lib/app_web/templates/layout/root.html.heex b/lib/app_web/templates/layout/root.html.heex index ca048d74..ba2551c8 100644 --- a/lib/app_web/templates/layout/root.html.heex +++ b/lib/app_web/templates/layout/root.html.heex @@ -32,9 +32,12 @@ <%= if @loggedin do %>
- <%= link "tags", to: "/tags", class: "text-white font-bold" %>
+ +
+ <%= link "lists", to: "/lists", class: "text-white font-bold" %> +
diff --git a/lib/app_web/templates/list/edit.html.heex b/lib/app_web/templates/list/edit.html.heex new file mode 100644 index 00000000..314f6033 --- /dev/null +++ b/lib/app_web/templates/list/edit.html.heex @@ -0,0 +1,8 @@ +<.container> +<.h2 class="text-center mt-3">Edit List + +<%= render "form.html", Map.put(assigns, :action, Routes.list_path(@conn, :update, @list)) |> Map.put(:method, "patch") %> + +<.a to={Routes.list_path(@conn, :index)} label="Back to lists" /> + + diff --git a/lib/app_web/templates/list/form.html.heex b/lib/app_web/templates/list/form.html.heex new file mode 100644 index 00000000..96765d03 --- /dev/null +++ b/lib/app_web/templates/list/form.html.heex @@ -0,0 +1,14 @@ + <.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.

+
+ <% end %> + <.form_field + type="text_input" + form={f} + field={:name} + /> + + <.button type="submit" label="Save" /> + diff --git a/lib/app_web/templates/list/index.html.heex b/lib/app_web/templates/list/index.html.heex new file mode 100644 index 00000000..afb335c3 --- /dev/null +++ b/lib/app_web/templates/list/index.html.heex @@ -0,0 +1,26 @@ +<.h2 class="text-center mt-3">Listing lists +<.container> +<.table> + <.tr> + <.th>Name + + <.th class="w-3"> + <.th class="w-3"> + +<%= for list <- @lists do %> + <.tr> + <.td> + <%= list.name %> + + + <.td> + <%= link "Edit", to: Routes.list_path(@conn, :edit, list) %> + + <.td class="!text-red-500"> + <%= link "Delete", to: Routes.list_path(@conn, :delete, list), method: :delete, data: [confirm: "Are you sure you want to delete this list?"] %> + + +<% end %> + +<.button link_type="a" to={Routes.list_path(@conn, :new)} label="New list" class="my-3"/> + diff --git a/lib/app_web/templates/list/new.html.heex b/lib/app_web/templates/list/new.html.heex new file mode 100644 index 00000000..c46aff85 --- /dev/null +++ b/lib/app_web/templates/list/new.html.heex @@ -0,0 +1,8 @@ +<.container> +<.h2 class="text-center mt-3">New list + +<%= render "form.html", Map.put(assigns, :action, Routes.list_path(@conn, :create)) |> Map.put(:method, "post") %> + +<.a to={Routes.list_path(@conn, :index)} label="Back to lists" /> + + diff --git a/lib/app_web/templates/tag/edit.html.heex b/lib/app_web/templates/tag/edit.html.heex index d9fde6ac..db661fec 100644 --- a/lib/app_web/templates/tag/edit.html.heex +++ b/lib/app_web/templates/tag/edit.html.heex @@ -3,6 +3,6 @@ <%= render "form.html", Map.put(assigns, :action, Routes.tag_path(@conn, :update, @tag)) %> -<.a to={Routes.tag_path(@conn, :index)} class="" label="Back to tags" /> +<.a to={Routes.tag_path(@conn, :index)} 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 462d61ff..dd938aac 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="patch" class="py-3"> <%= if @changeset.action do %>

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

@@ -12,4 +12,4 @@ <.form_field type="color_input" form={f} field={:color}/> <.button type="submit" label="Save" /> - + diff --git a/lib/app_web/views/list_view.ex b/lib/app_web/views/list_view.ex new file mode 100644 index 00000000..89a80c28 --- /dev/null +++ b/lib/app_web/views/list_view.ex @@ -0,0 +1,3 @@ +defmodule AppWeb.ListView do + use AppWeb, :view +end