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
27 lines
837 B
Elixir
27 lines
837 B
Elixir
defmodule Ammoprices.Prices.PriceSnapshot do
|
|
@moduledoc false
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
@foreign_key_type :binary_id
|
|
schema "price_snapshots" do
|
|
belongs_to :product, Ammoprices.Catalog.Product
|
|
|
|
field :price_cents, :integer
|
|
field :price_per_round_cents, :integer
|
|
field :in_stock, :boolean, default: true
|
|
field :recorded_at, :utc_datetime
|
|
|
|
timestamps(type: :utc_datetime, updated_at: false)
|
|
end
|
|
|
|
def changeset(snapshot, attrs) do
|
|
snapshot
|
|
|> cast(attrs, [:price_cents, :price_per_round_cents, :in_stock, :recorded_at])
|
|
|> validate_required([:price_cents, :price_per_round_cents, :recorded_at])
|
|
|> validate_number(:price_cents, greater_than: 0)
|
|
|> validate_number(:price_per_round_cents, greater_than: 0)
|
|
end
|
|
end
|