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
Binary file added __pycache__/server.cpython-313.pyc
Binary file not shown.
30 changes: 30 additions & 0 deletions client/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

fetch("http://localhost:5001/events")
.then(response => response.json())
.then(events => {
events.forEach(renderEvent);
});


document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
const title = document.querySelector("#title").value.trim();

if (!title) return;

fetch("http://localhost:5001/events", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title })
})
.then(response => response.json())
.then(renderEvent);

document.querySelector("#title").value = "";
});

function renderEvent(event) {
const li = document.createElement("li");
li.textContent = event.title;
document.querySelector("#event-list").appendChild(li);
}
37 changes: 22 additions & 15 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
from flask import Flask, jsonify, request
from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# Create a list called 'events' with a couple of sample event dictionaries
# Each dictionary should have an 'id' and a 'title'
events = [
{"id": 1, "title": "Yoga in the Park"},
{"id": 2, "title": "Lake 5K Run"}
]

# TASK: Create a route for "/"
# This route should return a JSON welcome message
@app.route("/", methods=["GET"])
def welcome():
return jsonify({"message": "Welcome!"}), 200

# TASK: Create a GET route for "/events"
# This route should return the full list of events as JSON
@app.route("/events", methods=["GET"])
def get_events():
return jsonify(events), 200

# TASK: Create a POST route for "/events"
# This route should:
# 1. Get the JSON data from the request
# 2. Validate that "title" is provided
# 3. Create a new event with a unique ID and the provided title
# 4. Add the new event to the events list
# 5. Return the new event with status code 201
@app.route("/events", methods=["POST"])
def add_event():
data = request.get_json()
if not data or "title" not in data:
return jsonify({"error": "Title is required"}), 400

new_id = max((e["id"] for e in events), default=0) + 1
new_event = {"id": new_id, "title": data["title"]}
events.append(new_event)
return jsonify(new_event), 201

if __name__ == "__main__":
app.run(debug=True)
app.run(debug=True, port=5001)
Binary file added tests/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file not shown.