zoeys.computer/lib/zoeyscomputer/gists/gist.ex

35 lines
806 B
Elixir
Raw Normal View History

2024-10-26 15:01:33 -04:00
defmodule Zoeyscomputer.Gists.Gist do
2024-10-26 21:41:22 -04:00
alias Zoeyscomputer.IdGenerator
2024-10-26 15:01:33 -04:00
use Ecto.Schema
import Ecto.Changeset
2024-10-26 21:41:22 -04:00
alias Zoeyscomputer.Users.User
2024-10-26 15:01:33 -04:00
2024-10-26 21:41:22 -04:00
@primary_key {:id, :string, autogenerate: false}
2024-10-26 15:01:33 -04:00
schema "gists" do
field :code, :string
field :lang, :string
2024-10-26 21:41:22 -04:00
field :title, :string
field :desc, :string
belongs_to :author, User
2024-10-26 15:01:33 -04:00
timestamps(type: :utc_datetime)
end
@doc false
def changeset(gist, attrs) do
gist
2024-10-26 22:38:41 -04:00
|> cast(attrs, [:code, :lang, :title, :desc, :author_id])
|> validate_required([:code, :lang, :title, :author_id])
2024-10-26 21:41:22 -04:00
|> put_new_id()
end
defp put_new_id(changeset) do
2024-10-26 22:38:41 -04:00
IO.puts("assigning new id")
2024-10-26 21:41:22 -04:00
case get_field(changeset, :id) do
nil -> put_change(changeset, :id, IdGenerator.generate(7))
_id -> changeset
end
2024-10-26 15:01:33 -04:00
end
end