add and delete tags to/from containers

This commit is contained in:
2022-02-13 21:14:48 -05:00
parent 0d52a943d7
commit df40f03589
12 changed files with 267 additions and 29 deletions

View File

@ -4,7 +4,7 @@ defmodule Cannery.EmailWorker do
"""
use Oban.Worker, queue: :mailers
alias Cannery.{Accounts, Mailer, Email}
alias Cannery.{Accounts, Email, Mailer}
@impl Oban.Worker
def perform(%Oban.Job{args: %{"email" => email, "user_id" => user_id, "attrs" => attrs}}) do

View File

@ -184,8 +184,7 @@ defmodule Cannery.Containers do
Repo.delete_all(
from ct in ContainerTag,
where: ct.container_id == ^container_id,
where: ct.tag_id == ^tag_id,
where: ct.user_id == ^user_id
where: ct.tag_id == ^tag_id
)
if count == 0, do: raise("could not delete container tag"), else: count

View File

@ -6,7 +6,8 @@ defmodule Cannery.Containers.Container do
use Ecto.Schema
import Ecto.Changeset
alias Ecto.{Changeset, UUID}
alias Cannery.{Accounts.User, Ammo.AmmoGroup, Containers.Container}
alias Cannery.Containers.{Container, ContainerTag}
alias Cannery.{Accounts.User, Ammo.AmmoGroup, Tags.Tag}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
@ -19,6 +20,7 @@ defmodule Cannery.Containers.Container do
belongs_to :user, User
has_many :ammo_groups, AmmoGroup
many_to_many :tags, Tag, join_through: ContainerTag
timestamps()
end
@ -32,6 +34,7 @@ defmodule Cannery.Containers.Container do
user: User.t(),
user_id: User.id(),
ammo_groups: [AmmoGroup.t()] | nil,
tags: [Tag.t()] | nil,
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}

View File

@ -4,6 +4,7 @@ defmodule Cannery.Tags do
"""
import Ecto.Query, warn: false
import CanneryWeb.Gettext
alias Cannery.{Accounts.User, Repo, Tags.Tag}
alias Ecto.Changeset
@ -22,6 +23,27 @@ defmodule Cannery.Tags do
@doc """
Gets a single tag.
## Examples
iex> get_tag(123, %User{id: 123})
{:ok, %Tag{}}
iex> get_tag(456, %User{id: 123})
{:error, "tag not found"}
"""
@spec get_tag(Tag.id(), User.t()) :: {:ok, Tag.t()} | {:error, String.t()}
def get_tag(id, %User{id: user_id}) do
Repo.one(from t in Tag, where: t.id == ^id and t.user_id == ^user_id)
|> case do
nil -> {:error, dgettext("errors", "Tag not found")}
tag -> {:ok, tag}
end
end
@doc """
Gets a single tag.
Raises `Ecto.NoResultsError` if the Tag does not exist.
## Examples