Refaktoring a bugfixing
This commit is contained in:
@@ -1,56 +1,46 @@
|
||||
from typing import List, Optional, TYPE_CHECKING
|
||||
from typing import List
|
||||
import strawberry
|
||||
from strawberry.file_uploads import Upload
|
||||
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 upload_utils import handle_file_upload, delete_file
|
||||
from database.transaction import get_session
|
||||
from graphql_schema.entities.helpers.combobox import handle_combobox_save
|
||||
from .resolvers.aircraft import get_aircraft_resolver
|
||||
from .resolvers.base import get_list, get_one
|
||||
from graphql_schema.entities.types.mutation_input import ComboboxInput
|
||||
from paths import AIRCRAFT_UPLOAD_DEST_PATH
|
||||
from utils.file import delete_file
|
||||
from utils.upload import handle_file_upload
|
||||
from .resolvers.aircraft import AircraftMutationResolver, AircraftQueryResolver
|
||||
from graphql_schema.entities.types.mutation_input import CreateAircraftInput, EditAircraftInput
|
||||
from graphql_schema.entities.types.types import Aircraft
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
||||
AIRCRAFT_UPLOAD_DEST_PATH = "/app/uploads/aircrafts/"
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class AircraftQueries:
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def aircrafts(root, info) -> List[Aircraft]:
|
||||
query = get_aircraft_resolver(info.context.user_id, info.context.organization_ids)
|
||||
return await get_list(models.Aircraft, query)
|
||||
return await AircraftQueryResolver().get_list(
|
||||
info.context.user_id,
|
||||
organization_ids=info.context.organization_ids
|
||||
)
|
||||
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def aircraft(root, info, id: int) -> Aircraft:
|
||||
query = get_aircraft_resolver(info.context.user_id, info.context.organization_ids, id)
|
||||
return await get_one(models.Aircraft, query)
|
||||
return await AircraftQueryResolver().get_one(
|
||||
id, user_id=info.context.user_id, organization_ids=info.context.organization_ids
|
||||
)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class CreateAircraftMutation:
|
||||
@strawberry_sqlalchemy_input(models.Aircraft, exclude_fields=['id', 'photo_filename'])
|
||||
class CreateAircraftInput:
|
||||
photo: Optional[Upload]
|
||||
organization: Optional[ComboboxInput] = None
|
||||
|
||||
class AircraftMutation:
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def create_aircraft(root, info, input: CreateAircraftInput) -> Aircraft:
|
||||
# TODO: kontrola organizace
|
||||
|
||||
input_data = input.to_dict()
|
||||
if input.photo:
|
||||
input_data['photo_filename'] = await handle_file_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
|
||||
|
||||
async with get_session() as db:
|
||||
if input.organization:
|
||||
if input.organization:
|
||||
async with get_session() as db:
|
||||
input_data['organization_id'] = await handle_combobox_save(
|
||||
db,
|
||||
models.Organization,
|
||||
@@ -58,29 +48,11 @@ class CreateAircraftMutation:
|
||||
user_id=info.context.user_id,
|
||||
)
|
||||
|
||||
aircraft = await models.Aircraft.create(
|
||||
db,
|
||||
data=dict(
|
||||
**input_data,
|
||||
created_by_id=info.context.user_id,
|
||||
)
|
||||
)
|
||||
|
||||
return Aircraft(**aircraft.as_dict())
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class EditAircraftMutation:
|
||||
@strawberry_sqlalchemy_input(models.Aircraft, exclude_fields=['photo_filename'])
|
||||
class EditAircraftInput:
|
||||
photo: Optional[Upload]
|
||||
organization: Optional[ComboboxInput] = None
|
||||
return await AircraftMutationResolver().create(data=input_data, user_id=info.context.user_id)
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def edit_aircraft(root, info, id: int, input: EditAircraftInput) -> Aircraft:
|
||||
# TODO: kontrola organizace
|
||||
|
||||
update_data = input.to_dict()
|
||||
async with get_session() as db:
|
||||
if input.organization:
|
||||
@@ -91,26 +63,24 @@ class EditAircraftMutation:
|
||||
user_id=info.context.user_id,
|
||||
)
|
||||
|
||||
aircraft = (await db.scalars(get_aircraft_resolver(user_id=info.context.user_id, aircraft_id=id))).one()
|
||||
query = AircraftQueryResolver().get_query(
|
||||
user_id=info.context.user_id,
|
||||
object_id=id,
|
||||
organization_ids=info.context.organization_ids
|
||||
)
|
||||
aircraft = (await db.scalars(query)).one()
|
||||
existing_photo_filename = aircraft.photo_filename
|
||||
|
||||
if input.photo:
|
||||
if aircraft.photo_filename:
|
||||
delete_file(AIRCRAFT_UPLOAD_DEST_PATH + "/" + aircraft.photo_filename, silent=True)
|
||||
update_data['photo_filename'] = await handle_file_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
|
||||
|
||||
aircraft = await models.Aircraft.update(db, obj=aircraft, data=update_data)
|
||||
if input.photo:
|
||||
if existing_photo_filename:
|
||||
delete_file(AIRCRAFT_UPLOAD_DEST_PATH + "/" + existing_photo_filename, silent=True)
|
||||
update_data['photo_filename'] = await handle_file_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
|
||||
|
||||
async with get_session() as db:
|
||||
aircraft = await models.Aircraft.update(db, id=id, data=update_data)
|
||||
return Aircraft(**aircraft.as_dict())
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class DeleteAircraftMutation:
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def delete_aircraft(self, info, id: int) -> Aircraft:
|
||||
async with get_session() as db:
|
||||
aircraft = (await db.scalars(get_aircraft_resolver(info.context.user_id, aircraft_id=id))).one()
|
||||
|
||||
aircraft = await models.Aircraft.update(db, obj=aircraft, data=dict(deleted=True))
|
||||
return Aircraft(**aircraft.as_dict())
|
||||
return await AircraftMutationResolver().delete(info.context.user_id, id)
|
||||
|
||||
Reference in New Issue
Block a user