50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from typing import Optional
|
|
|
|
from sqlalchemy import and_
|
|
|
|
from database import models
|
|
from graphql_schema.entities.resolvers.base import BaseQueryResolver
|
|
from graphql_schema.entities.types.types import Copilot
|
|
|
|
|
|
class CopilotQueryResolver(BaseQueryResolver):
|
|
def __init__(self):
|
|
super().__init__(Copilot, models.Copilot)
|
|
|
|
def get_query(
|
|
self,
|
|
user_id: int | None = None,
|
|
object_id: int | None = None,
|
|
order_by: list | None = None,
|
|
only_public: bool | None = False,
|
|
**kwargs
|
|
):
|
|
pilot_username = kwargs.pop("pilot_username", None)
|
|
upload_token = kwargs.pop("upload_token", None)
|
|
upload_flight_slug = kwargs.pop("upload_flight_slug", None)
|
|
|
|
query = super().get_query(
|
|
user_id=user_id, object_id=object_id, order_by=order_by,
|
|
only_public=only_public,
|
|
only_my=bool(user_id) and not only_public,
|
|
**kwargs
|
|
)
|
|
|
|
if pilot_username:
|
|
query = (
|
|
query.join(models.Copilot.created_by)
|
|
.filter(models.User.public_username == pilot_username)
|
|
)
|
|
|
|
if upload_token and upload_flight_slug:
|
|
query = (
|
|
query.join(models.Copilot.flights)
|
|
.filter(and_(
|
|
models.Flight.url_slug == upload_flight_slug,
|
|
models.Flight.created_by_id == models.User.id
|
|
))
|
|
.filter(models.flight_has_copilot.c.token == upload_token)
|
|
)
|
|
|
|
return query
|