commit ebe81946836318f7a43a74d5b3eb12e98bc17cdf
parent 20c387ab762bd95c391bf1206bfe7ef8b2d8032b
Author: srfsh <dev@srf.sh>
Date: Tue, 16 Aug 2022 19:02:03 +0300
Zenflows: introduce InstVars
Introduce instance-level, global variables.
Diffstat:
7 files changed, 247 insertions(+), 0 deletions(-)
diff --git a/priv/repo/migrations/20220816154732_create_and_fill_zf_inst_vars.exs b/priv/repo/migrations/20220816154732_create_and_fill_zf_inst_vars.exs
@@ -0,0 +1,59 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.DB.Repo.Migrations.Create_and_fill_zf_inst_vars do
+use Ecto.Migration
+
+alias Zenflows.VF.{Unit, ResourceSpecification}
+alias Zenflows.InstVars
+
+def up() do
+ create table("zf_inst_vars", primary_key: false) do
+ add :one_row, :boolean, default: true, primary_key: true
+ add :unit_one_id, references("vf_unit"), null: false
+ add :unit_currency_id, references("vf_unit"), null: false
+ add :spec_project_design_id, references("vf_resource_specification"), null: false
+ add :spec_project_service_id, references("vf_resource_specification"), null: false
+ add :spec_project_product_id, references("vf_resource_specification"), null: false
+ end
+
+ create constraint("zf_inst_vars", :one_row, check: "one_row")
+
+ flush()
+
+ execute(fn ->
+ r = repo()
+ {:ok, unit_one} = Unit.Domain.create(r, %{label: "one", symbol: "#"})
+ {:ok, unit_currency} = Unit.Domain.create(r, %{label: "currency", symbol: "$"})
+ {:ok, spec_design} = ResourceSpecification.Domain.create(r, %{name: "Design", default_unit_of_resource_id: unit_one.id})
+ {:ok, spec_service} = ResourceSpecification.Domain.create(r, %{name: "Service", default_unit_of_resource_id: unit_one.id})
+ {:ok, spec_product} = ResourceSpecification.Domain.create(r, %{name: "Product", default_unit_of_resource_id: unit_one.id})
+
+ r.insert!(%InstVars{
+ unit_one_id: unit_one.id,
+ unit_currency_id: unit_currency.id,
+ spec_project_design_id: spec_design.id,
+ spec_project_service_id: spec_service.id,
+ spec_project_product_id: spec_product.id,
+ })
+ end)
+end
+
+def down() do
+ drop table("zf_inst_vars")
+end
+end
diff --git a/src/zenflows/application.ex b/src/zenflows/application.ex
@@ -25,6 +25,7 @@ def start(_type, _args) do
children = [
Zenflows.DB.Repo,
+ Zenflows.InstVars.Domain,
{Plug.Cowboy, scheme: :http, plug: Zenflows.Web.Router, options: [port: 8000]},
]
diff --git a/src/zenflows/gql/schema.ex b/src/zenflows/gql/schema.ex
@@ -27,6 +27,7 @@ import_types Absinthe.Type.Custom
import_types Zenflows.GQL.Type
import_types Zenflows.SWPass.Type
import_types Zenflows.Keypairoom.Type
+import_types Zenflows.InstVars.Type
import_types VF.TimeUnit.Type
import_types VF.Action.Type
@@ -77,6 +78,7 @@ query do
end
import_fields :query_sw_pass
+ import_fields :query_inst_vars
import_fields :query_unit
import_fields :query_spatial_thing
diff --git a/src/zenflows/inst_vars.ex b/src/zenflows/inst_vars.ex
@@ -0,0 +1,44 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.InstVars do
+@moduledoc false
+# Don't use this module; it's designed to be only used within
+# Zenflows.InstVars.Domain and the migration script.
+
+use Ecto.Schema
+
+alias Zenflows.VF.{Unit, ResourceSpecification}
+
+@type t() :: %__MODULE__{
+ unit_one: Unit,
+ unit_currency: Unit,
+ spec_project_design: ResourceSpecification,
+ spec_project_service: ResourceSpecification,
+ spec_project_product: ResourceSpecification,
+}
+
+@primary_key false
+@foreign_key_type Zenflows.DB.ID
+schema "zf_inst_vars" do
+ belongs_to :unit_one, Unit
+ belongs_to :unit_currency, Unit
+ belongs_to :spec_project_design, ResourceSpecification
+ belongs_to :spec_project_service, ResourceSpecification
+ belongs_to :spec_project_product, ResourceSpecification
+end
+end
diff --git a/src/zenflows/inst_vars/domain.ex b/src/zenflows/inst_vars/domain.ex
@@ -0,0 +1,46 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.InstVars.Domain do
+@moduledoc "Domain logict of instance-level, global variables."
+
+use Agent
+
+alias Zenflows.{DB.Repo, InstVars}
+alias Zenflows.VF.{Unit, ResourceSpecification}
+
+@doc false
+def start_link(_) do
+ [vars] = Repo.all(InstVars)
+ Agent.start_link(fn -> %{
+ units: %{
+ unit_one: %{id: vars.unit_one_id},
+ unit_currency: %{id: vars.unit_currency_id},
+ },
+ specs: %{
+ spec_project_design: %{id: vars.spec_project_design_id},
+ spec_project_service: %{id: vars.spec_project_service_id},
+ spec_project_product: %{id: vars.spec_project_product_id},
+ },
+ } end, name: __MODULE__)
+end
+
+@spec get() :: %{units: [Unit.t()], specs: [ResourceSpecification.t()]}
+def get() do
+ Agent.get(__MODULE__, & &1)
+end
+end
diff --git a/src/zenflows/inst_vars/resolv.ex b/src/zenflows/inst_vars/resolv.ex
@@ -0,0 +1,47 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.InstVars.Resolv do
+@moduledoc "Resolvers of instance-level, global variables."
+
+alias Zenflows.InstVars.Domain
+alias Zenflows.VF.{Unit, ResourceSpecification}
+
+def instance_variables(_, _) do
+ {:ok, Domain.get()}
+end
+
+def unit_one(%{unit_one: %{id: id}}, _, _) do
+ Unit.Domain.one(id)
+end
+
+def unit_currency(%{unit_currency: %{id: id}}, _, _) do
+ Unit.Domain.one(id)
+end
+
+def spec_project_design(%{spec_project_design: %{id: id}}, _, _) do
+ ResourceSpecification.Domain.one(id)
+end
+
+def spec_project_service(%{spec_project_service: %{id: id}}, _, _) do
+ ResourceSpecification.Domain.one(id)
+end
+
+def spec_project_product(%{spec_project_product: %{id: id}}, _, _) do
+ ResourceSpecification.Domain.one(id)
+end
+end
diff --git a/src/zenflows/inst_vars/type.ex b/src/zenflows/inst_vars/type.ex
@@ -0,0 +1,48 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.InstVars.Type do
+@moduledoc "GraphQL types for instance-level, global variables."
+
+use Absinthe.Schema.Notation
+
+alias Zenflows.InstVars.Resolv
+
+object :instance_units do
+ field :unit_one, non_null(:unit), resolve: &Resolv.unit_one/3
+ field :unit_currency, non_null(:unit), resolve: &Resolv.unit_currency/3
+end
+
+object :instance_specs do
+ field :spec_project_design, non_null(:resource_specification),
+ resolve: &Resolv.spec_project_design/3
+ field :spec_project_service, non_null(:resource_specification),
+ resolve: &Resolv.spec_project_service/3
+ field :spec_project_product, non_null(:resource_specification),
+ resolve: &Resolv.spec_project_product/3
+end
+
+object :instance_variables do
+ field :units, non_null(:instance_units)
+ field :specs, non_null(:instance_specs)
+end
+
+object :query_inst_vars do
+ field :instance_variables, non_null(:instance_variables),
+ resolve: &Resolv.instance_variables/2, meta: [only_guest?: true]
+end
+end