diff --git a/src/database.py b/src/database.py index 20f1331..753dfc7 100644 --- a/src/database.py +++ b/src/database.py @@ -62,6 +62,7 @@ def setup_database_indexes(): game_collection.create_index([("sport", 1)], background=True) game_collection.create_index([("date", 1)], background=True) game_collection.create_index([("gender", 1)], background=True) + game_collection.create_index([("city", 1)], background=True) # Create compound indexes for common query combinations game_collection.create_index([("sport", 1), ("gender", 1)], background=True) diff --git a/src/queries/game_query.py b/src/queries/game_query.py index 5842d05..9343e2f 100644 --- a/src/queries/game_query.py +++ b/src/queries/game_query.py @@ -1,6 +1,6 @@ from bson import ObjectId from flask_jwt_extended import get_jwt_identity, jwt_required -from graphene import ObjectType, String, Field, List, Int, DateTime +from graphene import Boolean, ObjectType, String, Field, List, Int, DateTime from src.database import db from src.services.game_service import GameService from src.types import GameType @@ -30,6 +30,7 @@ class GameQuery(ObjectType): GameType, sport=String(required=True), gender=String(required=True) ) games_by_date = List(GameType, startDate=DateTime(required=True), endDate=DateTime(required=True)) + games_by_location = List(GameType, onCampus=Boolean(required=True)) my_favorited_games = List(GameType, description="Current user's favorited games (requires auth).") @jwt_required() @@ -88,3 +89,10 @@ def resolve_games_by_date(self, info, startDate, endDate): Resolver for retrieving games by date. """ return GameService.get_games_by_date(startDate, endDate) + + + def resolve_games_by_location(self, info, onCampus): + """ + Resolver for retrieving games by location. + """ + return GameService.get_games_by_location(onCampus) diff --git a/src/repositories/game_repository.py b/src/repositories/game_repository.py index e531286..62a19d2 100644 --- a/src/repositories/game_repository.py +++ b/src/repositories/game_repository.py @@ -246,6 +246,24 @@ def find_by_date(startDate, endDate): games = game_collection.find(query) return [Game.from_dict(game) for game in games] + + @staticmethod + def find_by_location(onCampus): + """ + Retrieve all games from the 'game' collection in MongoDB where + "onCampus" boolean indicates location is on campus (true) or + off campus (false). + """ + game_collection = db["game"] + + if onCampus: + query = {"city": "Ithaca"} + else: + query = {"city": {"$ne": "Ithaca"}} + + games = game_collection.find(query) + + return [Game.from_dict(game) for game in games] @staticmethod def find_by_ids(game_ids): diff --git a/src/services/game_service.py b/src/services/game_service.py index 6fd3479..c7c4721 100644 --- a/src/services/game_service.py +++ b/src/services/game_service.py @@ -120,6 +120,13 @@ def get_games_by_date(startDate, endDate): """ return GameRepository.find_by_date(startDate, endDate) + @staticmethod + def get_games_by_location(onCampus): + """ + Retrieves all games by their location. + """ + return GameRepository.find_by_location(onCampus) + @staticmethod def get_tournament_games_by_sport_gender(sport, gender, after_date=None): """