defmodule Bbox.Parties.Party do
@moduledoc """
Party in the election and haves a list of candidates.
"""
use Ecto.Schema
import Ecto.Changeset
alias Bbox.Candidates.Candidate
@primary_key false
@foreign_key_type :string
schema "parties" do
field(:initials, :string)
field(:name, :string)
field(:description, :string)
has_many(:candidates, Candidate, foreign_key: :party_initials, references: :initials)
timestamps()
end
def changeset(party, params \\ %{}) do
party
|> cast(params, [:initials, :name, :description])
|> validate_required([:initials, :name])
|> validate_length(:initials,
min: 2,
max: 6,
message: "initials must be at least 2 characters and at most 6 characters"
)
|> validate_length(:name, min: 3, message: "name must be at least 3 characters")
|> validate_length(:description, min: 3, message: "description must be at least 3 characters")
|> unique_constraint(:name, message: "name already exists")
end
end