defmodule Ammoprices.Catalog.Product do @moduledoc false use Ecto.Schema import Ecto.Changeset @primary_key {:id, :binary_id, autogenerate: true} @foreign_key_type :binary_id schema "products" do belongs_to :retailer, Ammoprices.Catalog.Retailer belongs_to :caliber, Ammoprices.Catalog.Caliber field :title, :string field :url, :string field :brand, :string field :grain_weight, :integer field :round_count, :integer field :casing, :string field :condition, :string, default: "new" field :upc, :string field :external_id, :string field :in_stock, :boolean, default: true field :last_seen_at, :utc_datetime has_many :price_snapshots, Ammoprices.Prices.PriceSnapshot timestamps(type: :utc_datetime) end def changeset(product, attrs) do product |> cast(attrs, [ :title, :url, :brand, :grain_weight, :round_count, :casing, :condition, :upc, :external_id, :in_stock, :last_seen_at ]) |> validate_required([:title, :url]) |> validate_inclusion(:casing, ~w(brass steel aluminum alloy composite)) |> validate_inclusion(:condition, ~w(new remanufactured surplus)) |> validate_number(:grain_weight, greater_than: 0) |> validate_number(:round_count, greater_than: 0) |> unique_constraint([:retailer_id, :url]) end end