Refaktoring a bugfixing
This commit is contained in:
@@ -1,54 +1,54 @@
|
||||
from typing import List, TYPE_CHECKING
|
||||
from typing import List
|
||||
import strawberry
|
||||
from sqlalchemy import delete
|
||||
from sqlalchemy.dialects.mysql import insert
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from database import models
|
||||
from decorators.endpoints import authenticated_user_only
|
||||
from dependencies.db import get_session
|
||||
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_input
|
||||
from .resolvers.base import get_base_resolver, get_list, get_one
|
||||
from database.transaction import get_session
|
||||
from graphql_schema.entities.resolvers.base import BaseQueryResolver, BaseMutationResolver
|
||||
from graphql_schema.entities.types.mutation_input import CreateOrganizationInput, EditOrganizationInput
|
||||
from graphql_schema.entities.types.types import Organization
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class OrganizationQueries:
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def organizations(root, info) -> List[Organization]:
|
||||
query = get_base_resolver(models.Organization, order_by=[models.Organization.name])
|
||||
return await get_list(models.Organization, query)
|
||||
return await BaseQueryResolver(Organization, models.Organization).get_list(
|
||||
info.context.user_id, order_by=[models.Organization.name]
|
||||
)
|
||||
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def organization(root, info, id: int) -> Organization:
|
||||
query = get_base_resolver(models.Organization, object_id=id)
|
||||
return await get_one(models.Organization, query)
|
||||
return await BaseQueryResolver(Organization, models.Organization).get_one(id, info.context.user_id)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class CreateOrganizationMutation:
|
||||
@strawberry_sqlalchemy_input(model=models.Organization, exclude_fields=["id"])
|
||||
class CreateOrganizationInput:
|
||||
pass
|
||||
class OrganizationMutation:
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def create_organization(root, info, input: CreateOrganizationInput) -> Organization:
|
||||
input_data = input.to_dict()
|
||||
async with get_session() as db:
|
||||
organization = await models.Organization.create(
|
||||
db,
|
||||
data=dict(
|
||||
**input_data,
|
||||
created_by_id=info.context.user_id,
|
||||
)
|
||||
)
|
||||
return await BaseMutationResolver(Organization, models.Organization).create(
|
||||
data=input.to_dict(),
|
||||
user_id=info.context.user_id
|
||||
)
|
||||
|
||||
return Organization(**organization.as_dict())
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def edit_organization(root, info, id: int, input: EditOrganizationInput) -> Organization:
|
||||
async with get_session() as db:
|
||||
organization = (await db.scalars(
|
||||
BaseQueryResolver(Organization, models.Organization).get_query(
|
||||
user_id=info.context.user_id, object_id=id
|
||||
)
|
||||
)).one()
|
||||
|
||||
updated_organization = await models.Organization.update(db, obj=organization, data=input.to_dict())
|
||||
return Organization(**updated_organization.as_dict())
|
||||
|
||||
|
||||
@strawberry.type
|
||||
@@ -58,7 +58,9 @@ class OrganizationUserMutation:
|
||||
@authenticated_user_only()
|
||||
async def add_to_organization(root, info, organization_id: int) -> Organization:
|
||||
async with get_session() as db:
|
||||
organization = (await db.scalars(get_base_resolver(models.Organization, object_id=organization_id))).one()
|
||||
organization = (await db.scalars(
|
||||
BaseQueryResolver(Organization, models.Organization).get_query(object_id=organization_id)
|
||||
)).one()
|
||||
|
||||
try:
|
||||
await db.execute(
|
||||
@@ -68,7 +70,6 @@ class OrganizationUserMutation:
|
||||
)
|
||||
)
|
||||
except IntegrityError:
|
||||
print("jiz existuje")
|
||||
pass
|
||||
|
||||
return Organization(**organization.as_dict())
|
||||
@@ -77,7 +78,9 @@ class OrganizationUserMutation:
|
||||
@authenticated_user_only()
|
||||
async def remove_from_organization(root, info, organization_id: int) -> Organization:
|
||||
async with get_session() as db:
|
||||
organization = (await db.scalars(get_base_resolver(models.Organization, object_id=organization_id))).one()
|
||||
organization = (await db.scalars(
|
||||
BaseQueryResolver(Organization, models.Organization).get_query(object_id=organization_id)
|
||||
)).one()
|
||||
|
||||
await db.execute(
|
||||
delete(models.user_is_in_organization).filter_by(
|
||||
@@ -87,21 +90,3 @@ class OrganizationUserMutation:
|
||||
)
|
||||
|
||||
return Organization(**organization.as_dict())
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class EditOrganizationMutation:
|
||||
@strawberry_sqlalchemy_input(model=models.Organization, exclude_fields=["id"])
|
||||
class EditOrganizationInput:
|
||||
pass
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def edit_organization(root, info, id: int, input: EditOrganizationInput) -> Organization:
|
||||
async with get_session() as db:
|
||||
organization = (await db.scalars(
|
||||
get_base_resolver(models.Organization, user_id=info.context.user_id, object_id=id)
|
||||
)).one()
|
||||
|
||||
updated_organization = await models.Organization.update(db, obj=organization, data=input.to_dict())
|
||||
return Organization(**updated_organization.as_dict())
|
||||
|
||||
Reference in New Issue
Block a user