Browse Source

initial tag functions

pull/26/head
Inhji Y. 1 year ago
parent
commit
7cce434dab
  1. 15
      apps/db/priv/repo/migrations/20200403192337_create_tags.exs
  2. 1
      apps/tags/.formatter.exs
  3. 31
      apps/tags/lib/tags.ex
  4. 9
      apps/tags/lib/tags/slug.ex
  5. 21
      apps/tags/lib/tags/tag.ex
  6. 6
      apps/tags/mix.exs
  7. 51
      apps/tags/test/tags_test.exs
  8. 2
      mix.lock

15
apps/db/priv/repo/migrations/20200403192337_create_tags.exs

@ -0,0 +1,15 @@
defmodule Db.Repo.Migrations.CreateTags do
use Ecto.Migration
def change do
create table(:tags) do
add :name, :string
add :slug, :string
timestamps()
end
create unique_index(:tags, [:name])
create unique_index(:tags, [:slug])
end
end

1
apps/tags/.formatter.exs

@ -1,4 +1,5 @@
# Used by "mix format"
[
import_deps: [:ecto],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

31
apps/tags/lib/tags.ex

@ -3,16 +3,31 @@ defmodule Tags do
Documentation for `Tags`.
"""
@doc """
Hello world.
def add(entity_id, tag) when is_binary(tag) do
end
## Examples
def create_tag(name) do
%Tags.Tag{}
|> Tags.Tag.changeset(%{name: name})
|> Db.Repo.insert()
end
iex> Tags.hello()
:world
def list_tags() do
Tags.Tag
|> Db.Repo.all()
end
"""
def hello do
:world
def create_or_get_tag(tag) do
case get_tag_by_slug(Slugger.slugify_downcase(tag)) do
nil -> create_tag(tag)
tag -> {:ok, tag}
end
end
def get_tag_by_slug!(slug), do: Db.Repo.get_by!(Tags.Tag, slug: slug)
def get_tag_by_slug(slug), do: Db.Repo.get_by(Tags.Tag, slug: slug)
def add_tag_relation(entity_id, topic_id) do
end
end

9
apps/tags/lib/tags/slug.ex

@ -0,0 +1,9 @@
defmodule Tags.Slug do
use EctoAutoslugField.Slug, from: :name, to: :slug
def build_slug(sources, changeset) do
sources
|> super(changeset)
|> String.downcase()
end
end

21
apps/tags/lib/tags/tag.ex

@ -0,0 +1,21 @@
defmodule Tags.Tag do
use Ecto.Schema
import Ecto.Changeset
alias Tags.Slug
schema "tags" do
field :name, :string
field :slug, Slug.Type
timestamps()
end
def changeset(tag, attrs \\ %{}) do
tag
|> cast(attrs, [:name, :slug])
|> validate_required([:name])
|> unique_constraint(:name)
|> Slug.maybe_generate_slug()
|> Slug.unique_constraint()
end
end

6
apps/tags/mix.exs

@ -25,9 +25,9 @@ defmodule Tags.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}
{:ecto_sql, "~> 3.1"},
{:ecto_autoslug_field, "~> 2.0"},
{:db, in_umbrella: true}
]
end
end

51
apps/tags/test/tags_test.exs

@ -1,8 +1,55 @@
defmodule TagsTest do
use ExUnit.Case
doctest Tags
alias Tags.Tag
test "greets the world" do
assert Tags.hello() == :world
@name "Foo Bar"
@name2 "Bar Baz"
@expected_slug "foo-bar"
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Db.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Db.Repo, {:shared, self()})
end
:ok
end
test "create_tag/1 creates a new tag" do
{:ok, tag} = Tags.create_tag(@name)
assert tag.name == @name
assert tag.slug == @expected_slug
end
test "get_tag_by_slug!/1 gets a tag by its slug" do
{:ok, tag} = Tags.create_tag(@name)
new_tag = Tags.get_tag_by_slug!(tag.slug)
assert new_tag.name == tag.name
end
test "get_tag_by_slug/1 gets a tag by its slug" do
{:ok, tag} = Tags.create_tag(@name)
new_tag = Tags.get_tag_by_slug(tag.slug)
assert new_tag.name == tag.name
end
test "list_tags/0 lists all tags" do
{:ok, _first_tag} = Tags.create_tag(@name)
{:ok, _second_tag} = Tags.create_tag(@name2)
assert [%Tag{}, %Tag{}] = Tags.list_tags()
end
test "create_or_get_tag/1 creates an new tag" do
{:ok, tag} = Tags.create_or_get_tag(@name)
assert tag.name == @name
end
test "create_or_get_tag/1 returns an existing tag" do
{:ok, tag} = Tags.create_tag("Foo bar")
{:ok, existing_tag} = Tags.create_or_get_tag("Foo bar")
assert tag.name == existing_tag.name
end
end

2
mix.lock

@ -5,6 +5,7 @@
"db_connection": {:hex, :db_connection, "2.2.1", "caee17725495f5129cb7faebde001dc4406796f12a62b8949f4ac69315080566", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "2b02ece62d9f983fcd40954e443b7d9e6589664380e5546b2b9b523cd0fb59e1"},
"decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"},
"ecto": {:hex, :ecto, "3.4.0", "a7a83ab8359bf816ce729e5e65981ce25b9fc5adfc89c2ea3980f4fed0bfd7c1", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "5eed18252f5b5bbadec56a24112b531343507dbe046273133176b12190ce19cc"},
"ecto_autoslug_field": {:hex, :ecto_autoslug_field, "2.0.1", "2177c1c253f6dd3efd4b56d1cb76104d0a6ef044c6b9a7a0ad6d32665c4111e5", [:mix], [{:ecto, ">= 2.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:slugger, ">= 0.2.0", [hex: :slugger, repo: "hexpm", optional: false]}], "hexpm", "a3cc73211f2e75b89a03332183812ebe1ac08be2e25a1df5aa3d1422f92c45c3"},
"ecto_sql": {:hex, :ecto_sql, "3.4.1", "3c9136ba138f9b74d31286c73c61232a92bd19385f7c5607bdeb3a4587ef91f5", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9b4be0bffe7b0bdf5393defcae52712f248e70cc2bc0e8ab6ddb03be66371516"},
"file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"},
"gettext": {:hex, :gettext, "0.17.4", "f13088e1ec10ce01665cf25f5ff779e7df3f2dc71b37084976cf89d1aa124d5c", [:mix], [], "hexpm", "3c75b5ea8288e2ee7ea503ff9e30dfe4d07ad3c054576a6e60040e79a801e14d"},
@ -21,5 +22,6 @@
"postgrex": {:hex, :postgrex, "0.15.3", "5806baa8a19a68c4d07c7a624ccdb9b57e89cbc573f1b98099e3741214746ae4", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4737ce62a31747b4c63c12b20c62307e51bb4fcd730ca0c32c280991e0606c90"},
"pow": {:hex, :pow, "1.0.19", "e6295de629338661afdc52b3420f1fa37c191d246aef5d844161843fed6fe88b", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3.0 or ~> 1.4.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and <= 3.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 2.0.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "da77fab98e038b39c5360a77dc98e606cdd1446dabdd65a88991e8b23f67a356"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
"slugger": {:hex, :slugger, "0.3.0", "efc667ab99eee19a48913ccf3d038b1fb9f165fa4fbf093be898b8099e61b6ed", [:mix], [], "hexpm", "20d0ded0e712605d1eae6c5b4889581c3460d92623a930ddda91e0e609b5afba"},
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
}
Loading…
Cancel
Save