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. Init project.
Added the Links model. Added the Links model.
Added README and CHANGELOG. 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 schema "links" do
field :name, :string field :name, :string
field :url, :string field :url, :string
field :shorten, :string
timestamps() timestamps()
end end
@@ -15,8 +16,9 @@ defmodule LinkShortener.Links do
@doc false @doc false
def changeset(link, attrs) do def changeset(link, attrs) do
link link
|> cast(attrs, [:name, :url]) |> cast(attrs, [:name, :url, :shorten])
|> validate_required([:name, :url]) |> validate_required([:url, :shorten])
|> unique_constraint(:shorten)
end end
def new_one(), do: Links.changeset(%Links{}) def new_one(), do: Links.changeset(%Links{})
@@ -5,8 +5,11 @@ defmodule LinkShortener.Repo.Migrations.CreateLinks do
create table(:links) do create table(:links) do
add :name, :string add :name, :string
add :url, :string add :url, :string
add :shorten, :string
timestamps() timestamps()
end end
create unique_index(:links, [:shorten])
end end
end end