Refaktoring a bugfixing
This commit is contained in:
@@ -2,43 +2,18 @@ import asyncio
|
||||
from typing import List, Optional
|
||||
import strawberry
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from starlette.status import HTTP_401_UNAUTHORIZED
|
||||
from background_jobs.weather import download_weather
|
||||
from database import models
|
||||
from decorators.endpoints import authenticated_user_only
|
||||
from decorators.error_logging import error_logging
|
||||
from dependencies.db import get_session
|
||||
from graphql_schema.entities.resolvers.flight import (
|
||||
handle_aircraft_save, handle_weather_info, FlightMutationResolver, get_airport,
|
||||
)
|
||||
from database.transaction import get_session
|
||||
from graphql_schema.entities.resolvers.flight import handle_aircraft_save, FlightMutationResolver, FlightQueryResolver
|
||||
from graphql_schema.entities.types.mutation_input import EditFlightInput, CreateFlightInput
|
||||
from .resolvers.base import get_list, get_one
|
||||
from .helpers.combobox import handle_combobox_save
|
||||
from graphql_schema.entities.types.types import Flight
|
||||
|
||||
|
||||
def get_base_query(user_id: Optional[int], username: Optional[str] = None, is_auth: bool = False):
|
||||
query = (
|
||||
select(models.Flight)
|
||||
.filter(models.Flight.deleted.is_(False))
|
||||
.order_by(models.Flight.takeoff_datetime.desc())
|
||||
)
|
||||
|
||||
if user_id:
|
||||
query = query.filter(models.Flight.created_by_id == user_id)
|
||||
|
||||
if username:
|
||||
query = (
|
||||
query
|
||||
.join(models.Flight.created_by)
|
||||
.filter(models.User.public_username == username)
|
||||
)
|
||||
|
||||
if not is_auth:
|
||||
query = query.filter(models.Flight.is_public.is_(True))
|
||||
|
||||
return query
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class FlightQueries:
|
||||
|
||||
@@ -47,11 +22,7 @@ class FlightQueries:
|
||||
if not info.context.user_id and not username:
|
||||
raise HTTPException(HTTP_401_UNAUTHORIZED)
|
||||
|
||||
query = (
|
||||
get_base_query(user_id=info.context.user_id, username=username, is_auth=bool(info.context.user_id))
|
||||
.order_by(models.Flight.id.desc())
|
||||
)
|
||||
return await get_list(models.Flight, query)
|
||||
return await FlightQueryResolver().get_list(user_id=info.context.user_id, username=username)
|
||||
|
||||
@strawberry.field()
|
||||
@error_logging
|
||||
@@ -59,11 +30,7 @@ class FlightQueries:
|
||||
if not info.context.user_id and not username:
|
||||
raise HTTPException(HTTP_401_UNAUTHORIZED)
|
||||
|
||||
query = (
|
||||
get_base_query(user_id=info.context.user_id, username=username, is_auth=bool(info.context.user_id))
|
||||
.filter(models.Flight.id == id)
|
||||
)
|
||||
return await get_one(models.Flight, query)
|
||||
return await FlightQueryResolver().get_one(id, user_id=info.context.user_id, username=username)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
@@ -72,36 +39,49 @@ class FlightMutation:
|
||||
@authenticated_user_only()
|
||||
async def create_flight(self, info, input: CreateFlightInput) -> Flight:
|
||||
data = input.to_dict()
|
||||
|
||||
async with (get_session() as db):
|
||||
takeoff_airport = await get_airport(db, input.takeoff_airport, info.context.user_id)
|
||||
landing_airport = await get_airport(db, input.landing_airport, info.context.user_id)
|
||||
|
||||
user_id = info.context.user_id
|
||||
async with get_session() as db:
|
||||
aircraft_id = await handle_aircraft_save(db, info.context.user_id, input.aircraft)
|
||||
weather_takeoff, weather_landing = await asyncio.gather(
|
||||
handle_weather_info(db, data['takeoff_datetime'], takeoff_airport),
|
||||
handle_weather_info(db, data['landing_datetime'], landing_airport)
|
||||
)
|
||||
await db.flush()
|
||||
|
||||
flight = await models.Flight.create(db, data={
|
||||
**data,
|
||||
"takeoff_weather_info_id": weather_takeoff.id if weather_takeoff else None,
|
||||
"landing_weather_info_id": weather_landing.id if weather_landing else None,
|
||||
"takeoff_airport_id": takeoff_airport.id,
|
||||
"landing_airport_id": landing_airport.id,
|
||||
"has_terrain_elevation": False,
|
||||
# TODO: tohle je blbost, bude to vyrabet dve stejne instance!
|
||||
takeoff_airport_id, landing_airport_id = await asyncio.gather(
|
||||
handle_combobox_save(
|
||||
db, models.Airport, input.takeoff_airport, user_id, name_column="icao_code",
|
||||
extra_data={"name": input.takeoff_airport.name}
|
||||
),
|
||||
handle_combobox_save(
|
||||
db, models.Airport, input.landing_airport, user_id, name_column="icao_code",
|
||||
extra_data={"name": input.landing_airport.name}
|
||||
)
|
||||
)
|
||||
|
||||
data.update({
|
||||
"takeoff_airport_id": takeoff_airport_id,
|
||||
"landing_airport_id": landing_airport_id,
|
||||
"aircraft_id": aircraft_id,
|
||||
"created_by_id": info.context.user_id
|
||||
"has_terrain_elevation": False,
|
||||
"name": "",
|
||||
"description": ""
|
||||
})
|
||||
return Flight(**flight.as_dict())
|
||||
flight = FlightMutationResolver().create(data, info.context.user_id)
|
||||
|
||||
info.context.background_tasks.add_task(
|
||||
download_weather,
|
||||
flight_id=flight.id, airport_id=takeoff_airport_id, date_time=flight.takeoff_datetime, type_="takeoff"
|
||||
)
|
||||
info.context.background_tasks.add_task(
|
||||
download_weather,
|
||||
flight_id=flight.id, airport_id=landing_airport_id, date_time=flight.landing_datetime, type_="landing"
|
||||
)
|
||||
|
||||
return flight
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def edit_flight(self, info, id: int, input: EditFlightInput) -> Flight:
|
||||
return await FlightMutationResolver.update(info.context, id, input)
|
||||
return await FlightMutationResolver().update(info.context, id, input)
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def delete_flight(self, info, id: int) -> Flight:
|
||||
return await FlightMutationResolver.delete(info.context.user_id, id)
|
||||
return await FlightMutationResolver().delete(info.context.user_id, id)
|
||||
|
||||
Reference in New Issue
Block a user