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
|
||||
Reference in New Issue
Block a user