Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type CreateReport {
report: Report
}

scalar Date

scalar DateTime

enum DayOfWeekEnum {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -275,6 +278,7 @@ type Query {
getUserFriends(userId: Int!): [User]
getCapacityReminderById(id: Int!): CapacityReminder
getAllCapacityReminders: [CapacityReminder]
getWeeklyChallengeByDate(date: Date!): WeeklyChallenge
}

type RefreshAccessToken {
Expand Down Expand Up @@ -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!
Expand Down
36 changes: 36 additions & 0 deletions src/models/weekly_challenge.py
Original file line number Diff line number Diff line change
@@ -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()
}

63 changes: 60 additions & 3 deletions src/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -301,6 +302,15 @@ class Meta:
exclude_fields = ("fcm_token",)



# MARK: - Weekly Challenge

class WeeklyChallenge(SQLAlchemyObjectType):
class Meta:
model = WeeklyChallengeModel



# MARK: - Query


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Loading