diff --git a/lib/app_api/captures/capture.ex b/lib/app_api/captures/capture.ex index 9dc5b25..8129678 100644 --- a/lib/app_api/captures/capture.ex +++ b/lib/app_api/captures/capture.ex @@ -29,12 +29,12 @@ defmodule AppApi.Captures.Capture do defp parse_tags(params) do tags = - (params["tags"] || "") + (params[:tags] || "") |> String.split(",") |> Enum.map(&String.trim/1) |> Enum.reject(&(&1 == "")) - insert_and_get_all(tags, params["id_person"]) + insert_and_get_all(tags, params[:id_person]) end defp insert_and_get_all([], _id_person) do @@ -42,8 +42,12 @@ defmodule AppApi.Captures.Capture do end defp insert_and_get_all(tags, id_person) do - maps = Enum.map(tags, &%{text: &1, id_person: id_person}) - Repo.insert_all(Tag, maps, on_conflict: :nothing) - Repo.all(from t in Tag, where: t.name in ^tags) + Enum.each(tags, fn t -> + attrs = %{text: t, id_person: id_person} + changeset = Tag.changeset(%Tag{}, attrs) + Repo.insert!(changeset, on_conflict: :nothing) + end) + + Repo.all(from t in Tag, where: t.text in ^tags) end end diff --git a/lib/app_api/tags.ex b/lib/app_api/tags.ex index 9299811..53bc4f0 100644 --- a/lib/app_api/tags.ex +++ b/lib/app_api/tags.ex @@ -8,19 +8,6 @@ defmodule AppApi.Tags do alias AppApi.Tags.Tag - @doc """ - Returns the list of tags. - - ## Examples - - iex> list_tags() - [%Tag{}, ...] - - """ - def list_tags do - Repo.all(Tag) - end - @doc """ Returns the list of tags created by a person i.e. default tags diff --git a/mix.lock b/mix.lock index 3b5cb3d..0500962 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,6 @@ %{ "certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"}, + "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, "cors_plug": {:hex, :cors_plug, "2.0.1", "61b9fb38a599d1d4a7f8c7cec9f81ae548890e2f832426cea13897ecbf2f5f8b", [:mix], [{:plug, "~> 1.8", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "571eea27ca6d08d46f44b27ce6ad59d29255111c3cefab833ea55c375db2b4eb"}, "cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"}, @@ -29,5 +30,7 @@ "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"}, "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, + "timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "f354efb2400dd7a80fd9eb6c8419068c4f632da4ac47f3d8822d6e33f08bc852"}, + "tzdata": {:hex, :tzdata, "1.0.3", "73470ad29dde46e350c60a66e6b360d3b99d2d18b74c4c349dbebbc27a09a3eb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a6e1ee7003c4d04ecbd21dd3ec690d4c6662db5d3bbdd7262d53cdf5e7c746c1"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"}, } diff --git a/priv/repo/migrations/20200421121617_remove_timestamp_captures_tags.exs b/priv/repo/migrations/20200421121617_remove_timestamp_captures_tags.exs new file mode 100644 index 0000000..2bf61d2 --- /dev/null +++ b/priv/repo/migrations/20200421121617_remove_timestamp_captures_tags.exs @@ -0,0 +1,10 @@ +defmodule AppApi.Repo.Migrations.RemoveTimestampCapturesTags do + use Ecto.Migration + + def change do + alter table(:captures_tags) do + remove :inserted_at + remove :updated_at + end + end +end diff --git a/test/app_api/captures_test.exs b/test/app_api/captures_test.exs index cbf1c8b..0e8ec32 100644 --- a/test/app_api/captures_test.exs +++ b/test/app_api/captures_test.exs @@ -7,7 +7,7 @@ defmodule AppApi.CapturesTest do alias AppApi.Captures.Capture @valid_attrs %{completed: true, id_person: 42, text: "some text", tags: "tag1, tag2"} - @update_attrs %{completed: false, id_person: 43, text: "some updated text", tags: "" } + @update_attrs %{completed: false, id_person: 43, text: "some updated text", tags: "tag3, tag4" } @invalid_attrs %{completed: nil, id_person: nil, text: nil, tags: ""} def capture_fixture(attrs \\ %{}) do @@ -33,6 +33,11 @@ defmodule AppApi.CapturesTest do assert capture.text == "some text" end + test "get_capture_by_id_person/1 returns the capture with given id person" do + capture = capture_fixture() # id person is 42 + assert Captures.get_capture_by_id_person(42) == [capture] + end + test "create_capture/1 with invalid data returns error changeset" do assert catch_error(Captures.create_capture(@invalid_attrs)) end @@ -61,5 +66,10 @@ defmodule AppApi.CapturesTest do capture = capture_fixture() assert %Ecto.Changeset{} = Captures.change_capture(capture) end + + test "add tag" do + capture = capture_fixture(%{tags: "hello"}) + assert %Capture{} = capture + end end end diff --git a/test/app_api/tags_test.exs b/test/app_api/tags_test.exs new file mode 100644 index 0000000..f228553 --- /dev/null +++ b/test/app_api/tags_test.exs @@ -0,0 +1,39 @@ +defmodule AppApi.TagsTest do + use AppApi.DataCase + + alias AppApi.Tags + + describe "tags" do + alias AppApi.Tags.Tag + + @valid_attrs %{id_person: 42, text: "tag1"} + @invalid_attrs %{id_person: 42, text: ""} + + def tag_fixture(attrs \\ %{}) do + attrs + |> Enum.into(@valid_attrs) + |> Tags.create_tag() + end + + test "get_default_tags/0 returns all tags" do + tag = tag_fixture(%{id_person: nil}) + assert Tags.get_default_tags() == [tag] + end + + test "get_tags_by_id_person/1 returns all tags for a person" do + tag1 = tag_fixture(%{text: "a", id_person: 32}) + tag2 = tag_fixture(%{text: "b", id_person: 32}) + assert Tags.get_tags_by_id_person(32) == [tag1, tag2] # order by tag name + end + + test "create tag with wrong attr return an error" do + assert catch_error(Tags.create_tag(@invalid_attrs)) + end + + test "create_tag/1 with valid data creates a tag" do + assert %Tag{} = tag = Tags.create_tag(@valid_attrs) + assert tag.id_person == 42 + assert tag.text == "tag1" + end + end +end