add models, context and liveviews

This commit is contained in:
2021-09-02 23:31:14 -04:00
committed by oliviasculley
parent feba4e1d14
commit d6ddf2a9bb
52 changed files with 2325 additions and 13 deletions

200
lib/cannery/ammo.ex Normal file
View File

@ -0,0 +1,200 @@
defmodule Cannery.Ammo do
@moduledoc """
The Ammo context.
"""
import Ecto.Query, warn: false
alias Cannery.Repo
alias Cannery.Ammo.AmmoType
@doc """
Returns the list of ammo_types.
## Examples
iex> list_ammo_types()
[%AmmoType{}, ...]
"""
def list_ammo_types do
Repo.all(AmmoType)
end
@doc """
Gets a single ammo_type.
Raises `Ecto.NoResultsError` if the Ammo type does not exist.
## Examples
iex> get_ammo_type!(123)
%AmmoType{}
iex> get_ammo_type!(456)
** (Ecto.NoResultsError)
"""
def get_ammo_type!(id), do: Repo.get!(AmmoType, id)
@doc """
Creates a ammo_type.
## Examples
iex> create_ammo_type(%{field: value})
{:ok, %AmmoType{}}
iex> create_ammo_type(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_ammo_type(attrs \\ %{}) do
%AmmoType{}
|> AmmoType.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a ammo_type.
## Examples
iex> update_ammo_type(ammo_type, %{field: new_value})
{:ok, %AmmoType{}}
iex> update_ammo_type(ammo_type, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_ammo_type(%AmmoType{} = ammo_type, attrs) do
ammo_type
|> AmmoType.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a ammo_type.
## Examples
iex> delete_ammo_type(ammo_type)
{:ok, %AmmoType{}}
iex> delete_ammo_type(ammo_type)
{:error, %Ecto.Changeset{}}
"""
def delete_ammo_type(%AmmoType{} = ammo_type) do
Repo.delete(ammo_type)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking ammo_type changes.
## Examples
iex> change_ammo_type(ammo_type)
%Ecto.Changeset{data: %AmmoType{}}
"""
def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}) do
AmmoType.changeset(ammo_type, attrs)
end
alias Cannery.Ammo.AmmoGroup
@doc """
Returns the list of ammo_groups.
## Examples
iex> list_ammo_groups()
[%AmmoGroup{}, ...]
"""
def list_ammo_groups do
Repo.all(AmmoGroup)
end
@doc """
Gets a single ammo_group.
Raises `Ecto.NoResultsError` if the Ammo group does not exist.
## Examples
iex> get_ammo_group!(123)
%AmmoGroup{}
iex> get_ammo_group!(456)
** (Ecto.NoResultsError)
"""
def get_ammo_group!(id), do: Repo.get!(AmmoGroup, id)
@doc """
Creates a ammo_group.
## Examples
iex> create_ammo_group(%{field: value})
{:ok, %AmmoGroup{}}
iex> create_ammo_group(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_ammo_group(attrs \\ %{}) do
%AmmoGroup{}
|> AmmoGroup.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a ammo_group.
## Examples
iex> update_ammo_group(ammo_group, %{field: new_value})
{:ok, %AmmoGroup{}}
iex> update_ammo_group(ammo_group, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_ammo_group(%AmmoGroup{} = ammo_group, attrs) do
ammo_group
|> AmmoGroup.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a ammo_group.
## Examples
iex> delete_ammo_group(ammo_group)
{:ok, %AmmoGroup{}}
iex> delete_ammo_group(ammo_group)
{:error, %Ecto.Changeset{}}
"""
def delete_ammo_group(%AmmoGroup{} = ammo_group) do
Repo.delete(ammo_group)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking ammo_group changes.
## Examples
iex> change_ammo_group(ammo_group)
%Ecto.Changeset{data: %AmmoGroup{}}
"""
def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}) do
AmmoGroup.changeset(ammo_group, attrs)
end
end

View File

@ -0,0 +1,25 @@
defmodule Cannery.Ammo.AmmoGroup do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "ammo_groups" do
field :count, :integer
field :notes, :string
field :price_paid, :float
field :tag_id, :binary_id
field :ammo_type_id, :binary_id
field :container_id, :binary_id
field :user_id, :binary_id
timestamps()
end
@doc false
def changeset(ammo_group, attrs) do
ammo_group
|> cast(attrs, [:count, :price_paid, :notes])
|> validate_required([:count, :price_paid, :notes])
end
end

View File

