Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Nov 14, 2023
1 parent df92024 commit d170685
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 47 deletions.
26 changes: 8 additions & 18 deletions lib/sanbase/queries/menu/menu.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ defmodule Sanbase.Queries.Menu do
import Ecto.Query
import Ecto.Changeset

alias __MODULE__.MenuItem
alias __MODULE__, as: Menu

schema "queries_menus" do
schema "menus" do
field(:name, :string)
field(:description, :string)
field(:order, :integer)

belongs_to(:parent, Menu, foreign_key: :parent_id)
belongs_to(:root_parentx, Menu, foreign_key: :root_parent_id)
has_many(:submenus, Menu, foreign_key: :submenu_id)
has_many(:menu_items, MenuItem)

belongs_to(:query, Sanbase.Query)
belongs_to(:dashboard, Sanbase.Dashboard)
# Build a hierarchy of menus.
belongs_to(:parent, Menu, foreign_key: :parent_id)
belongs_to(:root_parent, Menu, foreign_key: :root_parent_id)

timestamps()
end
Expand All @@ -26,20 +25,15 @@ defmodule Sanbase.Queries.Menu do
|> cast(attrs, [
:name,
:description,
:order,
:parent_id,
:root_parent_id,
:query_id,
:dashboard_id,
:submenu_id
:root_parent_id
])
|> validate_required([:name])
end

def create(attrs \\ %{}) do
%Menu{}
|> changeset(attrs)
|> Repo.insert()
end

def update(menu, attrs) do
Expand All @@ -53,10 +47,6 @@ defmodule Sanbase.Queries.Menu do
end

def get(id) do
Repo.get(Menu, id)
end

def list do
Repo.all(Menu)
from(m in Menu, where: m.id == ^id)
end
end
56 changes: 56 additions & 0 deletions lib/sanbase/queries/menu/menu_item.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule Sanbase.Queries.Menu.MenuItem do
use Ecto.Schema

import Ecto.Query
import Ecto.Changeset

alias Sanbase.Queries.Menu
alias Sanbase.Queries.Query
alias Sanbase.Queries.Dashboard

schema "menu_items" do
belongs_to(:menu, Menu, foreign_key: :menu_id)
belongs_to(:root_menu, Menu, foreign_key: :root_menu_id)

belongs_to(:query, Query, foreign_key: :query_id)
belongs_to(:dashboard, Dashboard, foreign_key: :dashboard_id)

field(:position, :integer)

timestamps()
end

def changeset(menu, attrs \\ %{}) do
menu
|> cast(attrs, [
:menu_id,
:query_id,
:dashboard_id,
:position
])
end

def create(attrs \\ %{}) do
%__MODULE__{}
|> changeset(attrs)
|> Repo.insert()
end

def update(menu, attrs) do
menu
|> changeset(attrs)
|> Repo.update()
end

def delete(menu) do
Repo.delete(menu)
end

def get(id) do
Repo.get(Menu, id)
end

def list do
Repo.all(Menu)
end
end
25 changes: 25 additions & 0 deletions lib/sanbase/queries/menus.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Sanbase.Menus do
alias Sanbase.Queries.Menu
alias Sanbase.Queries.Menu.MenuItem

def create_menu(args) do
Menu.create(args)
|> Sanbase.Repo.insert()
end

def add_menu_item(menu_id, item_args) do
with {:ok, root_parent_id} <- get_root_parent_id(menu_id),
{:ok, position} <- MenuItem.get_next_position(root_parent_id) do
MenuItem.create(%{menu_id: menu_id, root_menu_id: root_parent_id} ++ item_args)
|> Sanbase.Repo.insert()
end

MenuItem.create(%{menu_id: menu_id} ++ item_args)
end

defp get_root_parent_id(menu_id) do
query = Menu.get(menu_id) |> select(:root_parent_id)

{:ok, root_parent_id} = Sanbase.Repo.one(query)
end
end
110 changes: 110 additions & 0 deletions lib/sanbase_web/graphql/schema/queries/menu_queries.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
defmodule SanbaseWeb.Graphql.Schema.MenuQueriesQueries do
@moduledoc ~s"""
Queries and mutations for working with short urls
"""
use Absinthe.Schema.Notation

alias SanbaseWeb.Graphql.Resolvers.MenuResolver
alias SanbaseWeb.Graphql.Middlewares.JWTAuth

