Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UserInformation and Reset data Endpoint #72

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
69 changes: 34 additions & 35 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,11 @@ def reset_data():
Resets the values stored in data to placeholders.
"""
data.clear()
data["experience"] = [
Experience(
"Software Developer",
"A Cool Company",
"October 2022",
"Present",
"Writing Python Code",
"example-logo.png",
),
]
data["education"] = [
Education(
"Computer Science",
"University of Tech",
"September 2019",
"July 2022",
"80%",
"example-logo.png",
),
]
data["skill"] = [
Skill("Python", "1-2 Years", "example-logo.png"),
]
data["user_information"] = [
UserInformation("Joe Smith", "[email protected]", "+11234567890"),
]
data["experience"] = []
data["education"] = []
data["skill"] = []
data["user_information"] = []
save_data("data/data.json", data)


# reset_data()
Expand Down Expand Up @@ -114,6 +93,13 @@ def hello_world():
"""
return jsonify({"message": "Hello, World!"})

@app.route("/reset", methods=["POST"])
def reset():
"""
Resets the data to the initial state
"""
reset_data()
return jsonify({"message": "Data has been reset"}), 200

@app.route("/resume/experience", methods=["GET", "POST"])
def experience():
Expand Down Expand Up @@ -176,7 +162,7 @@ def experience():
)
data["experience"].append(new_experience)
save_data("data/data.json", data)
new_experience_index = len(data["experience"]) - 1
new_experience_index = new_id - 1
return (
jsonify({"message": "New experience created", "id": new_experience_index}),
201,
Expand All @@ -192,6 +178,7 @@ def delete_experience(index):
"""
if 0 <= index < len(data["experience"]):
data["experience"].pop(index)
save_data("data/data.json", data)
return jsonify({"message": "Experience entry successfully deleted"}), 200
return jsonify({"error": "Experience entry not found"}), 404

Expand Down Expand Up @@ -286,6 +273,7 @@ def delete_education(index):
"""
if 0 <= index < len(data["education"]):
data["education"].pop(index)
save_data("data/data.json", data)
return jsonify({"message": "Education entry successfully deleted"}), 200
return jsonify({"error": "Education entry not found"}), 404

Expand Down Expand Up @@ -390,19 +378,29 @@ def user_information():
Handle user information requests
"""
if request.method == "GET":
return jsonify(data["user_information"]), 200
return jsonify([user.__dict__ for user in data["user_information"]]), 200

request_body = request.get_json()

error = validate_fields(["name", "email_address", "phone_number"], request.json)
error = validate_fields(["name", "email_address", "phone_number"], request_body)
if error:
return jsonify({"error": f"{', '.join(error)} parameter(s) is required"}), 400

is_valid_phone_number = validate_phone_number(request.json["phone_number"])
is_valid_phone_number = validate_phone_number(request_body["phone_number"])
if not is_valid_phone_number:
return jsonify({"error": "Invalid phone number"}), 400
if error:
return jsonify({"error": f"{', '.join(error)} parameter(s) is required"}), 400

if request.method in ("POST", "PUT"):
data["user_information"] = request.json
return jsonify(data["user_information"]), 201
new_user_information = UserInformation(
name=request_body["name"],
email_address=request_body["email_address"],
phone_number=request_body["phone_number"]
)

data["user_information"] = [new_user_information]
save_data("data/data.json", data)

return jsonify(new_user_information.__dict__), 201

return 400

Expand Down Expand Up @@ -535,4 +533,5 @@ def get_data():
"""
Get all data from the data.json file
"""
return jsonify(data), 200
final_data = load_data("data/data.json")
return jsonify(final_data), 200
8 changes: 8 additions & 0 deletions test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,11 @@ def test_save_data(tmpdir):
# Assert that the file contains the correct data
assert saved_data["experience"][0]["title"] == "Developer"
assert saved_data["user_information"][0]["name"] == "John Doe"

def test_reset_endpoint(client):
"""
Test the POST /reset endpoint to ensure it resets the data.
"""
response = client.post("/reset")
assert response.status_code == 200
assert response.json["message"] == "Data has been reset"
Loading