-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
122 lines (105 loc) · 4.47 KB
/
Copy pathapp.py
File metadata and controls
122 lines (105 loc) · 4.47 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
116
117
118
119
120
121
122
"""
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"]
},
"Basketball Team": {
"description": "Competitive basketball team for interscholastic play",
"schedule": "Mondays and Wednesdays, 4:00 PM - 5:30 PM",
"max_participants": 15,
"participants": ["alex@mergington.edu"]
},
"Tennis Club": {
"description": "Learn tennis skills and compete in matches",
"schedule": "Tuesdays and Thursdays, 3:30 PM - 5:00 PM",
"max_participants": 16,
"participants": ["ava@mergington.edu"]
},
"Art Studio": {
"description": "Explore painting, drawing, and mixed media techniques",
"schedule": "Wednesdays, 3:30 PM - 5:00 PM",
"max_participants": 18,
"participants": ["isabella@mergington.edu", "lucas@mergington.edu"]
},
"Drama Club": {
"description": "Perform in theatrical productions and develop acting skills",
"schedule": "Thursdays, 4:00 PM - 5:30 PM",
"max_participants": 25,
"participants": ["noah@mergington.edu"]
},
"Debate Team": {
"description": "Engage in competitive debate and public speaking",
"schedule": "Mondays and Fridays, 3:30 PM - 4:30 PM",
"max_participants": 14,
"participants": ["mia@mergington.edu", "ethan@mergington.edu"]
},
"Science Club": {
"description": "Conduct experiments and explore advanced scientific concepts",
"schedule": "Tuesdays, 3:30 PM - 5:00 PM",
"max_participants": 20,
"participants": ["charlotte@mergington.edu"]
}
}
@app.get("/")
def root():
return RedirectResponse(url="/static/index.html")
@app.get("/activities")
def get_activities():
return activities
@app.post("/activities/{activity_name}/signup")
def signup_for_activity(activity_name: str, email: str):
"""Sign up a student for an activity"""
# Validate activity exists
if activity_name not in activities:
raise HTTPException(status_code=404, detail="Activity not found")
# Get the specific activity
activity = activities[activity_name]
# Validate student is not already signed up
if email in activity["participants"]:
raise HTTPException(status_code=400, detail="Student already signed up for this activity")
# Add student
activity["participants"].append(email)
return {"message": f"Signed up {email} for {activity_name}"}
@app.delete("/activities/{activity_name}/participants/{email}")
def unregister_from_activity(activity_name: str, email: str):
"""Unregister a student from an activity"""
# Validate activity exists
if activity_name not in activities:
raise HTTPException(status_code=404, detail="Activity not found")
activity = activities[activity_name]
# Validate student is currently signed up
if email not in activity["participants"]:
raise HTTPException(status_code=404, detail="Student is not signed up for this activity")
activity["participants"].remove(email)
return {"message": f"Unregistered {email} from {activity_name}"}