Update the Links model to have a unique shorten.

This commit is contained in:
2023-07-06 19:59:30 +03:00
parent ebef9bdbaa
commit c17b323566
3 changed files with 9 additions and 2 deletions
+4 -2
View File
@@ -8,6 +8,7 @@ defmodule LinkShortener.Links do
schema "links" do
field :name, :string
field :url, :string
field :shorten, :string
timestamps()
end
@@ -15,8 +16,9 @@ defmodule LinkShortener.Links do
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [:name, :url])
|> validate_required([:name, :url])
|> cast(attrs, [:name, :url, :shorten])
|> validate_required([:url, :shorten])
|> unique_constraint(:shorten)
end
def new_one(), do: Links.changeset(%Links{})