Add Articles

mix phx.gen.live Articles Article articles title:string\                                                            /Crucial/git/phoenix-liveview-book
  content:text url:string language:string\
  date:utc_datetime author_id:references:authors
This commit is contained in:
Thelonius Kort
2022-12-26 18:02:29 +01:00
parent 005a9d9337
commit f7f1e1a284
13 changed files with 582 additions and 0 deletions

104
lib/outlook/articles.ex Normal file
View File

@ -0,0 +1,104 @@
defmodule Outlook.Articles do
@moduledoc """
The Articles context.
"""
import Ecto.Query, warn: false
alias Outlook.Repo
alias Outlook.Articles.Article
@doc """
Returns the list of articles.
## Examples
iex> list_articles()
[%Article{}, ...]
"""
def list_articles do
Repo.all(Article)
end
@doc """
Gets a single article.
Raises `Ecto.NoResultsError` if the Article does not exist.
## Examples
iex> get_article!(123)
%Article{}
iex> get_article!(456)
** (Ecto.NoResultsError)
"""
def get_article!(id), do: Repo.get!(Article, id)
@doc """
Creates a article.
## Examples
iex> create_article(%{field: value})
{:ok, %Article{}}
iex> create_article(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_article(attrs \\ %{}) do
%Article{}
|> Article.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a article.
## Examples
iex> update_article(article, %{field: new_value})
{:ok, %Article{}}
iex> update_article(article, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_article(%Article{} = article, attrs) do
article
|> Article.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a article.
## Examples
iex> delete_article(article)
{:ok, %Article{}}
iex> delete_article(article)
{:error, %Ecto.Changeset{}}
"""
def delete_article(%Article{} = article) do
Repo.delete(article)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking article changes.
## Examples
iex> change_article(article)
%Ecto.Changeset{data: %Article{}}
"""
def change_article(%Article{} = article, attrs \\ %{}) do
Article.changeset(article, attrs)
end
end

View File

@ -0,0 +1,24 @@
defmodule Outlook.Articles.Article do
use Ecto.Schema
import Ecto.Changeset
alias Outlook.Authors.Author
schema "articles" do
field :content, :string
field :date, :utc_datetime
field :language, :string
field :title, :string
field :url, :string
belongs_to :author, Author
timestamps()
end
@doc false
def changeset(article, attrs) do
article
|> cast(attrs, [:title, :content, :url, :language, :date])
|> validate_required([:title, :content, :url, :language, :date])
end
end

View File

@ -2,11 +2,14 @@ defmodule Outlook.Authors.Author do
use Ecto.Schema
import Ecto.Changeset
alias Outlook.Articles.Article
schema "authors" do
field :description, :string
field :homepage_name, :string
field :homepage_url, :string
field :name, :string
has_many :articles, Article
timestamps()
end