diff --git a/app.py b/app.py index 1a76941..0e27f98 100644 --- a/app.py +++ b/app.py @@ -3,15 +3,18 @@ """ from dataclasses import asdict + from flask import Flask, jsonify, request from models import Education, Experience, Skill + app = Flask(__name__) data = { "experience": [ Experience( + 1, "Software Developer", "A Cool Company", "October 2022", @@ -48,11 +51,37 @@ def experience(): Handle experience requests """ if request.method == "GET": + data_experiences = data["experience"] + data_experiences_response = [asdict(exp) for exp in data_experiences] + return jsonify(data_experiences_response), 200 + + if request.method == "POST": + user_input = request.get_json() + required_fields = [ + "title", + "company", + "start_date", + "end_date", + "description", + "logo", + ] + # Validating User Input + if not all(field in user_input for field in required_fields): + return jsonify({"error": "Missing required fields"}), 400 + + # Create a new Experience instance + # Using the length of the of the experience list and the index of the new experience + user_input["id"] = len(data["experience"]) + 1 + new_experience = Experience(**user_input) + data["experience"].append(new_experience) + return jsonify({"index": new_experience.id - 1}), 201 + return jsonify() if request.method == "POST": return jsonify({}) + return jsonify({}) @@ -70,12 +99,19 @@ def get_single_experience(pk): return jsonify({}) + @app.route("/resume/education", methods=["GET", "POST"]) def education(): """ Handles education requests + """ + if request.method == "GET": + return jsonify({}) + + if request.method == "POST": + return jsonify({}) + - ''' if request.method == 'GET': existing_education_records = data["education"] return jsonify( @@ -86,6 +122,7 @@ def education(): ), 200 + if request.method == "POST": body = request.get_json() @@ -141,6 +178,7 @@ def skill(): new_skill = Skill(name, proficiency, logo) + data["skill"].append(new_skill) return jsonify( diff --git a/models.py b/models.py index b150e91..0e19a5a 100644 --- a/models.py +++ b/models.py @@ -12,6 +12,7 @@ class Experience: ''' Experience Class ''' + id: int title: str company: str start_date: str diff --git a/test_pytest.py b/test_pytest.py index f9189ae..766f134 100644 --- a/test_pytest.py +++ b/test_pytest.py @@ -21,6 +21,7 @@ def test_experience(): Check that it returns the new experience in that list """ example_experience = { + "id": 2, "title": "Software Developer", "company": "A Cooler Company", "start_date": "October 2022", @@ -30,7 +31,10 @@ def test_experience(): } item_id = ( - app.test_client().post("/resume/experience", json=example_experience).json["id"] + app.test_client() + .post("/resume/experience", json=example_experience) + .json["index"] + ) response = app.test_client().get("/resume/experience") assert response.json[item_id] == example_experience @@ -68,6 +72,7 @@ def test_skill(): "name": "JavaScript", "proficiency": "2-4 years", "logo": "example-logo.png", + } item_id = app.test_client().post("/resume/skill", json=example_skill).json["id"] @@ -81,10 +86,11 @@ def test_get_skill_by_id(): "name": "Blowing Bubbles an Fighting Crime", "proficiency": "5+ years", "logo": "some-logo.png" + } - item_id = app.test_client().post('/resume/skill', - json=example_skill).json['id'] + item_id = app.test_client().post("/resume/skill", json=example_skill).json["id"] + response = app.test_client().get(f'/resume/skill/{item_id}') data = response.json