defmodule Wild.BookReads.BookRead do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  schema "book_reads" do
    field :progress, :integer
    field :rating, :integer
    field :thoughts, :string
    field :read_start, :utc_datetime
    field :read_finish, :utc_datetime

    belongs_to :user, Wild.Accounts.User, type: :id, primary_key: true
    belongs_to :book, Wild.Books.Book, type: :id, primary_key: true

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(book_read, attrs) do
    book_read
    |> cast(attrs, [:rating, :thoughts, :read_start, :read_finish, :progress])
    |> validate_required([:rating, :thoughts, :read_start, :read_finish, :progress])
  end
end
