-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create list controller to add and edit lists
- Loading branch information
Showing
11 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule AppWeb.ListView do | ||
use AppWeb, :view | ||
end |