@ -0,0 +1,24 @@
defmodule Cannery.Ammo.AmmoType do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "ammo_types" do
field :bullet_type, :string
field :case_material, :string
field :desc, :string
field :manufacturer, :string
field :name, :string
field :weight, :float
timestamps()
end
@doc false
def changeset(ammo_type, attrs) do
ammo_type
|> cast(attrs, [:name, :desc, :case_material, :bullet_type, :weight, :manufacturer])
|> validate_required([:name, :desc, :case_material, :bullet_type, :weight, :manufacturer])
end
end

104
lib/cannery/containers.ex Normal file
View File

@ -0,0 +1,104 @@
defmodule Cannery.Containers do
@moduledoc """
The Containers context.
"""
import Ecto.Query, warn: false
alias Cannery.Repo
alias Cannery.Containers.Container
@doc """
Returns the list of containers.
## Examples
iex> list_containers()
[%Container{}, ...]
"""
def list_containers do
Repo.all(Container)
end
@doc """
Gets a single container.
Raises `Ecto.NoResultsError` if the Container does not exist.
## Examples
iex> get_container!(123)
%Container{}
iex> get_container!(456)
** (Ecto.NoResultsError)
"""
def get_container!(id), do: Repo.get!(Container, id)
@doc """
Creates a container.
## Examples
iex> create_container(%{field: value})
{:ok, %Container{}}
iex> create_container(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_container(attrs \\ %{}) do
%Container{}
|> Container.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a container.
## Examples
iex> update_container(container, %{field: new_value})
{:ok, %Container{}}
iex> update_container(container, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_container(%Container{} = container, attrs) do
container
|> Container.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a container.
## Examples
iex> delete_container(container)
{:ok, %Container{}}
iex> delete_container(container)
{:error, %Ecto.Changeset{}}
"""
def delete_container(%Container{} = container) do
Repo.delete(container)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking container changes.
## Examples
iex> change_container(container)
%Ecto.Changeset{data: %Container{}}
"""
def change_container(%Container{} = container, attrs \\ %{}) do
Container.changeset(container, attrs)
end
end

View File

@ -0,0 +1,23 @@
defmodule Cannery.Containers.Container do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "containers" do
field :desc, :string
field :location, :string
field :name, :string
field :type, :string
field :user_id, :binary_id
timestamps()
end
@doc false
def changeset(container, attrs) do
container
|> cast(attrs, [:name, :desc, :type, :location])
|> validate_required([:name, :desc, :type, :location])
end
end

104
lib/cannery/tags.ex Normal file
View File

@ -0,0 +1,104 @@
defmodule Cannery.Tags do
@moduledoc """
The Tags context.
"""
import Ecto.Query, warn: false
alias Cannery.Repo
alias Cannery.Tags.Tag
@doc """
Returns the list of tags.
## Examples
iex> list_tags()
[%Tag{}, ...]
"""
def list_tags do
Repo.all(Tag)
end
@doc """
Gets a single tag.
Raises `Ecto.NoResultsError` if the Tag does not exist.
## Examples
iex> get_tag!(123)
%Tag{}
iex> get_tag!(456)
** (Ecto.NoResultsError)
"""
def get_tag!(id), do: Repo.get!(Tag, id)
@doc """
Creates a tag.
## Examples
iex> create_tag(%{field: value})
{:ok, %Tag{}}
iex> create_tag(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_tag(attrs \\ %{}) do
%Tag{}
|> Tag.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a tag.
## Examples
iex> update_tag(tag, %{field: new_value})
{:ok, %Tag{}}
iex> update_tag(tag, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_tag(%Tag{} = tag, attrs) do
tag
|> Tag.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a tag.
## Examples
iex> delete_tag(tag)
{:ok, %Tag{}}
iex> delete_tag(tag)
{:error, %Ecto.Changeset{}}
"""
def delete_tag(%Tag{} = tag) do
Repo.delete(tag)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking tag changes.
## Examples
iex> change_tag(tag)
%Ecto.Changeset{data: %Tag{}}
"""
def change_tag(%Tag{} = tag, attrs \\ %{}) do
Tag.changeset(tag, attrs)
end
end

22
lib/cannery/tags/tag.ex Normal file
View File

@ -0,0 +1,22 @@
defmodule Cannery.Tags.Tag do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "tags" do
field :"bg-color", :string
field :name, :string
field :"text-color", :string
field :user_id, :binary_id
timestamps()
end
@doc false
def changeset(tag, attrs) do
tag
|> cast(attrs, [:name, :"bg-color", :"text-color"])
|> validate_required([:name, :"bg-color", :"text-color"])
end
end