Update the Links model to have a unique shorten.

main
KKlochko 2 years ago
parent ebef9bdbaa
commit c17b323566

@ -3,4 +3,6 @@
Init project.
Added the Links model.
Added README and CHANGELOG.
** 0.1.1 <2023-07-06 Thu>
Updated the Links model to have a unique shorten.

@ -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{})

@ -5,8 +5,11 @@ defmodule LinkShortener.Repo.Migrations.CreateLinks do
create table(:links) do
add :name, :string
add :url, :string
add :shorten, :string
timestamps()
end
create unique_index(:links, [:shorten])
end
end

Loading…
Cancel
Save