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 %>
Oops, something went wrong! Please check the errors below.
+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