ratemyclient/lib/my_first_elixir_vibe_code/reviews/review.ex

74 lines
1.8 KiB
Elixir
Raw Normal View History

defmodule MyFirstElixirVibeCode.Reviews.Review do
use Ecto.Schema
import Ecto.Changeset
schema "reviews" do
field :rating, :integer
field :title, :string
field :content, :string
field :project_type, :string
field :client_first_name, :string
field :client_last_name, :string
field :client_street_address, :string
field :client_city, :string
field :client_state, :string
field :client_zip, :string
field :client_country, :string
field :municipal_registration_proof, :string
field :work_permit_proof, :string
field :contractor_id, :id
field :client_id, :id
timestamps(type: :utc_datetime)
end
@doc false
def changeset(review, attrs) do
review
|> cast(attrs, [
:rating,
:title,
:content,
:project_type,
:client_first_name,
:client_last_name,
:client_street_address,
:client_city,
:client_state,
:client_zip,
:client_country,
:municipal_registration_proof,
:work_permit_proof
])
|> validate_required([
:rating,
:title,
:content,
:project_type,
:client_first_name,
:client_last_name,
:client_street_address,
:client_city,
:client_state,
:client_zip
])
|> validate_number(:rating, greater_than_or_equal_to: 1, less_than_or_equal_to: 5)
|> validate_at_least_one_document()
end
defp validate_at_least_one_document(changeset) do
municipal_proof = get_field(changeset, :municipal_registration_proof)
work_permit = get_field(changeset, :work_permit_proof)
if is_nil(municipal_proof) && is_nil(work_permit) do
add_error(
changeset,
:municipal_registration_proof,
"You must provide either municipal registration or work permit proof"
)
else
changeset
end
end
end