Skip to content

Commit 8ebefa3

Browse files
authored
Merge pull request #68 from cuappdev/andrew/query_by_campus_filter
Added filtering games by location (on/off-campus)
2 parents c57a11d + c41f439 commit 8ebefa3

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def setup_database_indexes():
6262
game_collection.create_index([("sport", 1)], background=True)
6363
game_collection.create_index([("date", 1)], background=True)
6464
game_collection.create_index([("gender", 1)], background=True)
65+
game_collection.create_index([("city", 1)], background=True)
6566

6667
# Create compound indexes for common query combinations
6768
game_collection.create_index([("sport", 1), ("gender", 1)], background=True)

src/queries/game_query.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from bson import ObjectId
22
from flask_jwt_extended import get_jwt_identity, jwt_required
3-
from graphene import ObjectType, String, Field, List, Int, DateTime
3+
from graphene import Boolean, ObjectType, String, Field, List, Int, DateTime
44
from src.database import db
55
from src.services.game_service import GameService
66
from src.types import GameType
@@ -30,6 +30,7 @@ class GameQuery(ObjectType):
3030
GameType, sport=String(required=True), gender=String(required=True)
3131
)
3232
games_by_date = List(GameType, startDate=DateTime(required=True), endDate=DateTime(required=True))
33+
games_by_location = List(GameType, onCampus=Boolean(required=True))
3334
my_favorited_games = List(GameType, description="Current user's favorited games (requires auth).")
3435

3536
@jwt_required()
@@ -88,3 +89,10 @@ def resolve_games_by_date(self, info, startDate, endDate):
8889
Resolver for retrieving games by date.
8990
"""
9091
return GameService.get_games_by_date(startDate, endDate)
92+
93+
94+
def resolve_games_by_location(self, info, onCampus):
95+
"""
96+
Resolver for retrieving games by location.
97+
"""
98+
return GameService.get_games_by_location(onCampus)

src/repositories/game_repository.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ def find_by_date(startDate, endDate):
246246

247247
games = game_collection.find(query)
248248
return [Game.from_dict(game) for game in games]
249+
250+
@staticmethod
251+
def find_by_location(onCampus):
252+
"""
253+
Retrieve all games from the 'game' collection in MongoDB where
254+
"onCampus" boolean indicates location is on campus (true) or
255+
off campus (false).
256+
"""
257+
game_collection = db["game"]
258+
259+
if onCampus:
260+
query = {"city": "Ithaca"}
261+
else:
262+
query = {"city": {"$ne": "Ithaca"}}
263+
264+
games = game_collection.find(query)
265+
266+
return [Game.from_dict(game) for game in games]
249267

250268
@staticmethod
251269
def find_by_ids(game_ids):

src/services/game_service.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ def get_games_by_date(startDate, endDate):
120120
"""
121121
return GameRepository.find_by_date(startDate, endDate)
122122

123+
@staticmethod
124+
def get_games_by_location(onCampus):
125+
"""
126+
Retrieves all games by their location.
127+
"""
128+
return GameRepository.find_by_location(onCampus)
129+
123130
@staticmethod
124131
def get_tournament_games_by_sport_gender(sport, gender, after_date=None):
125132
"""

0 commit comments

Comments
 (0)