Uprava nahravani, zmeny v modelech

This commit is contained in:
Michal Kváček
2023-06-27 22:45:03 +02:00
parent 1cf1f84e92
commit ba5c3b6a49
9 changed files with 124 additions and 17 deletions
+4 -3
View File
@@ -5,7 +5,7 @@ from strawberry.file_uploads import Upload
from sqlalchemy import select
from database import models
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input
from upload_utils import handle_img_upload, delete_file
from upload_utils import handle_file_upload, delete_file
AIRCRAFT_UPLOAD_DEST_PATH = "/app/uploads/aircrafts/"
@@ -52,13 +52,14 @@ class CreateAircraftMutation:
class CreateAircraftInput:
photo: Optional[Upload]
@strawberry.mutation
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_img_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
input_data['photo_filename'] = await handle_file_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
return await models.Aircraft.create(
info.context.db,
@@ -86,7 +87,7 @@ class EditAircraftMutation:
if input.photo:
if aircraft.photo_filename:
delete_file(AIRCRAFT_UPLOAD_DEST_PATH + "/" + aircraft.photo_filename, silent=True)
update_data['photo_filename'] = await handle_img_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
update_data['photo_filename'] = await handle_file_upload(input.photo, AIRCRAFT_UPLOAD_DEST_PATH)
return await models.Aircraft.update(info.context.db, obj=aircraft, data=update_data)
+32 -1
View File
@@ -12,6 +12,7 @@ from graphql_schema.entities.aircraft import Aircraft
from graphql_schema.entities.airport import Airport
from graphql_schema.entities.copilot import CopilotType
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input
from upload_utils import handle_file_upload
# Bude se hodit: https://strawberry.rocks/docs/types/lazy
@@ -88,7 +89,37 @@ class CreateFlightMutation:
async def create_flight(self, info, input: CreateFlightInput) -> Flight:
input_data = input.to_dict()
return await models.Flight.create(info.context.db, data={
flight = await models.Flight.create(info.context.db, data={
**input_data,
# "photos": [], # aby se nedelal select pri vytvareni fotek
"created_by_id": info.context.user_id
})
await info.context.db.flush()
#
# if input.photos:
# photos_dest = f"/app/uploads/photos/{flight.id}/"
# for photo in input.photos:
# filename = await handle_file_upload(photo, photos_dest)
# flight.photos.append(models.Photo(**{
# "name": "",
# "filename": filename,
# "description": "",
# "created_by_id": info.context.user_id,
# }))
return flight
@strawberry.type
class EditFlightMutation:
@strawberry_sqlalchemy_input(models.Flight, exclude_fields=["id"], all_optional=True)
class EditFlightInput:
pass
@strawberry.mutation
async def edit_flight(self, info, id: int, input: EditFlightInput) -> Flight:
input_data = input.to_dict()
return await models.Flight.update(info.context.db, id=id, data=input_data)
+52
View File
@@ -0,0 +1,52 @@
from typing import List, Optional
import strawberry
from sqlalchemy import select
from database import models
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input
@strawberry_sqlalchemy_type(models.Photo)
class Photo:
pass
def get_base_query(user_id: int):
return (
select(models.Photo)
.filter(models.Photo.created_by_id == user_id)
.order_by(models.Photo.id.desc())
)
@strawberry.type
class PhotoQueries:
@strawberry.field
async def photos(root, info) -> List[Photo]:
query = get_base_query(info.context.user_id)
return (await info.context.db.scalars(query)).all()
@strawberry.type
class DeletePhotoMutation:
@strawberry.input
class DeletePhotoInput:
id: int
@strawberry_sqlalchemy_input(models.Photo, exclude_fields=[], all_optional=True)
class UpdatePhotoInput:
pass
@strawberry.mutation
async def update_photo(self, info, input: UpdatePhotoInput) -> Photo:
query = get_base_query(info.context.user_id)
photo = info.context.db.scalars(query.filter(models.Photo.id == input.id))
update_data = input.to_dict()
photo.update(**update_data)
return photo
@strawberry.mutation
async def delete_photo(self, info, input: DeletePhotoInput) -> Photo:
pass