Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and Delete a Notebook #6

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/cursif_web/resolvers/notebooks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ defmodule CursifWeb.Resolvers.Notebooks do
end
end

@spec update_notebook(map(), map()) :: {:ok, Notebook.t()} | {:error, atom()}
def update_notebook(%{id: id} = args, _context) do
notebook = Notebooks.get_notebook!(id)

case Notebooks.update_notebook(notebook, args) do
{:ok, notebook} -> {:ok, notebook}
{:error, changeset} -> {:error, changeset}
end
end

@spec delete_notebook(map(), map()) :: {:ok, Notebook.t()} | {:error, atom()}
def delete_notebook(%{id: id}, _context) do
notebook = Notebooks.get_notebook!(id)

case Notebooks.delete_notebook(notebook) do
{:ok, notebook} -> {:ok, notebook}
{:error, changeset} -> {:error, changeset}
end
end

# @spec get_owner(map(), map(), map()) :: {:ok, User.t()}
def get_owner(notebook, _args, _context) do
{:ok, Notebooks.get_owner!(notebook)}
Expand Down
35 changes: 27 additions & 8 deletions lib/cursif_web/schema/notebook_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,13 @@ defmodule CursifWeb.Schema.NotebookTypes do
field :owner_type, :string
field :pages, list_of(:page)
field :collaborators, list_of(:partial_user)
field :macros, list_of(:macro)

field :owner, :owner do
resolve(&Notebooks.get_owner/3)
end
end

@desc "Macro representation"
object :macro do
field :id, :id
field :title, :string
field :pattern, :string
field :code, :string
end

union :owner do
types([:partial_user])

Expand All @@ -37,6 +30,14 @@ defmodule CursifWeb.Schema.NotebookTypes do
end
end

@desc "Macro representation"
object :macro do
field :id, :id
field :title, :string
field :pattern, :string
field :code, :string
end

@desc "Notebook queries"
object :notebook_queries do
@desc "Get the list of notebooks"
Expand Down Expand Up @@ -74,6 +75,24 @@ defmodule CursifWeb.Schema.NotebookTypes do
resolve(&Notebooks.create_notebook/2)
end

@desc "Update a notebook"
field :update_notebook, :notebook do
arg(:id, non_null(:id))
arg(:title, :string)
arg(:description, :string)
arg(:owner_id, :id)
arg(:owner_type, :string)

resolve(&Notebooks.update_notebook/2)
end

@desc "Delete an notebook"
field :delete_notebook, :notebook do
arg(:id, non_null(:id))

resolve(&Notebooks.delete_notebook/2)
end

@desc "Create a macro"
field :create_macro, :macro do
arg(:title, non_null(:string))
Expand Down