commit a25a4b94b528d7a389d9fcce60052bcdb25b17be
parent 10977de67412392d885a8b2a25f99f4cb444121f
Author: srfsh <dev@srf.sh>
Date: Mon, 22 Aug 2022 22:02:04 +0300
Zenflows{Test,}.VF.Intent: add paging support and small improvements
Diffstat:
4 files changed, 58 insertions(+), 62 deletions(-)
diff --git a/src/zenflows/vf/intent/domain.ex b/src/zenflows/vf/intent/domain.ex
@@ -20,6 +20,7 @@ defmodule Zenflows.VF.Intent.Domain do
alias Ecto.Multi
alias Zenflows.DB.Repo
+alias Zenflows.GQL.Paging
alias Zenflows.VF.{
Action,
Intent,
@@ -31,20 +32,22 @@ alias Zenflows.VF.{
@typep id() :: Zenflows.DB.Schema.id()
@typep params() :: Zenflows.DB.Schema.params()
-@spec one(repo(), id()) :: {:ok, Intent.t()} | {:error, String.t()}
-def one(repo \\ Repo, id) do
- one_by(repo, id: id)
-end
-
-@spec one_by(repo(), map() | Keyword.t())
+@spec one(repo(), id() | map() | Keyword.t())
:: {:ok, Intent.t()} | {:error, String.t()}
-def one_by(repo \\ Repo, clauses) do
+def one(repo \\ Repo, _)
+def one(repo, id) when is_binary(id), do: one(repo, id: id)
+def one(repo, clauses) do
case repo.get_by(Intent, clauses) do
nil -> {:error, "not found"}
found -> {:ok, found}
end
end
+@spec all(Paging.params()) :: Paging.result(Intent.t())
+def all(params) do
+ Paging.page(Intent, params)
+end
+
@spec create(params()) :: {:ok, Intent.t()} | {:error, chgset()}
def create(params) do
Multi.new()
@@ -61,7 +64,7 @@ end
def update(id, params) do
Multi.new()
|> Multi.put(:id, id)
- |> Multi.run(:one, &one_by/2)
+ |> Multi.run(:one, &one/2)
|> Multi.update(:update, &Intent.chgset(&1.one, params))
|> Repo.transaction()
|> case do
@@ -74,7 +77,7 @@ end
def delete(id) do
Multi.new()
|> Multi.put(:id, id)
- |> Multi.run(:one, &one_by/2)
+ |> Multi.run(:one, &one/2)
|> Multi.delete(:delete, &(&1.one))
|> Repo.transaction()
|> case do
diff --git a/src/zenflows/vf/intent/resolv.ex b/src/zenflows/vf/intent/resolv.ex
@@ -20,6 +20,32 @@ defmodule Zenflows.VF.Intent.Resolv do
alias Zenflows.VF.Intent.Domain
+def intent(params, _) do
+ Domain.one(params)
+end
+
+def intents(params, _) do
+ Domain.all(params)
+end
+
+def create_intent(%{intent: params}, _) do
+ with {:ok, int} <- Domain.create(params) do
+ {:ok, %{intent: int}}
+ end
+end
+
+def update_intent(%{intent: %{id: id} = params}, _) do
+ with {:ok, int} <- Domain.update(id, params) do
+ {:ok, %{intent: int}}
+ end
+end
+
+def delete_intent(%{id: id}, _) do
+ with {:ok, _} <- Domain.delete(id) do
+ {:ok, true}
+ end
+end
+
def action(int, _, _) do
int = Domain.preload(int, :action)
{:ok, int.action}
@@ -79,36 +105,4 @@ def published_in(int, _, _) do
int = Domain.preload(int, :published_in)
{:ok, int.published_in}
end
-
-def intent(%{id: id}, _) do
- Domain.one(id)
-end
-
-def intents(_, _) do
- {:ok, %{
- edges: [],
- page_info: %{
- has_previous_page: false,
- has_next_page: false,
- },
- }}
-end
-
-def create_intent(%{intent: params}, _) do
- with {:ok, int} <- Domain.create(params) do
- {:ok, %{intent: int}}
- end
-end
-
-def update_intent(%{intent: %{id: id} = params}, _) do
- with {:ok, int} <- Domain.update(id, params) do
- {:ok, %{intent: int}}
- end
-end
-
-def delete_intent(%{id: id}, _) do
- with {:ok, _} <- Domain.delete(id) do
- {:ok, true}
- end
-end
end
diff --git a/src/zenflows/vf/intent/type.ex b/src/zenflows/vf/intent/type.ex
@@ -175,20 +175,6 @@ object :intent do
resolve: &Resolv.published_in/3
end
-object :intent_response do
- field :intent, non_null(:intent)
-end
-
-object :intent_edge do
- field :cursor, non_null(:string)
- field :node, non_null(:intent)
-end
-
-object :intent_connection do
- field :page_info, non_null(:page_info)
- field :edges, non_null(list_of(non_null(:intent_edge)))
-end
-
input_object :intent_create_params do
@desc @action_id
field :action_id, non_null(:string), name: "action"
@@ -321,6 +307,20 @@ input_object :intent_update_params do
field :agreed_in, :uri
end
+object :intent_response do
+ field :intent, non_null(:intent)
+end
+
+object :intent_edge do
+ field :cursor, non_null(:id)
+ field :node, non_null(:intent)
+end
+
+object :intent_connection do
+ field :page_info, non_null(:page_info)
+ field :edges, non_null(list_of(non_null(:intent_edge)))
+end
+
object :query_intent do
field :intent, :intent do
arg :id, non_null(:id)
@@ -329,9 +329,9 @@ object :query_intent do
field :intents, non_null(:intent_connection) do
arg :first, :integer
- arg :after, :string
+ arg :after, :id
arg :last, :integer
- arg :before, :string
+ arg :before, :id
resolve &Resolv.intents/2
end
end
diff --git a/test/vf/intent/domain.test.exs b/test/vf/intent/domain.test.exs
@@ -67,7 +67,6 @@ setup do
agreed_in: Factory.uniq("uri"),
},
inserted: Factory.insert!(:intent),
- id: Factory.id(),
}
end
@@ -76,8 +75,8 @@ describe "one/1" do
assert {:ok, %Intent{}} = Domain.one(id)
end
- test "with bad id: doesn't find the Intent", %{id: id} do
- assert {:error, "not found"} = Domain.one(id)
+ test "with bad id: doesn't find the Intent" do
+ assert {:error, "not found"} = Domain.one(Factory.id())
end
end
@@ -223,8 +222,8 @@ describe "delete/1" do
assert {:error, "not found"} = Domain.one(id)
end
- test "with bad id: doesn't delete the Intent", %{id: id} do
- assert {:error, "not found"} = Domain.delete(id)
+ test "with bad id: doesn't delete the Intent" do
+ assert {:error, "not found"} = Domain.delete(Factory.id())
end
end