Pridani skriptu pro pridani elevace, uprava prace s transakcemi

This commit is contained in:
Michal Kváček
2023-09-12 11:00:36 +02:00
parent ed5df4f953
commit 64cb544e2d
22 changed files with 519 additions and 226 deletions
+31 -18
View File
@@ -3,6 +3,7 @@ import strawberry
from sqlalchemy import select
from database import models
from decorators.endpoints import authenticated_user_only
from dependencies.db import get_session
from graphql_schema.dataloaders.flight import flights_by_copilot_dataloader
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input
@@ -23,7 +24,7 @@ def get_base_query(user_id: int):
select(models.Copilot)
.filter(models.Copilot.created_by_id == user_id)
.filter(models.Copilot.deleted.is_(False))
.order_by(models.Copilot.id.desc())
.order_by(models.Copilot.name)
)
@@ -32,17 +33,22 @@ class CopilotQueries:
@strawberry.field()
@authenticated_user_only()
async def copilots(root, info) -> List[Copilot]:
return (await info.context.db.scalars(
get_base_query(info.context.user_id)
)).all()
async with get_session() as db:
copilots = (await db.scalars(
get_base_query(info.context.user_id)
)).all()
return [Copilot(**c.as_dict()) for c in copilots]
@strawberry.field()
@authenticated_user_only()
async def copilot(root, info, id: int) -> Copilot:
return (await info.context.db.scalars(
get_base_query(info.context.user_id)
.filter(models.Copilot.id == id)
)).one()
async with get_session() as db:
copilot = (await db.scalars(
get_base_query(info.context.user_id)
.filter(models.Copilot.id == id)
)).one()
return Copilot(**copilot.as_dict())
@strawberry.type
@@ -56,13 +62,16 @@ class CreateCopilotMutation:
@authenticated_user_only()
async def create_copilot(root, info, input: CreateCopilotInput) -> Copilot:
input_data = input.to_dict()
return await models.Copilot.create(
info.context.db,
data=dict(
**input_data,
created_by_id=info.context.user_id,
async with get_session() as db:
copilot = await models.Copilot.create(
db,
data=dict(
**input_data,
created_by_id=info.context.user_id,
)
)
)
return Copilot(**copilot.as_dict())
@strawberry.type
@@ -75,8 +84,12 @@ class EditCopilotMutation:
@strawberry.mutation
@authenticated_user_only()
async def edit_copilot(root, info, id: int, input: EditCopilotInput) -> Copilot:
copilot = (await info.context.db.scalars(
get_base_query(info.context.user_id).filter(models.Copilot.id == id)
)).one()
return await models.Copilot.update(info.context.db, obj=copilot, data=input.to_dict())
async with get_session() as db:
copilot = (await db.scalars(
get_base_query(info.context.user_id).filter(models.Copilot.id == id)
)).one()
updated_copilot = await models.Copilot.update(db, obj=copilot, data=input.to_dict())
return Copilot(**updated_copilot.as_dict())