diff --git a/requirements.txt b/requirements.txt index c6219de..e7020c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -80,4 +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 +firebase-admin==6.4.0 \ No newline at end of file diff --git a/schema.graphql b/schema.graphql index a161ce6..0d78c52 100644 --- a/schema.graphql +++ b/schema.graphql @@ -85,6 +85,8 @@ type CreateReport { report: Report } +scalar Date + scalar DateTime enum DayOfWeekEnum { @@ -232,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 { @@ -275,6 +278,7 @@ type Query { getUserFriends(userId: Int!): [User] getCapacityReminderById(id: Int!): CapacityReminder getAllCapacityReminders: [CapacityReminder] + getWeeklyChallengeByDate(date: Date!): WeeklyChallenge } type RefreshAccessToken { @@ -318,6 +322,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/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..0eb1a66 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,27 @@ 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 +1031,33 @@ 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) @@ -1124,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)