Add a method to create an Link using a generator.
This commit is contained in:
@@ -9,4 +9,6 @@
|
|||||||
Renamed the Links.get_one_by_name to get_one_by_shorten.
|
Renamed the Links.get_one_by_name to get_one_by_shorten.
|
||||||
** 0.2.0 <2023-07-07 Fri>
|
** 0.2.0 <2023-07-07 Fri>
|
||||||
Added a generator of a SafeString.
|
Added a generator of a SafeString.
|
||||||
|
** 0.2.1 <2023-07-07 Fri>
|
||||||
|
Added a method to create an Link using a generator.
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ defmodule LinkShortener.Links do
|
|||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
alias LinkShortener.Repo
|
alias LinkShortener.Repo
|
||||||
alias LinkShortener.Links
|
alias LinkShortener.Links
|
||||||
|
alias LinkShortener.Generators.SafeString
|
||||||
|
|
||||||
schema "links" do
|
schema "links" do
|
||||||
field :name, :string
|
field :name, :string
|
||||||
@@ -23,6 +24,11 @@ defmodule LinkShortener.Links do
|
|||||||
|
|
||||||
def new_one(), do: Links.changeset(%Links{})
|
def new_one(), do: Links.changeset(%Links{})
|
||||||
|
|
||||||
|
def create_one(attrs, length \\ 10, generator \\ SafeString) do
|
||||||
|
shorten = generator.generate(length)
|
||||||
|
insert_one(Map.put(attrs, :shorten, shorten))
|
||||||
|
end
|
||||||
|
|
||||||
def insert_one(attrs) do
|
def insert_one(attrs) do
|
||||||
%Links{}
|
%Links{}
|
||||||
|> Links.changeset(attrs)
|
|> Links.changeset(attrs)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ defmodule LinkShortener.MixProject do
|
|||||||
def project do
|
def project do
|
||||||
[
|
[
|
||||||
app: :link_shortener,
|
app: :link_shortener,
|
||||||
version: "0.1.2",
|
version: "0.2.1",
|
||||||
elixir: "~> 1.12",
|
elixir: "~> 1.12",
|
||||||
elixirc_paths: elixirc_paths(Mix.env()),
|
elixirc_paths: elixirc_paths(Mix.env()),
|
||||||
compilers: [] ++ Mix.compilers(),
|
compilers: [] ++ Mix.compilers(),
|
||||||
|
|||||||
Reference in New Issue
Block a user