Skip to content

Commit

Permalink
Add list controller
Browse files Browse the repository at this point in the history
Create list controller to add and edit lists
  • Loading branch information
SimonLab committed Oct 5, 2022
1 parent e75e79e commit 32e98de
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/app/list.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule App.List do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias App.{Item, Person, Repo}
alias __MODULE__

Expand Down Expand Up @@ -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
75 changes: 75 additions & 0 deletions lib/app_web/controllers/list_controller.ex
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/app_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
<%= if @loggedin do %>
<div class="flex justify-between items-center">
<div class="mr-1 inline-flex items-center pt-1 px-3 py-1 text-sm">

<%= link "tags", to: "/tags", class: "text-white font-bold" %>
</div>

<div class="mr-1 inline-flex items-center pt-1 px-3 py-1 text-sm">
<%= link "lists", to: "/lists", class: "text-white font-bold" %>
</div>

<div class="mr-1 inline-flex items-center -pt-1 px-3 py-1 text-sm
bg-red-600 hover:bg-red-700 text-white rounded-md">
Expand Down
8 changes: 8 additions & 0 deletions lib/app_web/templates/list/edit.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.container>
<.h2 class="text-center mt-3">Edit List</.h2>

<%= 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" />

</.container>
14 changes: 14 additions & 0 deletions lib/app_web/templates/list/form.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<.form :let={f} for={@changeset} action={@action} method={@method} 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 %>
<.form_field
type="text_input"
form={f}
field={:name}
/>

<.button type="submit" label="Save" />
</.form>
26 changes: 26 additions & 0 deletions lib/app_web/templates/list/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<.h2 class="text-center mt-3">Listing lists</.h2>
<.container>
<.table>
<.tr>
<.th>Name</.th>

<.th class="w-3"></.th>
<.th class="w-3"></.th>
</.tr>
<%= for list <- @lists do %>
<.tr>
<.td>
<%= list.name %>
</.td>

<.td>
<%= link "Edit", to: Routes.list_path(@conn, :edit, list) %>
</.td>
<.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?"] %>
</.td>
</.tr>
<% end %>
</.table>
<.button link_type="a" to={Routes.list_path(@conn, :new)} label="New list" class="my-3"/>
</.container>
8 changes: 8 additions & 0 deletions lib/app_web/templates/list/new.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.container>
<.h2 class="text-center mt-3">New list</.h2>

<%= 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" />

</.container>
2 changes: 1 addition & 1 deletion lib/app_web/templates/tag/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

</.container>
4 changes: 2 additions & 2 deletions lib/app_web/templates/tag/form.html.heex
Original file line number Diff line number Diff line change
@@ -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 %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
Expand All @@ -12,4 +12,4 @@
<.form_field type="color_input" form={f} field={:color}/>

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

0 comments on commit 32e98de

Please sign in to comment.