-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df92024
commit d170685
Showing
8 changed files
with
466 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
priv/repo/migrations/20231110093800_create_menus_table.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
priv/repo/migrations/20231110093800_create_queries_menu_table.exs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.