context: user_ips, user_fingerprints

This commit is contained in:
Liam P. White 2019-08-28 18:29:36 -04:00
parent 73beb449ef
commit ab578bd4d7
4 changed files with 351 additions and 0 deletions

View file

@ -101,4 +101,196 @@ defmodule Philomena.Users do
def change_user(%User{} = user) do
User.changeset(user, %{})
end
alias Philomena.Users.Ip
@doc """
Returns the list of user_ips.
## Examples
iex> list_user_ips()
[%Ip{}, ...]
"""
def list_user_ips do
Repo.all(Ip)
end
@doc """
Gets a single ip.
Raises `Ecto.NoResultsError` if the Ip does not exist.
## Examples
iex> get_ip!(123)
%Ip{}
iex> get_ip!(456)
** (Ecto.NoResultsError)
"""
def get_ip!(id), do: Repo.get!(Ip, id)
@doc """
Creates a ip.
## Examples
iex> create_ip(%{field: value})
{:ok, %Ip{}}
iex> create_ip(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_ip(attrs \\ %{}) do
%Ip{}
|> Ip.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a ip.
## Examples
iex> update_ip(ip, %{field: new_value})
{:ok, %Ip{}}
iex> update_ip(ip, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_ip(%Ip{} = ip, attrs) do
ip
|> Ip.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Ip.
## Examples
iex> delete_ip(ip)
{:ok, %Ip{}}
iex> delete_ip(ip)
{:error, %Ecto.Changeset{}}
"""
def delete_ip(%Ip{} = ip) do
Repo.delete(ip)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking ip changes.
## Examples
iex> change_ip(ip)
%Ecto.Changeset{source: %Ip{}}
"""
def change_ip(%Ip{} = ip) do
Ip.changeset(ip, %{})
end
alias Philomena.Users.Fingerprints
@doc """
Returns the list of user_fingerprints.
## Examples
iex> list_user_fingerprints()
[%Fingerprints{}, ...]
"""
def list_user_fingerprints do
Repo.all(Fingerprints)
end
@doc """
Gets a single fingerprints.
Raises `Ecto.NoResultsError` if the Fingerprints does not exist.
## Examples
iex> get_fingerprints!(123)
%Fingerprints{}
iex> get_fingerprints!(456)
** (Ecto.NoResultsError)
"""
def get_fingerprints!(id), do: Repo.get!(Fingerprints, id)
@doc """
Creates a fingerprints.
## Examples
iex> create_fingerprints(%{field: value})
{:ok, %Fingerprints{}}
iex> create_fingerprints(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_fingerprints(attrs \\ %{}) do
%Fingerprints{}
|> Fingerprints.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a fingerprints.
## Examples
iex> update_fingerprints(fingerprints, %{field: new_value})
{:ok, %Fingerprints{}}
iex> update_fingerprints(fingerprints, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_fingerprints(%Fingerprints{} = fingerprints, attrs) do
fingerprints
|> Fingerprints.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Fingerprints.
## Examples
iex> delete_fingerprints(fingerprints)
{:ok, %Fingerprints{}}
iex> delete_fingerprints(fingerprints)
{:error, %Ecto.Changeset{}}
"""
def delete_fingerprints(%Fingerprints{} = fingerprints) do
Repo.delete(fingerprints)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking fingerprints changes.
## Examples
iex> change_fingerprints(fingerprints)
%Ecto.Changeset{source: %Fingerprints{}}
"""
def change_fingerprints(%Fingerprints{} = fingerprints) do
Fingerprints.changeset(fingerprints, %{})
end
end

View file

@ -0,0 +1,20 @@
defmodule Philomena.Users.Fingerprints do
use Ecto.Schema
import Ecto.Changeset
schema "user_fingerprints" do
belongs_to :user, Philomena.Users.User
field :fingerprint, :string
field :uses, :integer, default: 0
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(fingerprints, attrs) do
fingerprints
|> cast(attrs, [])
|> validate_required([])
end
end

20
lib/philomena/users/ip.ex Normal file
View file

@ -0,0 +1,20 @@
defmodule Philomena.Users.Ip do
use Ecto.Schema
import Ecto.Changeset
schema "user_ips" do
belongs_to :user, Philomena.Users.User
field :ip, EctoNetwork.INET
field :uses, :integer, default: 0
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(ip, attrs) do
ip
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,119 @@
defmodule Philomena.UsersTest do
use Philomena.DataCase
alias Philomena.Users
describe "user_ips" do
alias Philomena.Users.Ip
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def ip_fixture(attrs \\ %{}) do
{:ok, ip} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_ip()
ip
end
test "list_user_ips/0 returns all user_ips" do
ip = ip_fixture()
assert Users.list_user_ips() == [ip]
end
test "get_ip!/1 returns the ip with given id" do
ip = ip_fixture()
assert Users.get_ip!(ip.id) == ip
end
test "create_ip/1 with valid data creates a ip" do
assert {:ok, %Ip{} = ip} = Users.create_ip(@valid_attrs)
end
test "create_ip/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_ip(@invalid_attrs)
end
test "update_ip/2 with valid data updates the ip" do
ip = ip_fixture()
assert {:ok, %Ip{} = ip} = Users.update_ip(ip, @update_attrs)
end
test "update_ip/2 with invalid data returns error changeset" do
ip = ip_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_ip(ip, @invalid_attrs)
assert ip == Users.get_ip!(ip.id)
end
test "delete_ip/1 deletes the ip" do
ip = ip_fixture()
assert {:ok, %Ip{}} = Users.delete_ip(ip)
assert_raise Ecto.NoResultsError, fn -> Users.get_ip!(ip.id) end
end
test "change_ip/1 returns a ip changeset" do
ip = ip_fixture()
assert %Ecto.Changeset{} = Users.change_ip(ip)
end
end
describe "user_fingerprints" do
alias Philomena.Users.Fingerprints
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def fingerprints_fixture(attrs \\ %{}) do
{:ok, fingerprints} =
attrs
|> Enum.into(@valid_attrs)
|> Users.create_fingerprints()
fingerprints
end
test "list_user_fingerprints/0 returns all user_fingerprints" do
fingerprints = fingerprints_fixture()
assert Users.list_user_fingerprints() == [fingerprints]
end
test "get_fingerprints!/1 returns the fingerprints with given id" do
fingerprints = fingerprints_fixture()
assert Users.get_fingerprints!(fingerprints.id) == fingerprints
end
test "create_fingerprints/1 with valid data creates a fingerprints" do
assert {:ok, %Fingerprints{} = fingerprints} = Users.create_fingerprints(@valid_attrs)
end
test "create_fingerprints/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Users.create_fingerprints(@invalid_attrs)
end
test "update_fingerprints/2 with valid data updates the fingerprints" do
fingerprints = fingerprints_fixture()
assert {:ok, %Fingerprints{} = fingerprints} = Users.update_fingerprints(fingerprints, @update_attrs)
end
test "update_fingerprints/2 with invalid data returns error changeset" do
fingerprints = fingerprints_fixture()
assert {:error, %Ecto.Changeset{}} = Users.update_fingerprints(fingerprints, @invalid_attrs)
assert fingerprints == Users.get_fingerprints!(fingerprints.id)
end
test "delete_fingerprints/1 deletes the fingerprints" do
fingerprints = fingerprints_fixture()
assert {:ok, %Fingerprints{}} = Users.delete_fingerprints(fingerprints)
assert_raise Ecto.NoResultsError, fn -> Users.get_fingerprints!(fingerprints.id) end
end
test "change_fingerprints/1 returns a fingerprints changeset" do
fingerprints = fingerprints_fixture()
assert %Ecto.Changeset{} = Users.change_fingerprints(fingerprints)
end
end
end