diff --git a/lib/philomena/versions.ex b/lib/philomena/versions.ex new file mode 100644 index 00000000..e37e281c --- /dev/null +++ b/lib/philomena/versions.ex @@ -0,0 +1,104 @@ +defmodule Philomena.Versions do + @moduledoc """ + The Versions context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Versions.Version + + @doc """ + Returns the list of versions. + + ## Examples + + iex> list_versions() + [%Version{}, ...] + + """ + def list_versions do + Repo.all(Version) + end + + @doc """ + Gets a single version. + + Raises `Ecto.NoResultsError` if the Version does not exist. + + ## Examples + + iex> get_version!(123) + %Version{} + + iex> get_version!(456) + ** (Ecto.NoResultsError) + + """ + def get_version!(id), do: Repo.get!(Version, id) + + @doc """ + Creates a version. + + ## Examples + + iex> create_version(%{field: value}) + {:ok, %Version{}} + + iex> create_version(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_version(attrs \\ %{}) do + %Version{} + |> Version.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a version. + + ## Examples + + iex> update_version(version, %{field: new_value}) + {:ok, %Version{}} + + iex> update_version(version, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_version(%Version{} = version, attrs) do + version + |> Version.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Version. + + ## Examples + + iex> delete_version(version) + {:ok, %Version{}} + + iex> delete_version(version) + {:error, %Ecto.Changeset{}} + + """ + def delete_version(%Version{} = version) do + Repo.delete(version) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking version changes. + + ## Examples + + iex> change_version(version) + %Ecto.Changeset{source: %Version{}} + + """ + def change_version(%Version{} = version) do + Version.changeset(version, %{}) + end +end diff --git a/lib/philomena/versions/version.ex b/lib/philomena/versions/version.ex new file mode 100644 index 00000000..a5e6988e --- /dev/null +++ b/lib/philomena/versions/version.ex @@ -0,0 +1,23 @@ +defmodule Philomena.Versions.Version do + use Ecto.Schema + import Ecto.Changeset + + schema "versions" do + field :event, :string + field :whodunnit, :string + field :object, :string + + # fixme: rails polymorphic relation + field :item_id, :integer + field :item_type, :string + + timestamps(inserted_at: :created_at, updated_at: false) + end + + @doc false + def changeset(version, attrs) do + version + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena/vpns.ex b/lib/philomena/vpns.ex new file mode 100644 index 00000000..b24254ec --- /dev/null +++ b/lib/philomena/vpns.ex @@ -0,0 +1,104 @@ +defmodule Philomena.Vpns do + @moduledoc """ + The Vpns context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Vpns.Vpn + + @doc """ + Returns the list of vpns. + + ## Examples + + iex> list_vpns() + [%Vpn{}, ...] + + """ + def list_vpns do + Repo.all(Vpn) + end + + @doc """ + Gets a single vpn. + + Raises `Ecto.NoResultsError` if the Vpn does not exist. + + ## Examples + + iex> get_vpn!(123) + %Vpn{} + + iex> get_vpn!(456) + ** (Ecto.NoResultsError) + + """ + def get_vpn!(id), do: Repo.get!(Vpn, id) + + @doc """ + Creates a vpn. + + ## Examples + + iex> create_vpn(%{field: value}) + {:ok, %Vpn{}} + + iex> create_vpn(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_vpn(attrs \\ %{}) do + %Vpn{} + |> Vpn.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a vpn. + + ## Examples + + iex> update_vpn(vpn, %{field: new_value}) + {:ok, %Vpn{}} + + iex> update_vpn(vpn, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_vpn(%Vpn{} = vpn, attrs) do + vpn + |> Vpn.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Vpn. + + ## Examples + + iex> delete_vpn(vpn) + {:ok, %Vpn{}} + + iex> delete_vpn(vpn) + {:error, %Ecto.Changeset{}} + + """ + def delete_vpn(%Vpn{} = vpn) do + Repo.delete(vpn) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking vpn changes. + + ## Examples + + iex> change_vpn(vpn) + %Ecto.Changeset{source: %Vpn{}} + + """ + def change_vpn(%Vpn{} = vpn) do + Vpn.changeset(vpn, %{}) + end +end diff --git a/lib/philomena/vpns/vpn.ex b/lib/philomena/vpns/vpn.ex new file mode 100644 index 00000000..6ce9fdd9 --- /dev/null +++ b/lib/philomena/vpns/vpn.ex @@ -0,0 +1,17 @@ +defmodule Philomena.Vpns.Vpn do + use Ecto.Schema + import Ecto.Changeset + + @primary_key false + + schema "vpns" do + field :ip, EctoNetwork.INET + end + + @doc false + def changeset(vpn, attrs) do + vpn + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/versions_test.exs b/test/philomena/versions_test.exs new file mode 100644 index 00000000..6432b7c8 --- /dev/null +++ b/test/philomena/versions_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.VersionsTest do + use Philomena.DataCase + + alias Philomena.Versions + + describe "versions" do + alias Philomena.Versions.Version + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def version_fixture(attrs \\ %{}) do + {:ok, version} = + attrs + |> Enum.into(@valid_attrs) + |> Versions.create_version() + + version + end + + test "list_versions/0 returns all versions" do + version = version_fixture() + assert Versions.list_versions() == [version] + end + + test "get_version!/1 returns the version with given id" do + version = version_fixture() + assert Versions.get_version!(version.id) == version + end + + test "create_version/1 with valid data creates a version" do + assert {:ok, %Version{} = version} = Versions.create_version(@valid_attrs) + end + + test "create_version/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Versions.create_version(@invalid_attrs) + end + + test "update_version/2 with valid data updates the version" do + version = version_fixture() + assert {:ok, %Version{} = version} = Versions.update_version(version, @update_attrs) + end + + test "update_version/2 with invalid data returns error changeset" do + version = version_fixture() + assert {:error, %Ecto.Changeset{}} = Versions.update_version(version, @invalid_attrs) + assert version == Versions.get_version!(version.id) + end + + test "delete_version/1 deletes the version" do + version = version_fixture() + assert {:ok, %Version{}} = Versions.delete_version(version) + assert_raise Ecto.NoResultsError, fn -> Versions.get_version!(version.id) end + end + + test "change_version/1 returns a version changeset" do + version = version_fixture() + assert %Ecto.Changeset{} = Versions.change_version(version) + end + end +end diff --git a/test/philomena/vpns_test.exs b/test/philomena/vpns_test.exs new file mode 100644 index 00000000..7a76b2d4 --- /dev/null +++ b/test/philomena/vpns_test.exs @@ -0,0 +1,62 @@ +defmodule Philomena.VpnsTest do + use Philomena.DataCase + + alias Philomena.Vpns + + describe "vpns" do + alias Philomena.Vpns.Vpn + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def vpn_fixture(attrs \\ %{}) do + {:ok, vpn} = + attrs + |> Enum.into(@valid_attrs) + |> Vpns.create_vpn() + + vpn + end + + test "list_vpns/0 returns all vpns" do + vpn = vpn_fixture() + assert Vpns.list_vpns() == [vpn] + end + + test "get_vpn!/1 returns the vpn with given id" do + vpn = vpn_fixture() + assert Vpns.get_vpn!(vpn.id) == vpn + end + + test "create_vpn/1 with valid data creates a vpn" do + assert {:ok, %Vpn{} = vpn} = Vpns.create_vpn(@valid_attrs) + end + + test "create_vpn/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Vpns.create_vpn(@invalid_attrs) + end + + test "update_vpn/2 with valid data updates the vpn" do + vpn = vpn_fixture() + assert {:ok, %Vpn{} = vpn} = Vpns.update_vpn(vpn, @update_attrs) + end + + test "update_vpn/2 with invalid data returns error changeset" do + vpn = vpn_fixture() + assert {:error, %Ecto.Changeset{}} = Vpns.update_vpn(vpn, @invalid_attrs) + assert vpn == Vpns.get_vpn!(vpn.id) + end + + test "delete_vpn/1 deletes the vpn" do + vpn = vpn_fixture() + assert {:ok, %Vpn{}} = Vpns.delete_vpn(vpn) + assert_raise Ecto.NoResultsError, fn -> Vpns.get_vpn!(vpn.id) end + end + + test "change_vpn/1 returns a vpn changeset" do + vpn = vpn_fixture() + assert %Ecto.Changeset{} = Vpns.change_vpn(vpn) + end + end +end