From 4122521aa3953cd799520dd8e9acb93a7b391023 Mon Sep 17 00:00:00 2001 From: april Date: Thu, 11 Jan 2024 14:54:14 -0600 Subject: [PATCH] Fix aircraft updating --- api/database/aircraft.py | 15 +++++++++++---- api/routes/aircraft.py | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/api/database/aircraft.py b/api/database/aircraft.py index 82f91c5..1619057 100644 --- a/api/database/aircraft.py +++ b/api/database/aircraft.py @@ -68,12 +68,13 @@ async def insert_aircraft(body: AircraftCreateSchema, id: str) -> ObjectId: return aircraft.inserted_id -async def update_aircraft(body: AircraftCreateSchema, id: str) -> AircraftDisplaySchema: +async def update_aircraft(body: AircraftCreateSchema, id: str, user: str) -> AircraftDisplaySchema: """ Update given aircraft in the database :param body: Updated aircraft data :param id: ID of aircraft to update + :param user: ID of updating user :return: Updated aircraft """ aircraft = await aircraft_collection.find_one({"_id": ObjectId(id)}) @@ -81,11 +82,17 @@ async def update_aircraft(body: AircraftCreateSchema, id: str) -> AircraftDispla if aircraft is None: raise HTTPException(404, "Aircraft not found") - updated_aircraft = await aircraft_collection.update_one({"_id": ObjectId(id)}, {"$set": body.model_dump()}) + updated_aircraft = await aircraft_collection.update_one({"_id": ObjectId(id)}, + {"$set": aircraft_add_helper(body.model_dump(), user)}) if updated_aircraft is None: - raise HTTPException(500, "Failed to update flight") + raise HTTPException(500, "Failed to update aircraft") - return AircraftDisplaySchema(**body.model_dump()) + aircraft = await aircraft_collection.find_one({"_id": ObjectId(id)}) + + if aircraft is None: + raise HTTPException(500, "Failed to fetch updated aircraft") + + return AircraftDisplaySchema(**aircraft_display_helper(aircraft)) async def update_aircraft_field(field: str, value: Any, id: str) -> AircraftDisplaySchema: diff --git a/api/routes/aircraft.py b/api/routes/aircraft.py index ac09d53..453fbe7 100644 --- a/api/routes/aircraft.py +++ b/api/routes/aircraft.py @@ -139,7 +139,7 @@ async def update_aircraft(aircraft_id: str, aircraft_body: AircraftCreateSchema, logger.info("Attempted access to unauthorized aircraft by %s", user.username) raise HTTPException(403, "Unauthorized access") - updated_aircraft_id = await db.update_aircraft(aircraft_body, aircraft_id) + updated_aircraft_id = await db.update_aircraft(aircraft_body, aircraft_id, user.id) return {"id": str(updated_aircraft_id)}