Nove dataloadery, lepsi sprava POI, migrace DB
This commit is contained in:
@@ -1,19 +1,35 @@
|
||||
from typing import List
|
||||
from typing import List, Optional, TYPE_CHECKING, Annotated
|
||||
import strawberry
|
||||
from sqlalchemy import select, or_
|
||||
from database import models
|
||||
from decorators.endpoints import authenticated_user_only
|
||||
from graphql_schema.dataloaders.flight import flight_by_poi_dataloader
|
||||
from graphql_schema.dataloaders.photos import poi_photos_dataloader
|
||||
from graphql_schema.dataloaders.poi import poi_type_dataloader
|
||||
from graphql_schema.entities.helpers.flight import handle_combobox_save
|
||||
from graphql_schema.entities.photo import Photo
|
||||
from graphql_schema.entities.poi_type import PointOfInterestType
|
||||
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input
|
||||
from graphql_schema.types import ComboboxInput
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .flight import Flight
|
||||
|
||||
@strawberry_sqlalchemy_type(models.PointOfInterest)
|
||||
class PointOfInterest:
|
||||
async def load_photos(root):
|
||||
return await poi_photos_dataloader.load(root.id)
|
||||
|
||||
async def load_type(root):
|
||||
return await poi_type_dataloader.load(root.type_id)
|
||||
|
||||
async def load_flights(root):
|
||||
return await flight_by_poi_dataloader.load(root.id)
|
||||
|
||||
type: Optional[PointOfInterestType] = strawberry.field(resolver=load_type)
|
||||
photos: List[Photo] = strawberry.field(resolver=load_photos)
|
||||
flights: List[Annotated["Flight", strawberry.lazy('.flight')]] = strawberry.field(resolver=load_flights)
|
||||
|
||||
|
||||
def get_base_query(user_id: int, only_my: bool = False):
|
||||
@@ -36,7 +52,7 @@ def get_base_query(user_id: int, only_my: bool = False):
|
||||
@strawberry.type
|
||||
class PointOfInterestQueries:
|
||||
|
||||
@strawberry.field
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def points_of_interest(root, info) -> List[PointOfInterest]:
|
||||
query = (
|
||||
@@ -46,7 +62,7 @@ class PointOfInterestQueries:
|
||||
|
||||
return (await info.context.db.scalars(query)).all()
|
||||
|
||||
@strawberry.field
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def point_of_interest(root, info, id: int) -> PointOfInterest:
|
||||
query = (
|
||||
@@ -56,16 +72,25 @@ class PointOfInterestQueries:
|
||||
return (await info.context.db.scalars(query)).one()
|
||||
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class CreatePointOfInterestMutation:
|
||||
@strawberry_sqlalchemy_input(models.PointOfInterest, exclude_fields=['id'])
|
||||
@strawberry_sqlalchemy_input(models.PointOfInterest, exclude_fields=['id', 'type_id'])
|
||||
class CreatePointOfInterestInput:
|
||||
pass
|
||||
type: Optional[ComboboxInput] = None
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def create_point_of_interest(root, info, input: CreatePointOfInterestInput) -> PointOfInterest:
|
||||
input_data = input.to_dict()
|
||||
|
||||
input_data['type_id'] = await handle_combobox_save(
|
||||
info.context.db,
|
||||
models.PointOfInterestType,
|
||||
input.type,
|
||||
info.context.user_id
|
||||
)
|
||||
|
||||
return await models.PointOfInterest.create(
|
||||
info.context.db,
|
||||
data=dict(
|
||||
@@ -77,22 +102,30 @@ class CreatePointOfInterestMutation:
|
||||
|
||||
@strawberry.type
|
||||
class EditPointOfInterestMutation:
|
||||
@strawberry_sqlalchemy_input(models.PointOfInterest, exclude_fields=['id'])
|
||||
@strawberry_sqlalchemy_input(models.PointOfInterest, exclude_fields=['id', 'type_id'])
|
||||
class EditPointOfInterestInput:
|
||||
pass
|
||||
type: Optional[ComboboxInput] = None
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def edit_point_of_interest(root, info, id: int, input: EditPointOfInterestInput) -> PointOfInterest:
|
||||
# TODO: kontrola organizace
|
||||
# TODO: kontrola opravneni na akci
|
||||
input_data = input.to_dict()
|
||||
|
||||
if input.type is not None:
|
||||
input_data['type_id'] = await handle_combobox_save(
|
||||
info.context.db,
|
||||
models.PointOfInterestType,
|
||||
input.type,
|
||||
info.context.user_id
|
||||
)
|
||||
|
||||
poi = (
|
||||
await info.context.db.scalars(
|
||||
get_base_query(info.context.user_id, only_my=True)
|
||||
.filter(models.PointOfInterest.id == id))
|
||||
).one()
|
||||
return await models.PointOfInterest.update(info.context.db, obj=poi, data=input.to_dict())
|
||||
return await models.PointOfInterest.update(info.context.db, obj=poi, data=input_data)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
|
||||
Reference in New Issue
Block a user