105 lines
1.6 KiB
Elixir
105 lines
1.6 KiB
Elixir
defmodule Bright.Urls do
|
|
@moduledoc """
|
|
The Urls context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Bright.Repo
|
|
|
|
alias Bright.Urls.Url
|
|
|
|
@doc """
|
|
Returns the list of urls.
|
|
|
|
## Examples
|
|
|
|
iex> list_urls()
|
|
[%Url{}, ...]
|
|
|
|
"""
|
|
def list_urls do
|
|
Repo.all(Url)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single url.
|
|
|
|
Raises `Ecto.NoResultsError` if the Url does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_url!(123)
|
|
%Url{}
|
|
|
|
iex> get_url!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_url!(id), do: Repo.get!(Url, id)
|
|
|
|
@doc """
|
|
Creates a url.
|
|
|
|
## Examples
|
|
|
|
iex> create_url(%{field: value})
|
|
{:ok, %Url{}}
|
|
|
|
iex> create_url(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_url(attrs \\ %{}) do
|
|
%Url{}
|
|
|> Url.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a url.
|
|
|
|
## Examples
|
|
|
|
iex> update_url(url, %{field: new_value})
|
|
{:ok, %Url{}}
|
|
|
|
iex> update_url(url, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_url(%Url{} = url, attrs) do
|
|
url
|
|
|> Url.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a url.
|
|
|
|
## Examples
|
|
|
|
iex> delete_url(url)
|
|
{:ok, %Url{}}
|
|
|
|
iex> delete_url(url)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_url(%Url{} = url) do
|
|
Repo.delete(url)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking url changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_url(url)
|
|
%Ecto.Changeset{data: %Url{}}
|
|
|
|
"""
|
|
def change_url(%Url{} = url, attrs \\ %{}) do
|
|
Url.changeset(url, attrs)
|
|
end
|
|
end
|