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
+2
View File
@@ -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.
+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{})
@@ -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