Phoenix 1.8 app that scrapes ammunition retailers (Lucky Gunner, SGAmmo) for price data and displays historical price trends. - Data model: retailers, calibers, products, price snapshots - Scraper infrastructure with Req, Floki, realistic browser headers - Oban-scheduled scrape jobs (every 4h with randomized delays) - LiveView pages: homepage with category cards, caliber detail with price table, Chart.js price history, and price stats banner - 18 seeded calibers across handgun/rifle/rimfire/shotgun categories - 77 tests
52 lines
1.4 KiB
Elixir
52 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 :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
|