Added mutations #162

This commit is contained in:
Sven Heidemann 2023-02-08 17:48:54 +01:00
parent 23e238b7d5
commit af8c2dea60
11 changed files with 209 additions and 7 deletions

View File

@ -37,10 +37,18 @@ class AutoRole(TableABC):
def discord_channel_id(self) -> int:
return self._discord_channel_id
@discord_channel_id.setter
def discord_channel_id(self, value: int):
self._discord_channel_id = value
@property
def discord_message_id(self) -> int:
return self._discord_message_id
@discord_message_id.setter
def discord_message_id(self, value: int):
self._discord_message_id = value
@staticmethod
def get_select_all_string() -> str:
return str(

View File

@ -36,10 +36,18 @@ class AutoRoleRule(TableABC):
def emoji_name(self) -> str:
return self._discord_emoji_name
@emoji_name.setter
def emoji_name(self, value: str):
self._discord_emoji_name = value
@property
def role_id(self) -> int:
return self._discord_role_id
@role_id.setter
def role_id(self, value: int):
self._discord_role_id = value
@staticmethod
def get_select_all_string() -> str:
return str(

View File

@ -20,3 +20,16 @@ input AutoRoleFilter {
message_id: String
server: ServerFilter
}
type AutoRoleMutation {
create_auto_role(input: AutoRoleInput!): AutoRole
update_auto_role(input: AutoRoleInput!): AutoRole
delete_auto_role(id: ID): AutoRole
}
input AutoRoleInput {
id: ID!
channel_id: String!
message_id: String!
server_id: ID!
}

View File

@ -17,3 +17,16 @@ input AutoRoleRuleFilter {
role_name: String
auto_role: AutoRoleFilter
}
type AutoRoleRuleMutation {
create_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule
update_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule
delete_auto_role_rule(id: ID): AutoRoleRule
}
input AutoRoleRuleInput {
id: ID!
emoji_name: String!
role_id: String!
auto_role_id: ID!
}

View File

@ -24,6 +24,7 @@ type LevelMutation {
}
input LevelInput {
id: ID
name: String!
color: String!
min_xp: Int!

View File

@ -1,3 +1,6 @@
type Mutation {
auto_role: AutoRoleMutation
auto_role_rule: AutoRoleRuleMutation
level: LevelMutation
user: UserMutation
}

View File

@ -27,3 +27,12 @@ input UserFilter {
level: LevelFilter
server: ServerFilter
}
type UserMutation {
update_user(input: UserInput!): User
}
input UserInput {
id: ID!
xp: Int!
}

View File

@ -0,0 +1,57 @@
from cpl_core.database.context import DatabaseContextABC
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.auto_role import AutoRole
from bot_graphql.abc.query_abc import QueryABC
class AutoRoleMutation(QueryABC):
def __init__(
self,
servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC,
db: DatabaseContextABC,
):
QueryABC.__init__(self, "AutoRoleMutation")
self._servers = servers
self._auto_roles = auto_roles
self._db = db
self.set_field("create_auto_role", self.resolve_create_auto_role)
self.set_field("update_auto_role", self.resolve_update_auto_role)
self.set_field("delete_auto_role", self.resolve_delete_auto_role)
def resolve_create_auto_role(self, *_, input: dict):
auto_role = AutoRole(
self._servers.get_server_by_id(input["server_id"]), input["channel_id"], input["message_id"]
)
self._auto_roles.add_auto_role(auto_role)
self._db.save_changes()
def get_new(x: AutoRole):
return (
x.server.server_id == input["server_id"]
and x.discord_channel_id == input["channel_id"]
and x.discord_message_id == input["message_id"]
)
return self._auto_roles.get_auto_roles_by_server_id(auto_role.server.server_id).where(get_new)
def resolve_update_auto_role(self, *_, input: dict):
auto_role = self._auto_roles.get_auto_role_by_id(input["id"])
auto_role.discord_channel_id = input["channel_id"] if "channel_id" in input else auto_role.discord_channel_id
auto_role.discord_message_id = input["message_id"] if "message_id" in input else auto_role.discord_message_id
self._auto_roles.update_auto_role(auto_role)
self._db.save_changes()
auto_role = self._auto_roles.get_auto_role_by_id(input["id"])
return auto_role
def resolve_delete_auto_role(self, *_, id: int):
auto_role = self._auto_roles.get_auto_role_by_id(id)
self._auto_roles.delete_auto_role(auto_role)
self._db.save_changes()
return auto_role

View File

@ -0,0 +1,59 @@
from cpl_core.database.context import DatabaseContextABC
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.auto_role_rule import AutoRoleRule
from bot_graphql.abc.query_abc import QueryABC
class AutoRoleRuleMutation(QueryABC):
def __init__(
self,
servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC,
db: DatabaseContextABC,
):
QueryABC.__init__(self, "AutoRoleRuleMutation")
self._servers = servers
self._auto_roles = auto_roles
self._db = db
self.set_field("create_auto_role_rule", self.resolve_create_auto_role_rule)
self.set_field("update_auto_role_rule", self.resolve_update_auto_role_rule)
self.set_field("delete_auto_role_rule", self.resolve_delete_auto_role_rule)
def resolve_create_auto_role_rule(self, *_, input: dict):
auto_role_rule = AutoRoleRule(
self._auto_roles.get_auto_role_by_id(input["auto_role_id"]), input["emoji_name"], input["role_id"]
)
self._auto_roles.add_auto_role_rule(auto_role_rule)
self._db.save_changes()
def get_new(x: AutoRoleRule):
return (
x.auto_role.auto_role_id == input["auto_role_id"]
and x.emoji_name == input["emoji_name"]
and x.role_id == input["role_id"]
)
return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.auto_role_id).where(
get_new
)
def resolve_update_auto_role_rule(self, *_, input: dict):
auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"])
auto_role_rule.emoji_name = input["emoji_name"] if "emoji_name" in input else auto_role_rule.emoji_name
auto_role_rule.role_id = input["role_id"] if "role_id" in input else auto_role_rule.role_id
self._auto_roles.update_auto_role_rule(auto_role_rule)
self._db.save_changes()
auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"])
return auto_role_rule
def resolve_delete_auto_role_rule(self, *_, id: int):
auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(id)
self._auto_roles.delete_auto_role_rule(auto_role_rule)
self._db.save_changes()
return auto_role_rule

View File

@ -20,8 +20,8 @@ class LevelMutation(QueryABC):
self._db = db
self.set_field("create_level", self.resolve_create_level)
self.set_field("update_level", self.resolve_create_level)
self.set_field("delete_level", self.resolve_create_level)
self.set_field("update_level", self.resolve_update_level)
self.set_field("delete_level", self.resolve_delete_level)
def resolve_create_level(self, *_, input: dict):
level = Level(
@ -34,7 +34,7 @@ class LevelMutation(QueryABC):
self._levels.add_level(level)
self._db.save_changes()
def get_new_server(l: Level):
def get_new_level(l: Level):
return (
l.name == level.name
and l.color == level.color
@ -42,10 +42,10 @@ class LevelMutation(QueryABC):
and l.permissions == level.permissions
)
return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_server)
return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_level)
def resolve_update_level(self, *_, input):
level = self._levels.get_level_by_id(input.id)
def resolve_update_level(self, *_, input: dict):
level = self._levels.get_level_by_id(input["id"])
level.name = input["name"] if "name" in input else level.name
level.color = input["color"] if "color" in input else level.color
level.min_xp = input["min_xp"] if "min_xp" in input else level.min_xp
@ -54,7 +54,7 @@ class LevelMutation(QueryABC):
self._levels.update_level(level)
self._db.save_changes()
level = self._levels.get_level_by_id(input.id)
level = self._levels.get_level_by_id(input["id"])
return level
def resolve_delete_level(self, *_, id: int):

View File

@ -0,0 +1,31 @@
from cpl_core.database.context import DatabaseContextABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_graphql.abc.query_abc import QueryABC
class UserMutation(QueryABC):
def __init__(
self,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
db: DatabaseContextABC,
):
QueryABC.__init__(self, "UserMutation")
self._servers = servers
self._users = users
self._db = db
self.set_field("update_user", self.resolve_update_user)
def resolve_update_user(self, *_, input: dict):
user = self._users.get_user_by_id(input["id"])
user.xp = input["xp"] if "xp" in input else user.xp
self._users.update_user(user)
self._db.save_changes()
user = self._users.get_user_by_id(input["id"])
return user