diff --git a/__pycache__/server.cpython-313.pyc b/__pycache__/server.cpython-313.pyc new file mode 100644 index 0000000..92bb88d Binary files /dev/null and b/__pycache__/server.cpython-313.pyc differ diff --git a/client/script.js b/client/script.js index e69de29..d857d5d 100644 --- a/client/script.js +++ b/client/script.js @@ -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); +} \ No newline at end of file diff --git a/server.py b/server.py index e258245..765128b 100644 --- a/server.py +++ b/server.py @@ -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) \ No newline at end of file diff --git a/tests/__pycache__/__init__.cpython-313.pyc b/tests/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..e1f05eb Binary files /dev/null and b/tests/__pycache__/__init__.cpython-313.pyc differ diff --git a/tests/__pycache__/test_app.cpython-313-pytest-8.4.1.pyc b/tests/__pycache__/test_app.cpython-313-pytest-8.4.1.pyc new file mode 100644 index 0000000..1fc8efe Binary files /dev/null and b/tests/__pycache__/test_app.cpython-313-pytest-8.4.1.pyc differ