ammocpr/lib/ammoprices/catalog/product.ex
Graham McIntire 16a28ac01c
Add Target Sports USA scraper, product filtering, and subsonic detection
- Add subsonic boolean field to products with composite filter indexes
- Add TextDetector utility for detecting subsonic/casing from product titles
- Wire TextDetector into SgAmmo (casing + subsonic) and LuckyGunner (subsonic + fallback casing)
- Extend latest_prices_for_caliber with casing, grain_weight, and subsonic filter options
- Add distinct_grain_weights_for_caliber and distinct_casings_for_caliber queries
- Add filter UI with toggleable casing, grain weight, and subsonic buttons
- Add grain weight column and subsonic badge to product table
- Refresh filter options on PubSub price updates for real-time UI
- Implement Target Sports USA scraper with 18 caliber mappings
- Display all prices in dollar format ($0.38 instead of 38¢)
2026-03-11 16:28:04 -05:00

54 lines
1.4 KiB
Elixir

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 :subsonic, :boolean, default: false
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,
:subsonic,
: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