Skip to content

Commit 96579c5

Browse files
authored
Merge pull request #72 from yalsaffar/fixs-reset-userinfo
Refactor UserInformation and Reset data Endpoint
2 parents f5e4c4e + a13b2c2 commit 96579c5

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

app.py

+34-35
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,11 @@ def reset_data():
4141
Resets the values stored in data to placeholders.
4242
"""
4343
data.clear()
44-
data["experience"] = [
45-
Experience(
46-
"Software Developer",
47-
"A Cool Company",
48-
"October 2022",
49-
"Present",
50-
"Writing Python Code",
51-
"example-logo.png",
52-
),
53-
]
54-
data["education"] = [
55-
Education(
56-
"Computer Science",
57-
"University of Tech",
58-
"September 2019",
59-
"July 2022",
60-
"80%",
61-
"example-logo.png",
62-
),
63-
]
64-
data["skill"] = [
65-
Skill("Python", "1-2 Years", "example-logo.png"),
66-
]
67-
data["user_information"] = [
68-
UserInformation("Joe Smith", "[email protected]", "+11234567890"),
69-
]
44+
data["experience"] = []
45+
data["education"] = []
46+
data["skill"] = []
47+
data["user_information"] = []
48+
save_data("data/data.json", data)
7049

7150

7251
# reset_data()
@@ -114,6 +93,13 @@ def hello_world():
11493
"""
11594
return jsonify({"message": "Hello, World!"})
11695

96+
@app.route("/reset", methods=["POST"])
97+
def reset():
98+
"""
99+
Resets the data to the initial state
100+
"""
101+
reset_data()
102+
return jsonify({"message": "Data has been reset"}), 200
117103

118104
@app.route("/resume/experience", methods=["GET", "POST"])
119105
def experience():
@@ -176,7 +162,7 @@ def experience():
176162
)
177163
data["experience"].append(new_experience)
178164
save_data("data/data.json", data)
179-
new_experience_index = len(data["experience"]) - 1
165+
new_experience_index = new_id - 1
180166
return (
181167
jsonify({"message": "New experience created", "id": new_experience_index}),
182168
201,
@@ -192,6 +178,7 @@ def delete_experience(index):
192178
"""
193179
if 0 <= index < len(data["experience"]):
194180
data["experience"].pop(index)
181+
save_data("data/data.json", data)
195182
return jsonify({"message": "Experience entry successfully deleted"}), 200
196183
return jsonify({"error": "Experience entry not found"}), 404
197184

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

@@ -390,19 +378,29 @@ def user_information():
390378
Handle user information requests
391379
"""
392380
if request.method == "GET":
393-
return jsonify(data["user_information"]), 200
381+
return jsonify([user.__dict__ for user in data["user_information"]]), 200
382+
383+
request_body = request.get_json()
394384

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

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

403393
if request.method in ("POST", "PUT"):
404-
data["user_information"] = request.json
405-
return jsonify(data["user_information"]), 201
394+
new_user_information = UserInformation(
395+
name=request_body["name"],
396+
email_address=request_body["email_address"],
397+
phone_number=request_body["phone_number"]
398+
)
399+
400+
data["user_information"] = [new_user_information]
401+
save_data("data/data.json", data)
402+
403+
return jsonify(new_user_information.__dict__), 201
406404

407405
return 400
408406

@@ -535,4 +533,5 @@ def get_data():
535533
"""
536534
Get all data from the data.json file
537535
"""
538-
return jsonify(data), 200
536+
final_data = load_data("data/data.json")
537+
return jsonify(final_data), 200

test_pytest.py

+8
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,11 @@ def test_save_data(tmpdir):
414414
# Assert that the file contains the correct data
415415
assert saved_data["experience"][0]["title"] == "Developer"
416416
assert saved_data["user_information"][0]["name"] == "John Doe"
417+
418+
def test_reset_endpoint(client):
419+
"""
420+
Test the POST /reset endpoint to ensure it resets the data.
421+
"""
422+
response = client.post("/reset")
423+
assert response.status_code == 200
424+
assert response.json["message"] == "Data has been reset"

0 commit comments

Comments
 (0)