defmodule WhoNeedHelp.Catalog.CategoryVote do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "category_votes" do
    belongs_to :proposal, WhoNeedHelp.Catalog.CategoryProposal
    belongs_to :user, WhoNeedHelp.Accounts.User
    timestamps(type: :utc_datetime, updated_at: false)
  end

  def changeset(vote, attrs) do
    vote
    |> cast(attrs, [:proposal_id, :user_id])
    |> validate_required([:proposal_id, :user_id])
    |> unique_constraint([:proposal_id, :user_id])
  end
end
