-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
115 lines (101 loc) · 14.4 KB
/
app.py
File metadata and controls
115 lines (101 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
High School Management System API
A super simple FastAPI application that allows students to view and sign up
for extracurricular activities at Mergington High School.
"""
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import RedirectResponse
import os
from pathlib import Path
app = FastAPI(title="Mergington High School API",
description="API for viewing and signing up for extracurricular activities")
# Mount the static files directory
current_dir = Path(__file__).parent
app.mount("/static", StaticFiles(directory=os.path.join(Path(__file__).parent,
"static")), name="static")
# In-memory activity database
activities = {
"Chess Club": {
"description": "Learn strategies and compete in chess tournaments",
"schedule": "Fridays, 3:30 PM - 5:00 PM",
"max_participants": 12,
"participants": ["michael@mergington.edu", "daniel@mergington.edu"]
},
"Programming Class": {
"description": "Learn programming fundamentals and build software projects",
"schedule": "Tuesdays and Thursdays, 3:30 PM - 4:30 PM",
"max_participants": 20,
"participants": ["emma@mergington.edu", "sophia@mergington.edu"]
},
"Gym Class": {
"description": "Physical education and sports activities",
"schedule": "Mondays, Wednesdays, Fridays, 2:00 PM - 3:00 PM",
"max_participants": 30,
"participants": ["john@mergington.edu", "olivia@mergington.edu"]
},
"Soccer Team": {
"description": "Practice soccer skills and compete in inter-school matches",
"schedule": "Mondays and Wednesdays, 4:00 PM - 5:30 PM",
"max_participants": 22,
"participants": ["liam@mergington.edu", "noah@mergington.edu"]
},
"Basketball Club": {
"description": "Develop teamwork and basketball fundamentals",
"schedule": "Tuesdays and Fridays, 4:00 PM - 5:30 PM",
"max_participants": 15,
"participants": ["ava@mergington.edu", "isabella@mergington.edu"]
},
"Drama Club": {
"description": "Explore acting, stage presence, and theater production",
"schedule": "Thursdays, 3:30 PM - 5:00 PM",
"max_participants": 18,
"participants": ["mia@mergington.edu", "charlotte@mergington.edu"]
},
"Painting Workshop": {
"description": "Learn painting techniques and create visual art projects",
"schedule": "Wednesdays, 3:30 PM - 5:00 PM",
"max_participants": 16,
"participants": ["amelia@mergington.edu", "harper@mergington.edu"]
},
"Debate Team": {
"description": "Build public speaking and critical thinking through debates",
"schedule": "Mondays, 3:30 PM - 5:00 PM",
"max_participants": 14,
"participants": ["elijah@mergington.edu", "james@mergington.edu"]
},
"Math Olympiad": {
"description": "Solve advanced math problems and prepare for competitions",
"schedule": "Fridays, 3:30 PM - 5:00 PM",
"max_participants": 12,
"participants": ["lucas@mergington.edu", "benjamin@mergington.edu"]
}
}
@app.get("/")
def root():
return RedirectResponse(url="/static/index.html")
@app.get("/activities")
def get_activities():
return activities
# POST: Register participant
@app.post("/activities/{activity_name}/signup")
def signup_for_activity(activity_name: str, email: str):
"""Sign up a student for an activity"""
if activity_name not in activities:
raise HTTPException(status_code=404, detail="Activity not found")
activity = activities[activity_name]
if email in activity["participants"]:
raise HTTPException(status_code=400, detail="Student already signed up for this activity")
activity["participants"].append(email)
return {"message": f"Signed up {email} for {activity_name}"}
# DELETE: Unregister participant
@app.delete("/activities/{activity_name}/signup")
def unregister_from_activity(activity_name: str, email: str):
"""Remove a student from an activity"""
if activity_name not in activities:
raise HTTPException(status_code=404, detail="Activity not found")
activity = activities[activity_name]
if email not in activity["participants"]:
raise HTTPException(status_code=404, detail="Student not registered for this activity")
activity["participants"].remove(email)
return {"message": f"Removed {email} from {activity_name}"}