@@ -149,13 +149,14 @@ async def score_set_update(
149149 item_update : score_set .ScoreSetUpdateAllOptional ,
150150 exclude_unset : bool = False ,
151151 user_data : UserData ,
152+ existing_item : Optional [ScoreSet ] = None ,
152153) -> ScoreSetUpdateResult :
153154 logger .info (msg = "Updating score set." , extra = logging_context ())
154155
155156 should_create_variants = False
156157 item_update_dict : dict [str , Any ] = item_update .model_dump (exclude_unset = exclude_unset )
157158
158- item = db .query (ScoreSet ).filter (ScoreSet .urn == urn ).one_or_none ()
159+ item = existing_item or db .query (ScoreSet ).filter (ScoreSet .urn == urn ).one_or_none ()
159160 if not item or item .id is None :
160161 logger .info (msg = "Failed to update score set; The requested score set does not exist." , extra = logging_context ())
161162 raise HTTPException (status_code = 404 , detail = f"score set with URN '{ urn } ' not found" )
@@ -1415,11 +1416,23 @@ async def update_score_set_with_variants(
14151416 }
14161417
14171418 # Create the update object using **kwargs in as_form
1418- item_update = score_set .ScoreSetUpdateAllOptional .as_form (** form_dict )
1419+ item_update_partial = score_set .ScoreSetUpdateAllOptional .as_form (** form_dict )
14191420 except Exception as e :
14201421 raise HTTPException (status_code = 422 , detail = str (e ))
14211422
1422- itemUpdateResult = await score_set_update (db = db , urn = urn , item_update = item_update , exclude_unset = True , user_data = user_data )
1423+ # get existing item from db
1424+ existing_item = db .query (ScoreSet ).filter (ScoreSet .urn == urn ).one_or_none ()
1425+
1426+ # merge existing item data with item_update data to validate against ScoreSetUpdate
1427+ if existing_item :
1428+ existing_item_data = score_set .ScoreSet .model_validate (existing_item ).model_dump ()
1429+ updated_data = {** existing_item_data , ** item_update_partial .model_dump (exclude_unset = True )}
1430+ score_set .ScoreSetUpdate .model_validate (updated_data )
1431+ else :
1432+ logger .info (msg = "Failed to update score set; The requested score set does not exist." , extra = logging_context ())
1433+ raise HTTPException (status_code = 404 , detail = f"score set with URN '{ urn } ' not found" )
1434+
1435+ itemUpdateResult = await score_set_update (db = db , urn = urn , item_update = item_update_partial , exclude_unset = True , user_data = user_data , existing_item = existing_item )
14231436 updatedItem = itemUpdateResult ["item" ]
14241437 should_create_variants = itemUpdateResult .get ("should_create_variants" , False )
14251438
@@ -1471,6 +1484,8 @@ async def update_score_set(
14711484 save_to_logging_context ({"requested_resource" : urn })
14721485 logger .debug (msg = "Began score set update." , extra = logging_context ())
14731486
1487+ # this object will contain all required fields because item_update type is ScoreSetUpdate, but
1488+ # is converted to instance of ScoreSetUpdateAllOptional to match expected input of score_set_update function
14741489 score_set_update_item = score_set .ScoreSetUpdateAllOptional .model_validate (item_update .model_dump ())
14751490 itemUpdateResult = await score_set_update (db = db , urn = urn , item_update = score_set_update_item , exclude_unset = False , user_data = user_data )
14761491 updatedItem = itemUpdateResult ["item" ]
0 commit comments