From 1a9ddde570ad065128722fc683b7f4a178d03e61 Mon Sep 17 00:00:00 2001 From: tran Date: Mon, 24 Nov 2025 16:54:23 -0500 Subject: [PATCH 1/3] added weekly_challenge table and query get_ weekly_challenge_by_date in schema.py --- src/models/weekly_challenge.py | 36 ++++++++++++++++++++ src/schema.py | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/models/weekly_challenge.py diff --git a/src/models/weekly_challenge.py b/src/models/weekly_challenge.py new file mode 100644 index 0000000..33ae30c --- /dev/null +++ b/src/models/weekly_challenge.py @@ -0,0 +1,36 @@ +from datetime import datetime +from sqlalchemy import Column, Integer, String, Date, DateTime +from src.database import Base + +class WeeklyChallenge(Base): + """ + A weekly challenge. + + Attributes: + - `id` The ID of weekly challenge. + - `name` The name of weekly challenge. + - `message` The challenge's message. + - `start_date` The start date of the weekly challenge. + - `end_date` The end date of the weekly challenge. + """ + __tablename__ = "weekly_challenge" + + id = Column(Integer, primary_key = True, autoincrement = True) + name = Column(String, nullable = False) + message = Column(String, nullable = False) + start_date = Column(Date, nullable = False) + end_date = Column (Date, nullable = False) + + + def serialize(self): + """ + Serialize weekly challenge object + """ + return { + "id": self.id, + "name": self.name, + "message": self.message, + "start_date": self.start_date.isoformat(), + "end_date": self.end_date.isoformat() + } + \ No newline at end of file diff --git a/src/schema.py b/src/schema.py index d17ca30..c7a1fdf 100644 --- a/src/schema.py +++ b/src/schema.py @@ -21,6 +21,7 @@ from src.models.enums import DayOfWeekGraphQLEnum, CapacityReminderGymGraphQLEnum from src.models.giveaway import Giveaway as GiveawayModel from src.models.giveaway import GiveawayInstance as GiveawayInstanceModel +from src.models.weekly_challenge import WeeklyChallenge as WeeklyChallengeModel from src.models.workout import Workout as WorkoutModel from src.models.report import Report as ReportModel from src.models.hourly_average_capacity import HourlyAverageCapacity as HourlyAverageCapacityModel @@ -301,6 +302,15 @@ class Meta: exclude_fields = ("fcm_token",) + +# MARK: - Weekly Challenge + +class WeeklyChallenge(SQLAlchemyObjectType): + class Meta: + model = WeeklyChallengeModel + + + # MARK: - Query @@ -341,6 +351,28 @@ class Query(graphene.ObjectType): description="Get all capacity reminders." ) + get_weekly_challenge_by_date = graphene.Field( + WeeklyChallenge, + date = graphene.Date(required = True), + description = "Get a weekly challenge by its Date." + + ) + + + def resolve_get_weekly_challenge_by_date(self, info, date): + # Query all weekly challenges where the date falls between start_date and end_date + challenge = ( + WeeklyChallenge.get_query(info) + .filter( + WeeklyChallengeModel.start_date <= date, + WeeklyChallengeModel.end_date >= date + ) + .first() + ) + if not challenge: + raise GraphQLError("No weekly challenge found for the given date.") + return challenge + def resolve_get_all_gyms(self, info): query = Gym.get_query(info) return query.all() @@ -1000,6 +1032,35 @@ def mutate(self, info, reminder_id): return reminder + +""" +class CreateWeeklyChallenge(graphene.Mutation): + class Arguments: + name = graphene.String(required = True) + message = graphene.String(required = True) + start_date = graphene.Date(required = True) + end_date = graphene.Date(required = True) + + Output = WeeklyChallenge + + def mutate(self, info, name, message, start_date, end_date): + #Validate that end_date is after start_date + if end_date <= start_date: + raise GraphQLError("End date must be after start date") + + #Create new weekly challenge + new_challenge = WeeklyChallengeModel( + name = name, + message = message, + start_date = start_date, + end_date = end_date, + ) + + db_session.add(new_challenge) + db_session.commit() + return new_challenge +""" + class AddFriend(graphene.Mutation): class Arguments: user_id = graphene.Int(required=True) From afe9a57f40798219e80c24bf613c3e485964eae5 Mon Sep 17 00:00:00 2001 From: tran Date: Mon, 24 Nov 2025 21:15:20 -0500 Subject: [PATCH 2/3] added weekly challenge model --- requirements.txt | 1 + schema.graphql | 11 +++++++++++ src/schema.py | 1 - 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index c6219de..4277bac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -81,3 +81,4 @@ zipp==3.15.0 sentry-sdk==2.13.0 flask_jwt_extended==4.7.1 firebase-admin==6.4.0 +dotenv \ No newline at end of file diff --git a/schema.graphql b/schema.graphql index a161ce6..6a09b5b 100644 --- a/schema.graphql +++ b/schema.graphql @@ -85,6 +85,8 @@ type CreateReport { report: Report } +scalar Date + scalar DateTime enum DayOfWeekEnum { @@ -275,6 +277,7 @@ type Query { getUserFriends(userId: Int!): [User] getCapacityReminderById(id: Int!): CapacityReminder getAllCapacityReminders: [CapacityReminder] + getWeeklyChallengeByDate(date: Date!): WeeklyChallenge } type RefreshAccessToken { @@ -318,6 +321,14 @@ type User { friends: [User] } +type WeeklyChallenge { + id: ID! + name: String! + message: String! + startDate: String! + endDate: String! +} + type Workout { id: ID! workoutTime: DateTime! diff --git a/src/schema.py b/src/schema.py index c7a1fdf..7c174c4 100644 --- a/src/schema.py +++ b/src/schema.py @@ -355,7 +355,6 @@ class Query(graphene.ObjectType): WeeklyChallenge, date = graphene.Date(required = True), description = "Get a weekly challenge by its Date." - ) From 860553ae5c3ff86e562c18fd666269c2ae71b01c Mon Sep 17 00:00:00 2001 From: tran Date: Thu, 27 Nov 2025 08:37:57 -0500 Subject: [PATCH 3/3] added CreateWeeklyChallenge --- requirements.txt | 3 +-- schema.graphql | 1 + src/schema.py | 9 +++------ 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index 4277bac..e7020c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -80,5 +80,4 @@ Werkzeug==2.2.2 zipp==3.15.0 sentry-sdk==2.13.0 flask_jwt_extended==4.7.1 -firebase-admin==6.4.0 -dotenv \ No newline at end of file +firebase-admin==6.4.0 \ No newline at end of file diff --git a/schema.graphql b/schema.graphql index 6a09b5b..0d78c52 100644 --- a/schema.graphql +++ b/schema.graphql @@ -234,6 +234,7 @@ type Mutation { acceptFriendRequest(friendshipId: Int!): Friendship removeFriend(friendId: Int!, userId: Int!): RemoveFriend getPendingFriendRequests(userId: Int!): GetPendingFriendRequests + createWeeklyChallenge(endDate: Date!, message: String!, name: String!, startDate: Date!): WeeklyChallenge } type OpenHours { diff --git a/src/schema.py b/src/schema.py index 7c174c4..0eb1a66 100644 --- a/src/schema.py +++ b/src/schema.py @@ -1031,8 +1031,6 @@ def mutate(self, info, reminder_id): return reminder - -""" class CreateWeeklyChallenge(graphene.Mutation): class Arguments: name = graphene.String(required = True) @@ -1058,7 +1056,7 @@ def mutate(self, info, name, message, start_date, end_date): db_session.add(new_challenge) db_session.commit() return new_challenge -""" + class AddFriend(graphene.Mutation): class Arguments: @@ -1184,8 +1182,7 @@ class Mutation(graphene.ObjectType): add_friend = AddFriend.Field(description="Send a friend request to another user.") accept_friend_request = AcceptFriendRequest.Field(description="Accept a friend request.") remove_friend = RemoveFriend.Field(description="Remove a friendship.") - get_pending_friend_requests = GetPendingFriendRequests.Field( - description="Get all pending friend requests for a user.") - + get_pending_friend_requests = GetPendingFriendRequests.Field(description="Get all pending friend requests for a user.") + create_weekly_challenge = CreateWeeklyChallenge.Field(description="Creates a new weekly challenge.") schema = graphene.Schema(query=Query, mutation=Mutation)