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

Education endpoints #9

Merged
merged 2 commits into from
Oct 10, 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
58 changes: 56 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,68 @@ def education():
Handles education requests
'''
if request.method == 'GET':
return jsonify({})
return jsonify(data['education']), 200

if request.method == 'POST':
return jsonify({})
new_education = request.json

if (
'course' not in new_education
or 'school' not in new_education
or 'start_date' not in new_education
or 'end_date' not in new_education
or 'grade' not in new_education
or 'logo' not in new_education
):
return jsonify({'error': 'Invalid input, all fields (course, school, start_date, end_date, grade, logo) are required'}), 400

data['education'].append(new_education)

return jsonify({'message': 'Education added', 'data': new_education, 'index': len(data['education']) - 1}), 201

return jsonify({})


@app.route('/resume/education/<int:education_id>', methods=['GET', 'PUT', 'DELETE'])
def education_at_id(education_id=None):
'''
Handles education requests at a specific ID
'''
if request.method == 'GET':
if 0 <= education_id < len(data['education']):
return jsonify(data['education'][education_id]), 200
else:
return jsonify({'error': 'Education not found'}), 404

if request.method == 'PUT':
if 0 <= education_id < len(data['education']):
updated_data = request.get_json()

course = updated_data.get('course')
school = updated_data.get('school')
start_date = updated_data.get('start_date')
end_date = updated_data.get('end_date')
grade = updated_data.get('grade')
logo = updated_data.get('logo')

if not (course and school and start_date and end_date and grade and logo):
return jsonify({'error': 'Invalid input, all fields (course, school, start_date, end_date, grade, logo) are required'}), 400

data['education'][education_id] = Education(course, school, start_date, end_date, grade, logo)

return jsonify({'message': 'Education updated', 'data': data['education'][education_id]}), 200
else:
return jsonify({'error': 'Education not found'}), 404

if request.method == 'DELETE':
if 0 <= education_id < len(data['education']):
deleted_education = data['education'].pop(education_id)

return jsonify({"message": "Education deleted", "data": deleted_education}), 200
else:
return jsonify({"error": "Education not found"}), 404


@app.route('/resume/skill', methods=['GET', 'POST'])
def skill():
'''
Expand Down
54 changes: 54 additions & 0 deletions test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,57 @@ def test_skill():

response = app.test_client().get('/resume/skill')
assert response.json[item_id] == example_skill


def delete_education():
'''
Add a new education and then delete it.

Check that the education is no longer in the list.
'''
example_education = {
"course": "Computer Science",
"end_date": "July 2022",
"grade": "80%",
"logo": "updated-logo.png",
"school": "University of Tech",
"start_date": "September 2019"
}

added_item_id = app.test_client().post('/resume/education', json=example_education).json['id']

app.test_client().delete(f'/resume/education/{added_item_id}')

response = app.test_client().get(f'/resume/education/{added_item_id}')
assert response.json['message'] == "Education not found"


def edit_education():
'''
Add a new education and then edit it.

Check that the education is updated in the list.
'''
example_education = {
"course": "Computer Science",
"end_date": "July 2022",
"grade": "80%",
"logo": "updated-logo.png",
"school": "University of Tech",
"start_date": "September 2019"
}

edited_education = {
"course": "NEW Computer Science",
"end_date": "July 2022",
"grade": "NEW 80%",
"logo": "updated-logo.png",
"school": "NEW University of Tech",
"start_date": "September 2019"
}

index = app.test_client().post('/resume/education', json=example_education).json['id']
app.test_client().put(f'/resume/education/{index}', json=edited_education)
response = app.test_client().get(f'/resume/education/{index}')

assert response["course"] == edited_education["course"]