@@ -402,26 +402,6 @@ async def aggregate_movies_recent_commented(
402402 limit : int = Query (default = 10 , ge = 1 , le = 50 ),
403403 movie_id : str = Query (default = None )
404404):
405- # Define aggregation pipeline to join movies with their most recent comments
406- pipeline = [
407- {
408- "$match" : {
409- "year" : {"$type" : "number" , "$gte" : 1800 , "$lte" : 2030 }
410- }
411- }
412- ]
413-
414- # Add movie_id filter if provided
415- if movie_id :
416- try :
417- object_id = ObjectId (movie_id )
418- pipeline [0 ]["$match" ]["_id" ] = object_id
419- except Exception :
420- return create_error_response (
421- message = "Invalid movie ID format" ,
422- code = "INTERNAL_SERVER_ERROR" ,
423- details = "The provided movie_id is not a valid ObjectId"
424- )
425405
426406 # Add a multi-stage aggregation that:
427407 # 1. Filters movies by valid year range
@@ -441,6 +421,18 @@ async def aggregate_movies_recent_commented(
441421 }
442422 }
443423 ]
424+
425+ # Add movie_id filter if provided
426+ if movie_id :
427+ try :
428+ object_id = ObjectId (movie_id )
429+ pipeline [0 ]["$match" ]["_id" ] = object_id
430+ except Exception :
431+ return create_error_response (
432+ message = "Invalid movie ID format" ,
433+ code = "INTERNAL_SERVER_ERROR" ,
434+ details = "The provided movie_id is not a valid ObjectId"
435+ )
444436
445437 # Add remaining pipeline stages
446438 pipeline .extend ([
0 commit comments