diff --git a/requirements.txt b/requirements.txt index 5d9efb5..f2821b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ fastapi uvicorn httpx -watchfiles \ No newline at end of file +watchfiles +pytest \ No newline at end of file diff --git a/src/README.md b/src/README.md index a90534b..869ab56 100644 --- a/src/README.md +++ b/src/README.md @@ -6,6 +6,7 @@ A super simple FastAPI application that allows students to view and sign up for - View all available extracurricular activities - Sign up for activities +- Unregister participants from activities ## Getting Started @@ -31,6 +32,7 @@ A super simple FastAPI application that allows students to view and sign up for | ------ | ----------------------------------------------------------------- | ------------------------------------------------------------------- | | GET | `/activities` | Get all activities with their details and current participant count | | POST | `/activities/{activity_name}/signup?email=student@mergington.edu` | Sign up for an activity | +| DELETE | `/activities/{activity_name}/participants?email=student@mergington.edu` | Unregister a participant from an activity | ## Data Model diff --git a/src/app.py b/src/app.py index 4ebb1d9..acd54e0 100644 --- a/src/app.py +++ b/src/app.py @@ -38,6 +38,42 @@ "schedule": "Mondays, Wednesdays, Fridays, 2:00 PM - 3:00 PM", "max_participants": 30, "participants": ["john@mergington.edu", "olivia@mergington.edu"] + }, + "Soccer Club": { + "description": "Team-based soccer training, drills, and friendly matches", + "schedule": "Wednesdays, 3:30 PM - 5:00 PM", + "max_participants": 22, + "participants": ["liam@mergington.edu", "isabella@mergington.edu"] + }, + "Basketball Club": { + "description": "Basketball fundamentals, teamwork, and practice games", + "schedule": "Mondays and Thursdays, 4:00 PM - 5:30 PM", + "max_participants": 18, + "participants": ["noah@mergington.edu", "mia@mergington.edu"] + }, + "Art Club": { + "description": "Explore drawing, painting, and mixed-media art projects", + "schedule": "Tuesdays, 3:30 PM - 5:00 PM", + "max_participants": 15, + "participants": ["ava@mergington.edu", "ethan@mergington.edu"] + }, + "Drama Club": { + "description": "Acting workshops, improvisation, and stage performances", + "schedule": "Fridays, 4:00 PM - 5:30 PM", + "max_participants": 16, + "participants": ["charlotte@mergington.edu", "lucas@mergington.edu"] + }, + "Debate Club": { + "description": "Practice public speaking and structured argumentation", + "schedule": "Thursdays, 3:30 PM - 5:00 PM", + "max_participants": 14, + "participants": ["amelia@mergington.edu", "henry@mergington.edu"] + }, + "Math Olympiad": { + "description": "Advanced problem-solving and competition math preparation", + "schedule": "Wednesdays, 3:30 PM - 5:00 PM", + "max_participants": 12, + "participants": ["elijah@mergington.edu", "harper@mergington.edu"] } } @@ -62,6 +98,25 @@ def signup_for_activity(activity_name: str, email: str): # 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") +def unregister_participant(activity_name: str, email: str): + """Unregister 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="Participant not found in this activity") + + activity["participants"].remove(email) + return {"message": f"Unregistered {email} from {activity_name}"} diff --git a/src/static/app.js b/src/static/app.js index dcc1e38..41eb798 100644 --- a/src/static/app.js +++ b/src/static/app.js @@ -12,6 +12,7 @@ document.addEventListener("DOMContentLoaded", () => { // Clear loading message activitiesList.innerHTML = ""; + activitySelect.innerHTML = ''; // Populate activities list Object.entries(activities).forEach(([name, details]) => { @@ -19,12 +20,40 @@ document.addEventListener("DOMContentLoaded", () => { activityCard.className = "activity-card"; const spotsLeft = details.max_participants - details.participants.length; + const participantsMarkup = + details.participants.length > 0 + ? details.participants + .map( + (participant) => ` +
  • + ${participant} + +
  • + ` + ) + .join("") + : '
  • No participants yet
  • '; activityCard.innerHTML = `

    ${name}

    ${details.description}

    Schedule: ${details.schedule}

    Availability: ${spotsLeft} spots left

    +
    +
    Participants
    + +
    `; activitiesList.appendChild(activityCard); @@ -62,6 +91,7 @@ document.addEventListener("DOMContentLoaded", () => { messageDiv.textContent = result.message; messageDiv.className = "success"; signupForm.reset(); + await fetchActivities(); } else { messageDiv.textContent = result.detail || "An error occurred"; messageDiv.className = "error"; @@ -81,6 +111,52 @@ document.addEventListener("DOMContentLoaded", () => { } }); + activitiesList.addEventListener("click", async (event) => { + const deleteButton = event.target.closest(".participant-delete-btn"); + if (!deleteButton) { + return; + } + + const activity = deleteButton.dataset.activity; + const email = deleteButton.dataset.email; + + if (!activity || !email) { + return; + } + + deleteButton.disabled = true; + + try { + const response = await fetch( + `/activities/${encodeURIComponent(activity)}/participants?email=${encodeURIComponent(email)}`, + { method: "DELETE" } + ); + const result = await response.json(); + + if (response.ok) { + messageDiv.textContent = result.message; + messageDiv.className = "success"; + await fetchActivities(); + } else { + messageDiv.textContent = result.detail || "An error occurred"; + messageDiv.className = "error"; + } + + messageDiv.classList.remove("hidden"); + + setTimeout(() => { + messageDiv.classList.add("hidden"); + }, 5000); + } catch (error) { + messageDiv.textContent = "Failed to unregister participant. Please try again."; + messageDiv.className = "error"; + messageDiv.classList.remove("hidden"); + console.error("Error unregistering participant:", error); + } finally { + deleteButton.disabled = false; + } + }); + // Initialize app fetchActivities(); }); diff --git a/src/static/styles.css b/src/static/styles.css index a533b32..9f8518c 100644 --- a/src/static/styles.css +++ b/src/static/styles.css @@ -74,6 +74,69 @@ section h3 { margin-bottom: 8px; } +.participants-section { + margin-top: 12px; + padding-top: 10px; + border-top: 1px solid #d9e1f3; +} + +.participants-section h5 { + margin-bottom: 8px; + color: #1a237e; + font-size: 0.95rem; +} + +.participants-list { + list-style: none; + margin-left: 0; + padding-left: 0; + color: #334155; +} + +.participants-list li { + margin-bottom: 4px; +} + +.participant-item { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + background-color: #eef2ff; + border: 1px solid #dbe4ff; + border-radius: 6px; + padding: 8px 10px; +} + +.participant-empty { + font-style: italic; + color: #64748b; +} + +.participant-delete-btn { + background-color: #ffffff; + border: 1px solid #f2b8b5; + color: #c62828; + border-radius: 50%; + width: 30px; + height: 30px; + padding: 0; + display: inline-flex; + align-items: center; + justify-content: center; + font-size: 14px; + line-height: 1; +} + +.participant-delete-btn:hover { + background-color: #ffebee; +} + +.participant-delete-btn:disabled { + opacity: 0.55; + cursor: not-allowed; +} + .form-group { margin-bottom: 15px; } diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..5ca652a --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +import copy + +import pytest +from fastapi.testclient import TestClient + +from src import app as app_module + + +@pytest.fixture +def client(): + return TestClient(app_module.app) + + +@pytest.fixture(autouse=True) +def reset_activities(): + original_activities = copy.deepcopy(app_module.activities) + yield + app_module.activities.clear() + app_module.activities.update(copy.deepcopy(original_activities)) diff --git a/tests/test_activities_api.py b/tests/test_activities_api.py new file mode 100644 index 0000000..ff4239a --- /dev/null +++ b/tests/test_activities_api.py @@ -0,0 +1,95 @@ +from urllib.parse import quote + + +def test_get_activities_returns_activity_map(client): + response = client.get("/activities") + + assert response.status_code == 200 + activities = response.json() + assert isinstance(activities, dict) + assert "Chess Club" in activities + assert "participants" in activities["Chess Club"] + + +def test_signup_adds_participant(client): + activity_name = "Chess Club" + email = "new-student@mergington.edu" + + response = client.post( + f"/activities/{quote(activity_name, safe='')}/signup", + params={"email": email}, + ) + + assert response.status_code == 200 + assert response.json() == {"message": f"Signed up {email} for {activity_name}"} + + activities_response = client.get("/activities") + participants = activities_response.json()[activity_name]["participants"] + assert email in participants + + +def test_signup_unknown_activity_returns_404(client): + response = client.post( + f"/activities/{quote('Unknown Club', safe='')}/signup", + params={"email": "student@mergington.edu"}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Activity not found"} + + +def test_signup_duplicate_participant_returns_400(client): + activity_name = "Chess Club" + existing_email = "michael@mergington.edu" + + response = client.post( + f"/activities/{quote(activity_name, safe='')}/signup", + params={"email": existing_email}, + ) + + assert response.status_code == 400 + assert response.json() == {"detail": "Student already signed up for this activity"} + + +def test_unregister_removes_participant(client): + activity_name = "Chess Club" + email = "temp-removal@mergington.edu" + encoded_activity = quote(activity_name, safe="") + + signup_response = client.post( + f"/activities/{encoded_activity}/signup", + params={"email": email}, + ) + assert signup_response.status_code == 200 + + delete_response = client.delete( + f"/activities/{encoded_activity}/participants", + params={"email": email}, + ) + + assert delete_response.status_code == 200 + assert delete_response.json() == {"message": f"Unregistered {email} from {activity_name}"} + + activities_response = client.get("/activities") + participants = activities_response.json()[activity_name]["participants"] + assert email not in participants + + +def test_unregister_unknown_activity_returns_404(client): + response = client.delete( + f"/activities/{quote('Unknown Club', safe='')}/participants", + params={"email": "student@mergington.edu"}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Activity not found"} + + +def test_unregister_missing_participant_returns_404(client): + response = client.delete( + f"/activities/{quote('Chess Club', safe='')}/participants", + params={"email": "not-registered@mergington.edu"}, + ) + + assert response.status_code == 404 + assert response.json() == {"detail": "Participant not found in this activity"}