|
1 | 1 | from src.database import db
|
2 | 2 | from src.models.game import Game
|
3 | 3 |
|
| 4 | +import threading |
| 5 | +import logging |
| 6 | + |
| 7 | +# Configure logging |
| 8 | +logging.basicConfig( |
| 9 | + level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" |
| 10 | +) |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
4 | 13 |
|
5 | 14 | class GameRepository:
|
6 | 15 | @staticmethod
|
7 | 16 | def find_all(limit=100, offset=0):
|
8 | 17 | """
|
9 |
| - Retrieve all games from the 'games' collection in MongoDB with pagination. |
| 18 | + Retrieve all games from the 'game' collection in MongoDB with pagination. |
| 19 | + """ |
| 20 | + request_id = id(threading.current_thread()) # Get a unique ID for this request |
| 21 | + logger.info( |
| 22 | + f"Request {request_id}: Starting find_all with limit={limit}, offset={offset}" |
| 23 | + ) |
10 | 24 |
|
11 |
| - Args: |
12 |
| - limit (int): Maximum number of records to return |
13 |
| - offset (int): Number of records to skip |
| 25 | + try: |
| 26 | + game_collection = db["game"] |
| 27 | + logger.info(f"Request {request_id}: Connected to game collection") |
14 | 28 |
|
15 |
| - Returns: |
16 |
| - List[Game]: A list of Game objects. |
17 |
| - """ |
18 |
| - game_collection = db["game"] |
19 |
| - games = game_collection.find().skip(offset).limit(limit) |
20 |
| - return [Game.from_dict(game) for game in games] |
| 29 | + cursor = game_collection.find().skip(offset).limit(limit) |
| 30 | + logger.info(f"Request {request_id}: Created cursor") |
| 31 | + |
| 32 | + # Force MongoDB to actually perform the query |
| 33 | + games_list = list(cursor) |
| 34 | + logger.info(f"Request {request_id}: Retrieved {len(games_list)} games") |
| 35 | + |
| 36 | + result = [Game.from_dict(game) for game in games_list] |
| 37 | + logger.info( |
| 38 | + f"Request {request_id}: Converted to {len(result)} Game objects" |
| 39 | + ) |
| 40 | + |
| 41 | + return result |
| 42 | + except Exception as e: |
| 43 | + logger.error(f"Request {request_id}: Error in find_all: {str(e)}") |
| 44 | + raise |
21 | 45 |
|
22 | 46 | @staticmethod
|
23 | 47 | def find_by_id(game_id):
|
|
0 commit comments