11from src .database import daily_sun_db
22from src .models .article import Article
33from pymongo import UpdateOne
4- from datetime import datetime , timedelta
4+ from datetime import datetime , timedelta , timezone
55
66class ArticleRepository :
77 @staticmethod
@@ -52,7 +52,9 @@ def find_recent(limit_days=3):
5252 Retrieve articles from the last N days, sorted by published_at descending.
5353 """
5454 article_collection = daily_sun_db ["news_articles" ]
55- query = {"published_at" : {"$gte" : datetime .now () - timedelta (days = limit_days )}}
55+ # Calculate threshold as ISO 8601 string
56+ threshold = (datetime .now (timezone .utc ) - timedelta (days = limit_days )).isoformat ().replace ('+00:00' , 'Z' )
57+ query = {"published_at" : {"$gte" : threshold }}
5658 articles = article_collection .find (query ).sort ("published_at" , - 1 )
5759 return [Article .from_dict (article ) for article in articles ]
5860
@@ -62,9 +64,11 @@ def find_by_sports_type(sports_type, limit_days=3):
6264 Retrieve articles by sports_type from the last N days, sorted by published_at descending.
6365 """
6466 article_collection = daily_sun_db ["news_articles" ]
67+ # Calculate threshold as ISO 8601 string
68+ threshold = (datetime .now (timezone .utc ) - timedelta (days = limit_days )).isoformat ().replace ('+00:00' , 'Z' )
6569 query = {
6670 "sports_type" : sports_type ,
67- "published_at" : {"$gte" : datetime . now () - timedelta ( days = limit_days ) }
71+ "published_at" : {"$gte" : threshold }
6872 }
6973 articles = article_collection .find (query ).sort ("published_at" , - 1 )
7074 return [Article .from_dict (article ) for article in articles ]
@@ -75,5 +79,7 @@ def delete_not_recent(limit_days=3):
7579 Delete articles older than N days, sorted by published_at descending.
7680 """
7781 article_collection = daily_sun_db ["news_articles" ]
78- query = {"published_at" : {"$lt" : datetime .now () - timedelta (days = limit_days )}}
82+ # Calculate threshold as ISO 8601 string
83+ threshold = (datetime .now (timezone .utc ) - timedelta (days = limit_days )).isoformat ().replace ('+00:00' , 'Z' )
84+ query = {"published_at" : {"$lt" : threshold }}
7985 article_collection .delete_many (query )
0 commit comments