Commit 8c918d10 authored by Antonis Kalou's avatar Antonis Kalou

Remove do_create/do_update from ResourceSpecification

parent 4f451133
......@@ -180,39 +180,6 @@ defmodule ValueFlows.Knowledge.ResourceSpecification.GraphQL do
})
end
def create_resource_spec(
%{resource_specification: %{in_scope_of: context_ids} = resource_spec_attrs},
info
)
when is_list(context_ids) do
# FIXME: support multiple contexts?
context_id = List.first(context_ids)
create_resource_spec(
%{resource_specification: Map.merge(resource_spec_attrs, %{in_scope_of: context_id})},
info
)
end
def create_resource_spec(
%{resource_specification: %{in_scope_of: context_id} = resource_spec_attrs},
info
)
when not is_nil(context_id) do
Repo.transact_with(fn ->
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, pointer} <- Pointers.one(id: context_id),
context = Pointers.follow!(pointer),
{:ok, uploads} <- UploadResolver.upload(user, resource_spec_attrs, info),
resource_spec_attrs = Map.merge(resource_spec_attrs, uploads),
resource_spec_attrs = Map.merge(resource_spec_attrs, %{is_public: true}),
{:ok, resource_spec} <-
ResourceSpecifications.create(user, context, resource_spec_attrs) do
{:ok, %{resource_specification: resource_spec}}
end
end)
end
# FIXME: duplication!
def create_resource_spec(%{resource_specification: resource_spec_attrs}, info) do
Repo.transact_with(fn ->
......@@ -226,34 +193,13 @@ defmodule ValueFlows.Knowledge.ResourceSpecification.GraphQL do
end)
end
def update_resource_spec(%{resource_specification: %{in_scope_of: context_ids} = changes}, info) do
context_id = List.first(context_ids)
Repo.transact_with(fn ->
do_update(changes, info, fn resource_spec, changes ->
with {:ok, pointer} <- Pointers.one(id: context_id) do
context = Pointers.follow!(pointer)
ResourceSpecifications.update(resource_spec, context, changes)
end
end)
end)
end
def update_resource_spec(%{resource_specification: changes}, info) do
Repo.transact_with(fn ->
do_update(changes, info, fn resource_spec, changes ->
ResourceSpecifications.update(resource_spec, changes)
end)
end)
end
defp do_update(%{id: id} = changes, info, update_fn) do
def update_resource_spec(%{resource_specification: %{id: id} = changes}, info) do
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, resource_spec} <- resource_spec(%{id: id}, info),
:ok <- ensure_update_permission(user, resource_spec),
{:ok, uploads} <- UploadResolver.upload(user, changes, info),
changes = Map.merge(changes, uploads),
{:ok, resource_spec} <- update_fn.(resource_spec, changes) do
{:ok, resource_spec} <- ResourceSpecifications.update(resource_spec, changes) do
{:ok, %{resource_specification: resource_spec}}
end
end
......
......@@ -50,7 +50,7 @@ defmodule ValueFlows.Knowledge.ResourceSpecification do
end
@required ~w(name is_public)a
@cast @required ++ ~w(note is_disabled image_id)a
@cast @required ++ ~w(note is_disabled context_id image_id)a
def create_changeset(
%User{} = creator,
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Knowledge.ResourceSpecification.ResourceSpecifications do
import CommonsPub.Common, only: [maybe_put: 3]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
alias CommonsPub.Contexts
alias CommonsPub.Feeds.FeedActivities
alias CommonsPub.Users.User
# alias CommonsPub.Meta.Pointers
# alias Measurement.Measure
alias ValueFlows.Knowledge.ResourceSpecification
alias ValueFlows.Knowledge.ResourceSpecification.Queries
# alias ValueFlows.Knowledge.Action
# alias ValueFlows.Knowledge.Action.Actions
def cursor(), do: &[&1.id]
def test_cursor(), do: &[&1["id"]]
......@@ -86,26 +84,12 @@ defmodule ValueFlows.Knowledge.ResourceSpecification.ResourceSpecifications do
## mutations
# @spec create(User.t(), Community.t(), attrs :: map) :: {:ok, ResourceSpecification.t()} | {:error, Changeset.t()}
def create(%User{} = creator, %{id: _id} = context, attrs)
when is_map(attrs) do
do_create(creator, attrs, fn ->
ResourceSpecification.create_changeset(creator, context, attrs)
end)
end
# @spec create(User.t(), attrs :: map) :: {:ok, ResourceSpecification.t()} | {:error, Changeset.t()}
@spec create(User.t(), attrs :: map) :: {:ok, ResourceSpecification.t()} | {:error, Changeset.t()}
def create(%User{} = creator, attrs) when is_map(attrs) do
do_create(creator, attrs, fn ->
ResourceSpecification.create_changeset(creator, attrs)
end)
end
def do_create(creator, attrs, changeset_fn) do
Repo.transact_with(fn ->
cs = changeset_fn.()
attrs = prepare_attrs(attrs)
with {:ok, item} <- Repo.insert(cs),
with {:ok, item} <- Repo.insert(ResourceSpecification.create_changeset(creator, attrs)),
{:ok, item} <- ValueFlows.Util.try_tag_thing(creator, item, attrs),
act_attrs = %{verb: "created", is_local: true},
# FIXME
......@@ -166,25 +150,14 @@ defmodule ValueFlows.Knowledge.ResourceSpecification.ResourceSpecifications do
# TODO: take the user who is performing the update
# @spec update(%ResourceSpecification{}, attrs :: map) :: {:ok, ResourceSpecification.t()} | {:error, Changeset.t()}
def update(%ResourceSpecification{} = resource_spec, attrs) do
do_update(resource_spec, attrs, &ResourceSpecification.update_changeset(&1, attrs))
end
def update(%ResourceSpecification{} = resource_spec, %{id: _id} = context, attrs) do
do_update(resource_spec, attrs, &ResourceSpecification.update_changeset(&1, context, attrs))
end
def do_update(resource_spec, attrs, changeset_fn) do
Repo.transact_with(fn ->
resource_spec =
Repo.preload(resource_spec, [
:default_unit_of_effort
])
cs =
resource_spec
|> changeset_fn.()
with {:ok, resource_spec} <- Repo.update(cs),
attrs = prepare_attrs(attrs)
with {:ok, resource_spec} <- Repo.update(ResourceSpecification.update_changeset(resource_spec, attrs)),
{:ok, resource_spec} <- ValueFlows.Util.try_tag_thing(nil, resource_spec, attrs) do
publish(resource_spec, :updated)
{:ok, resource_spec}
......@@ -226,4 +199,11 @@ defmodule ValueFlows.Knowledge.ResourceSpecification.ResourceSpecifications do
:ok
end
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment