context: adverts

This commit is contained in:
Liam P. White 2019-08-28 12:34:37 -04:00
parent 4b52640efc
commit 6da5633010
3 changed files with 192 additions and 0 deletions

104
lib/philomena/adverts.ex Normal file
View file

@ -0,0 +1,104 @@
defmodule Philomena.Adverts do
@moduledoc """
The Adverts context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.Adverts.Advert
@doc """
Returns the list of adverts.
## Examples
iex> list_adverts()
[%Advert{}, ...]
"""
def list_adverts do
Repo.all(Advert)
end
@doc """
Gets a single advert.
Raises `Ecto.NoResultsError` if the Advert does not exist.
## Examples
iex> get_advert!(123)
%Advert{}
iex> get_advert!(456)
** (Ecto.NoResultsError)
"""
def get_advert!(id), do: Repo.get!(Advert, id)
@doc """
Creates a advert.
## Examples
iex> create_advert(%{field: value})
{:ok, %Advert{}}
iex> create_advert(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_advert(attrs \\ %{}) do
%Advert{}
|> Advert.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a advert.
## Examples
iex> update_advert(advert, %{field: new_value})
{:ok, %Advert{}}
iex> update_advert(advert, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_advert(%Advert{} = advert, attrs) do
advert
|> Advert.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Advert.
## Examples
iex> delete_advert(advert)
{:ok, %Advert{}}
iex> delete_advert(advert)
{:error, %Ecto.Changeset{}}
"""
def delete_advert(%Advert{} = advert) do
Repo.delete(advert)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking advert changes.
## Examples
iex> change_advert(advert)
%Ecto.Changeset{source: %Advert{}}
"""
def change_advert(%Advert{} = advert) do
Advert.changeset(advert, %{})
end
end

View file

@ -0,0 +1,26 @@
defmodule Philomena.Adverts.Advert do
use Ecto.Schema
import Ecto.Changeset
schema "adverts" do
field :image, :string
field :link, :string
field :title, :string
field :clicks, :integer, default: 0
field :impressions, :integer, default: 0
field :live, :boolean, default: false
field :start_date, :naive_datetime
field :finish_date, :naive_datetime
field :restrictions, :string
field :notes, :string
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(advert, attrs) do
advert
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,62 @@
defmodule Philomena.AdvertsTest do
use Philomena.DataCase
alias Philomena.Adverts
describe "adverts" do
alias Philomena.Adverts.Advert
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def advert_fixture(attrs \\ %{}) do
{:ok, advert} =
attrs
|> Enum.into(@valid_attrs)
|> Adverts.create_advert()
advert
end
test "list_adverts/0 returns all adverts" do
advert = advert_fixture()
assert Adverts.list_adverts() == [advert]
end
test "get_advert!/1 returns the advert with given id" do
advert = advert_fixture()
assert Adverts.get_advert!(advert.id) == advert
end
test "create_advert/1 with valid data creates a advert" do
assert {:ok, %Advert{} = advert} = Adverts.create_advert(@valid_attrs)
end
test "create_advert/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Adverts.create_advert(@invalid_attrs)
end
test "update_advert/2 with valid data updates the advert" do
advert = advert_fixture()
assert {:ok, %Advert{} = advert} = Adverts.update_advert(advert, @update_attrs)
end
test "update_advert/2 with invalid data returns error changeset" do
advert = advert_fixture()
assert {:error, %Ecto.Changeset{}} = Adverts.update_advert(advert, @invalid_attrs)
assert advert == Adverts.get_advert!(advert.id)
end
test "delete_advert/1 deletes the advert" do
advert = advert_fixture()
assert {:ok, %Advert{}} = Adverts.delete_advert(advert)
assert_raise Ecto.NoResultsError, fn -> Adverts.get_advert!(advert.id) end
end
test "change_advert/1 returns a advert changeset" do
advert = advert_fixture()
assert %Ecto.Changeset{} = Adverts.change_advert(advert)
end
end
end