object :menu_queries do
field :get_menu, :string do
meta(access: :free)
arg(:id, non_null(:integer))

middleware(JWTAuth)

resolve(&MenuResolver.get_menu/3)
end
end

object :menu_mutations do
field :create_menu, :string do
arg(:params, :menu_create_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.create_menu/3)
end

field :update_menu, :string do
arg(:id, non_null(:id))
arg(:params, :menu_update_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.update_menu/3)
end

field :delete_menu, :string do
arg(:id, non_null(:string))

middleware(JWTAuth)

resolve(&MenuResolver.delete_menu/3)
end

field :add_menu_item, :string do
arg(:menu_id, non_null(:string))
arg(:item_params, :menu_item_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.add_menu_item/3)
end

field :create_menu_question, :string do
arg(:menu_uuid, non_null(:string))

arg(:params, :menu_question_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.create_question/3)
end

field :update_menu_question, :menu do
arg(:question_uuid, non_null(:string))
arg(:params, :menu_question_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.update_question/3)
end

field :delete_menu_question, :menu do
arg(:question_uuid, non_null(:string))

middleware(JWTAuth)

resolve(&MenuResolver.delete_question/3)
end

field :create_menu_answer, :menu_answer do
arg(:question_uuid, non_null(:string))
arg(:params, :menu_answer_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.create_answer/3)
end

field :update_menu_answer, :menu_answer do
arg(:answer_uuid, non_null(:string))
arg(:params, :menu_answer_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.update_answer/3)
end

field :delete_menu_answer, :menu_answer do
arg(:answer_uuid, non_null(:string))
arg(:params, :menu_answer_params_input_object)

middleware(JWTAuth)

resolve(&MenuResolver.delete_answer/3)
end
end
end
27 changes: 27 additions & 0 deletions lib/sanbase_web/graphql/schema/types/menu_types.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule SanbaseWeb.Graphql.MenuTypes do
use Absinthe.Schema.Notation

input_object :menu_create_params_input_object do
field(:name, non_null(:string))
field(:description, :string)

@desc ~s"""
If the menu is a sub-menu, this field should be set to the parent menu's id.
If this field is not set or explicitly set to null, the menu will be created as
a top-level menu
"""
field(:parent_id, :integer, default_value: nil)
end

input_object :menu_update_params_input_object do
field(:name, :string)
field(:description, :string)

@desc ~s"""
If the menu is a sub-menu, this field should be set to the parent menu's id.
If this field is not set or explicitly set to null, the menu will be created as
a top-level menu
"""
field(:parent_id, :integer, default_value: nil)
end
end
51 changes: 51 additions & 0 deletions priv/repo/migrations/20231110093800_create_menus_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Sanbase.Repo.Migrations.CreatesMenusTable do
use Ecto.Migration

def change do
create table(:menus) do
add(:name, :string)
add(:description, :string)

add(:parent_id, references(:menus, on_delete: :delete_all))
add(:root_parent_id, references(:menus, on_delete: :delete_all))

add(:position, :integer)

add(:user_id, references(:users, on_delete: :delete_all))

add(:is_admin_controlled, :boolean, default: false)

timestamps()
end

# When searching for all sub-menus of a menu, the `where` clause will
# find these submenus by the root_parent_id, hence the index. The
# parent_id is used only to build the hierarchy after that.
create(index(:menus, [:root_parent_id]))
create(index(:menus, [:user_id]))

create table(:menu_items) do
add(:menu_id, references(:menus, on_delete: :delete_all))
add(:root_menu_id, references(:menus, on_delete: :delete_all))

add(:query_id, references(:queries, on_delete: :delete_all))
add(:dashboard_id, references(:dashboards, on_delete: :delete_all))

add(:position, :integer)

timestamps()
end

# When searching for all menu items of a menu, the `where` clause will
# find these items by the root_menu_id, hence the index. The
# menu_id is used only to build the hierarchy after that.
create(index(:menu_items, [:root_menu_id]))

fk_check = """
(CASE WHEN query_id IS NULL THEN 0 ELSE 1 END) +
(CASE WHEN dashboard_id IS NULL THEN 0 ELSE 1 END) = 1
"""

create(constraint(:menu_items, :only_one_fk, check: fk_check))
end
end
29 changes: 0 additions & 29 deletions priv/repo/migrations/20231110093800_create_queries_menu_table.exs

This file was deleted.

Loading

0 comments on commit d170685

Please sign in to comment.