Skip to content

Commit 0aaeac0

Browse files
authored
feat: reorder section items on the backend (#35)
* feat: reorder section items on the backend * fix pylint issues * tests for PUT changes * address failing test cases * fix linting
1 parent f0efad4 commit 0aaeac0

File tree

2 files changed

+146
-32
lines changed

2 files changed

+146
-32
lines changed

app.py

+70-20
Original file line numberDiff line numberDiff line change
@@ -109,58 +109,92 @@ def edit_user(body):
109109
return jsonify({"error": "Unsupported request method !"}), 405
110110

111111

112-
@app.route("/resume/experience", methods=["GET", "POST"])
112+
@app.route("/resume/experience", methods=["GET", "POST", 'PUT'])
113113
def experience():
114114
"""
115115
Handle experience requests
116116
"""
117-
118-
if request.method == "GET":
117+
if request.method == 'GET':
119118
return jsonify(
120119
{"experience": [exp.__dict__ for exp in data["experience"]]})
121120

122121
if request.method == "POST":
123122
new_experience = request.json
123+
new_exp = new_experience["data"][0]
124124
experience_instance = Experience(
125-
new_experience["title"],
126-
new_experience["company"],
127-
new_experience["start_date"],
128-
new_experience["end_date"],
129-
new_experience["description"],
130-
new_experience["logo"],
125+
new_exp["title"],
126+
new_exp["company"],
127+
new_exp["start_date"],
128+
new_exp["end_date"],
129+
new_exp["description"],
130+
new_exp["logo"],
131131
)
132132
data["experience"].append(experience_instance)
133133
return jsonify({"id": len(data["experience"]) - 1})
134134

135-
return jsonify({})
135+
if request.method == 'PUT':
136+
body = request.get_json()
137+
new_experience_order = []
138+
for exp in body['data']:
139+
title = exp['title']
140+
company = exp['company']
141+
start_date = exp['start_date']
142+
end_date = exp['end_date']
143+
description = exp['description']
144+
logo = exp['logo']
145+
146+
new_experience_order.append(
147+
Experience(title, company, start_date, end_date, description, logo)
148+
)
149+
data['experience'] = new_experience_order
150+
151+
return_data = [item.__dict__ for item in data['experience']]
152+
return jsonify(return_data), 200
136153

154+
return jsonify({"error": "Unsupported request method !"}), 405
137155

138-
@app.route("/resume/education", methods=["GET", "POST"])
156+
@app.route('/resume/education', methods=['GET', 'POST', 'PUT'])
139157
def education():
140158
"""
141159
Handles education requests
142160
"""
143-
if request.method == "GET":
161+
if request.method == 'GET':
144162
return jsonify(
145163
{"education": [edu.__dict__ for edu in data["education"]]})
146164

147165
if request.method == "POST":
148166
new_education = request.json
167+
new_edu = new_education["data"][0]
149168
education_instance = Education(
150-
new_education["course"],
151-
new_education["school"],
152-
new_education["start_date"],
153-
new_education["end_date"],
154-
new_education["grade"],
155-
new_education["logo"],
169+
new_edu["course"],
170+
new_edu["school"],
171+
new_edu["start_date"],
172+
new_edu["end_date"],
173+
new_edu["grade"],
174+
new_edu["logo"],
156175
)
157176
data["education"].append(education_instance)
158177
return jsonify({"id": len(data["education"]) - 1})
159178

179+
if request.method == 'PUT':
180+
body = request.get_json()
181+
new_education_order = []
182+
for edu in body['data']:
183+
course = edu['course']
184+
school = edu['school']
185+
start_date = edu['start_date']
186+
end_date = edu['end_date']
187+
grade = edu['grade']
188+
logo = edu['logo']
189+
new_education_order.append(Education(course, school, start_date, end_date, grade, logo))
190+
data['education'] = new_education_order
191+
192+
return_data = [item.__dict__ for item in data['education']]
193+
return jsonify(return_data), 200
160194
return jsonify({})
161195

162196

163-
@app.route("/resume/skill", methods=["GET", "POST"])
197+
@app.route("/resume/skill", methods=["GET", "POST", 'PUT'])
164198
def skill():
165199
"""
166200
Handles Skill requests
@@ -171,12 +205,28 @@ def skill():
171205

172206
if request.method == "POST":
173207
new_skill = request.json
208+
skill_data = new_skill["data"][0]
174209
skill_instance = Skill(
175-
new_skill["name"], new_skill["proficiency"], new_skill["logo"]
210+
skill_data["name"],
211+
skill_data["proficiency"],
212+
skill_data["logo"]
176213
)
177214
data["skill"].append(skill_instance)
178215
return jsonify({"id": len(data["skill"]) - 1})
179216

217+
if request.method == 'PUT':
218+
body = request.get_json()
219+
new_skill_order = []
220+
for _skill in body['data']:
221+
name = _skill['name']
222+
proficiency = _skill['proficiency']
223+
logo = _skill['logo']
224+
new_skill_order.append(Skill(name, proficiency, logo))
225+
data['skill'] = new_skill_order
226+
227+
return_data = [item.__dict__ for item in data['skill']]
228+
return jsonify(return_data), 200
229+
180230
return jsonify({})
181231

182232

test_pytest.py

+76-12
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,43 @@ def test_experience():
6262
Check that it returns the new experience in that list
6363
'''
6464
example_experience = {
65-
"title": "Software Developer",
66-
"company": "A Cooler Company",
67-
"start_date": "October 2022",
68-
"end_date": "Present",
69-
"description": "Writing JavaScript Code",
70-
"logo": "example-logo.png"
65+
"data": [
66+
{
67+
"title": "Software Developer",
68+
"company": "A Cooler Company",
69+
"start_date": "October 2022",
70+
"end_date": "Present",
71+
"description": "Writing JavaScript Code",
72+
"logo": "example-logo.png"
73+
}
74+
]
7175
}
7276

7377
item_id = app.test_client().post('/resume/experience',
7478
json=example_experience).json['id']
7579
response = app.test_client().get('/resume/experience')
76-
assert response.json["experience"][item_id] == example_experience
80+
print(response.json)
81+
assert response.json["experience"][item_id] == example_experience['data'][0]
82+
83+
# test PUT request
84+
response = app.test_client().put('/resume/experience', json={"data": [
85+
{
86+
"title": "Software Developer",
87+
"company": "The Coolest Company",
88+
"start_date": "October 2024",
89+
"end_date": "Present",
90+
"description": "Writing Python Code",
91+
"logo": "example-logo.png"
92+
}
93+
]})
94+
assert response.status_code == 200
95+
response_data = response.json[0]
96+
assert response_data['title'] == 'Software Developer'
97+
assert response_data['company'] == 'The Coolest Company'
98+
assert response_data['start_date'] == 'October 2024'
99+
assert response_data['end_date'] == 'Present'
100+
assert response_data['description'] == 'Writing Python Code'
101+
assert response_data['logo'] == 'example-logo.png'
77102

78103

79104
def test_education():
@@ -82,19 +107,41 @@ def test_education():
82107
83108
Check that it returns the new education in that list
84109
'''
85-
example_education = {
110+
example_education = {"data": [{
86111
"course": "Engineering",
87112
"school": "NYU",
88113
"start_date": "October 2022",
89114
"end_date": "August 2024",
90115
"grade": "86%",
91116
"logo": "example-logo.png"
92-
}
117+
}]}
118+
93119
item_id = app.test_client().post('/resume/education',
94120
json=example_education).json['id']
95121

96122
response = app.test_client().get('/resume/education')
97-
assert response.json["education"][item_id] == example_education
123+
assert response.json["education"][item_id] == example_education['data'][0]
124+
125+
response = app.test_client().put('/resume/education', json={
126+
"data": [
127+
{
128+
"course": "Updated Course",
129+
"school": "Updated University",
130+
"start_date": "September 2020",
131+
"end_date": "June 2023",
132+
"grade": "90%",
133+
"logo": "new-education-logo.png"
134+
}
135+
]
136+
})
137+
assert response.status_code == 200
138+
response_data = response.json[0]
139+
assert response_data['course'] == 'Updated Course'
140+
assert response_data['school'] == 'Updated University'
141+
assert response_data['start_date'] == 'September 2020'
142+
assert response_data['end_date'] == 'June 2023'
143+
assert response_data['grade'] == '90%'
144+
assert response_data['logo'] == 'new-education-logo.png'
98145

99146

100147
def test_skill():
@@ -103,17 +150,34 @@ def test_skill():
103150
104151
Check that it returns the new skill in that list
105152
'''
106-
example_skill = {
153+
example_skill = { "data":[{
107154
"name": "JavaScript",
108155
"proficiency": "2-4 years",
109156
"logo": "example-logo.png"
157+
}]
110158
}
111159

112160
item_id = app.test_client().post('/resume/skill',
113161
json=example_skill).json['id']
114162

115163
response = app.test_client().get('/resume/skill')
116-
assert response.json["skills"][item_id] == example_skill
164+
assert response.json["skills"][item_id] == example_skill["data"][0]
165+
166+
response = app.test_client().put('/resume/skill', json={
167+
"data": [
168+
{
169+
"name": "Python",
170+
"proficiency": "4-6 years",
171+
"logo": "new-logo.png"
172+
}
173+
]
174+
})
175+
assert response.status_code == 200
176+
response_data = response.json[0]
177+
assert response_data['name'] == 'Python'
178+
assert response_data['proficiency'] == '4-6 years'
179+
assert response_data['logo'] == 'new-logo.png'
180+
117181

118182

119183
def test_get_project():

0 commit comments

Comments
 (0)