add error case for create_changeset

This commit is contained in:
shibao 2022-07-04 20:06:31 -04:00
parent 3cbd62e84c
commit 9ebca20dc6

View File

@ -38,7 +38,12 @@ defmodule Cannery.ActivityLog.ShotGroup do
@type id :: UUID.t()
@doc false
@spec create_changeset(new_shot_group(), User.t(), AmmoGroup.t(), attrs :: map()) ::
@spec create_changeset(
new_shot_group(),
User.t() | any(),
AmmoGroup.t() | any(),
attrs :: map()
) ::
Changeset.t(new_shot_group())
def create_changeset(
shot_group,
@ -56,6 +61,14 @@ defmodule Cannery.ActivityLog.ShotGroup do
|> validate_required([:count, :ammo_group_id, :user_id])
end
def create_changeset(shot_group, _invalid_user, _invalid_ammo_group, attrs) do
shot_group
|> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0)
|> validate_required([:count, :ammo_group_id, :user_id])
|> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo group"))
end
defp validate_create_shot_group_count(changeset, %AmmoGroup{count: ammo_group_count}) do
if changeset |> Changeset.get_field(:count) > ammo_group_count do
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)