Add Authors
mix phx.gen.live Authors Author authors name:string description:text homepage_name:string homepage_url:string
This commit is contained in:
104
lib/outlook/authors.ex
Normal file
104
lib/outlook/authors.ex
Normal file
@ -0,0 +1,104 @@
|
||||
defmodule Outlook.Authors do
|
||||
@moduledoc """
|
||||
The Authors context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Outlook.Repo
|
||||
|
||||
alias Outlook.Authors.Author
|
||||
|
||||
@doc """
|
||||
Returns the list of authors.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_authors()
|
||||
[%Author{}, ...]
|
||||
|
||||
"""
|
||||
def list_authors do
|
||||
Repo.all(Author)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single author.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Author does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_author!(123)
|
||||
%Author{}
|
||||
|
||||
iex> get_author!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_author!(id), do: Repo.get!(Author, id)
|
||||
|
||||
@doc """
|
||||
Creates a author.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_author(%{field: value})
|
||||
{:ok, %Author{}}
|
||||
|
||||
iex> create_author(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_author(attrs \\ %{}) do
|
||||
%Author{}
|
||||
|> Author.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a author.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_author(author, %{field: new_value})
|
||||
{:ok, %Author{}}
|
||||
|
||||
iex> update_author(author, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_author(%Author{} = author, attrs) do
|
||||
author
|
||||
|> Author.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a author.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_author(author)
|
||||
{:ok, %Author{}}
|
||||
|
||||
iex> delete_author(author)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_author(%Author{} = author) do
|
||||
Repo.delete(author)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking author changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_author(author)
|
||||
%Ecto.Changeset{data: %Author{}}
|
||||
|
||||
"""
|
||||
def change_author(%Author{} = author, attrs \\ %{}) do
|
||||
Author.changeset(author, attrs)
|
||||
end
|
||||
end
|
||||
20
lib/outlook/authors/author.ex
Normal file
20
lib/outlook/authors/author.ex
Normal file
@ -0,0 +1,20 @@
|
||||
defmodule Outlook.Authors.Author do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "authors" do
|
||||
field :description, :string
|
||||
field :homepage_name, :string
|
||||
field :homepage_url, :string
|
||||
field :name, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(author, attrs) do
|
||||
author
|
||||
|> cast(attrs, [:name, :description, :homepage_name, :homepage_url])
|
||||
|> validate_required([:name, :description, :homepage_name, :homepage_url])
|
||||
end
|
||||
end
|
||||
84
lib/outlook_web/live/author_live/form_component.ex
Normal file
84
lib/outlook_web/live/author_live/form_component.ex
Normal file
@ -0,0 +1,84 @@
|
||||
defmodule OutlookWeb.AuthorLive.FormComponent do
|
||||
use OutlookWeb, :live_component
|
||||
|
||||
alias Outlook.Authors
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
<%= @title %>
|
||||
<:subtitle>Use this form to manage author records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
:let={f}
|
||||
for={@changeset}
|
||||
id="author-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={{f, :name}} type="text" label="name" />
|
||||
<.input field={{f, :description}} type="text" label="description" />
|
||||
<.input field={{f, :homepage_name}} type="text" label="homepage_name" />
|
||||
<.input field={{f, :homepage_url}} type="text" label="homepage_url" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Author</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{author: author} = assigns, socket) do
|
||||
changeset = Authors.change_author(author)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"author" => author_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.author
|
||||
|> Authors.change_author(author_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"author" => author_params}, socket) do
|
||||
save_author(socket, socket.assigns.action, author_params)
|
||||
end
|
||||
|
||||
defp save_author(socket, :edit, author_params) do
|
||||
case Authors.update_author(socket.assigns.author, author_params) do
|
||||
{:ok, _author} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Author updated successfully")
|
||||
|> push_navigate(to: socket.assigns.navigate)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_author(socket, :new, author_params) do
|
||||
case Authors.create_author(author_params) do
|
||||
{:ok, _author} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Author created successfully")
|
||||
|> push_navigate(to: socket.assigns.navigate)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
46
lib/outlook_web/live/author_live/index.ex
Normal file
46
lib/outlook_web/live/author_live/index.ex
Normal file
@ -0,0 +1,46 @@
|
||||
defmodule OutlookWeb.AuthorLive.Index do
|
||||
use OutlookWeb, :live_view
|
||||
|
||||
alias Outlook.Authors
|
||||
alias Outlook.Authors.Author
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :authors, list_authors())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Author")
|
||||
|> assign(:author, Authors.get_author!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Author")
|
||||
|> assign(:author, %Author{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Authors")
|
||||
|> assign(:author, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
author = Authors.get_author!(id)
|
||||
{:ok, _} = Authors.delete_author(author)
|
||||
|
||||
{:noreply, assign(socket, :authors, list_authors())}
|
||||
end
|
||||
|
||||
defp list_authors do
|
||||
Authors.list_authors()
|
||||
end
|
||||
end
|
||||
42
lib/outlook_web/live/author_live/index.html.heex
Normal file
42
lib/outlook_web/live/author_live/index.html.heex
Normal file
@ -0,0 +1,42 @@
|
||||
<.header>
|
||||
Listing Authors
|
||||
<:actions>
|
||||
<.link patch={~p"/authors/new"}>
|
||||
<.button>New Author</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table id="authors" rows={@authors} row_click={&JS.navigate(~p"/authors/#{&1}")}>
|
||||
<:col :let={author} label="Name"><%= author.name %></:col>
|
||||
<:col :let={author} label="Description"><%= author.description %></:col>
|
||||
<:col :let={author} label="Homepage name"><%= author.homepage_name %></:col>
|
||||
<:col :let={author} label="Homepage url"><%= author.homepage_url %></:col>
|
||||
<:action :let={author}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/authors/#{author}"}>Show</.link>
|
||||
</div>
|
||||
<.link patch={~p"/authors/#{author}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={author}>
|
||||
<.link phx-click={JS.push("delete", value: %{id: author.id})} data-confirm="Are you sure?">
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.modal
|
||||
:if={@live_action in [:new, :edit]}
|
||||
id="author-modal"
|
||||
show
|
||||
on_cancel={JS.navigate(~p"/authors")}
|
||||
>
|
||||
<.live_component
|
||||
module={OutlookWeb.AuthorLive.FormComponent}
|
||||
id={@author.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
author={@author}
|
||||
navigate={~p"/authors"}
|
||||
/>
|
||||
</.modal>
|
||||
21
lib/outlook_web/live/author_live/show.ex
Normal file
21
lib/outlook_web/live/author_live/show.ex
Normal file
@ -0,0 +1,21 @@
|
||||
defmodule OutlookWeb.AuthorLive.Show do
|
||||
use OutlookWeb, :live_view
|
||||
|
||||
alias Outlook.Authors
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:author, Authors.get_author!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Author"
|
||||
defp page_title(:edit), do: "Edit Author"
|
||||
end
|
||||
29
lib/outlook_web/live/author_live/show.html.heex
Normal file
29
lib/outlook_web/live/author_live/show.html.heex
Normal file
@ -0,0 +1,29 @@
|
||||
<.header>
|
||||
Author <%= @author.id %>
|
||||
<:subtitle>This is a author record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/authors/#{@author}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit author</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Name"><%= @author.name %></:item>
|
||||
<:item title="Description"><%= @author.description %></:item>
|
||||
<:item title="Homepage name"><%= @author.homepage_name %></:item>
|
||||
<:item title="Homepage url"><%= @author.homepage_url %></:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/authors"}>Back to authors</.back>
|
||||
|
||||
<.modal :if={@live_action == :edit} id="author-modal" show on_cancel={JS.patch(~p"/authors/#{@author}")}>
|
||||
<.live_component
|
||||
module={OutlookWeb.AuthorLive.FormComponent}
|
||||
id={@author.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
author={@author}
|
||||
navigate={~p"/authors/#{@author}"}
|
||||
/>
|
||||
</.modal>
|
||||
@ -69,6 +69,13 @@ defmodule OutlookWeb.Router do
|
||||
live "/users/settings", UserSettingsLive, :edit
|
||||
live "/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email
|
||||
end
|
||||
|
||||
live "/authors", AuthorLive.Index, :index
|
||||
live "/authors/new", AuthorLive.Index, :new
|
||||
live "/authors/:id/edit", AuthorLive.Index, :edit
|
||||
|
||||
live "/authors/:id", AuthorLive.Show, :show
|
||||
live "/authors/:id/show/edit", AuthorLive.Show, :edit
|
||||
end
|
||||
|
||||
scope "/", OutlookWeb do
|
||||
|
||||
Reference in New Issue
Block a user