|
3 | 3 | ''' |
4 | 4 | from flask import Flask, jsonify, request |
5 | 5 | from models import Experience, Education, Skill |
| 6 | +from utils import validate_data |
6 | 7 |
|
7 | 8 | app = Flask(__name__) |
8 | 9 |
|
@@ -63,22 +64,24 @@ def experience(): |
63 | 64 | return jsonify(data['experience']), 200 |
64 | 65 |
|
65 | 66 | if request.method == 'POST': |
66 | | - experience_data = request.get_json() |
67 | | - # Validate the json data |
68 | | - if not all(key in experience_data for key in ['title', 'company', 'start_date', |
69 | | - 'end_date', 'description', 'logo']): |
70 | | - return jsonify({"error": "Missing required fields"}), 400 |
71 | | - |
72 | | - new_experience = Experience( |
73 | | - experience_data['title'], |
74 | | - experience_data['company'], |
75 | | - experience_data['start_date'], |
76 | | - experience_data['end_date'], |
77 | | - experience_data['description'], |
78 | | - experience_data['logo'] |
79 | | - ) |
80 | | - data['experience'].append(new_experience) |
81 | | - return jsonify({"id": len(data['experience']) - 1}), 201 |
| 67 | + try: |
| 68 | + experience_data = request.get_json() |
| 69 | + is_valid, error_message = validate_data('experience', experience_data) |
| 70 | + if not is_valid: |
| 71 | + return jsonify({"error": error_message}), 400 |
| 72 | + |
| 73 | + new_experience = Experience( |
| 74 | + experience_data['title'], |
| 75 | + experience_data['company'], |
| 76 | + experience_data['start_date'], |
| 77 | + experience_data['end_date'], |
| 78 | + experience_data['description'], |
| 79 | + experience_data['logo'] |
| 80 | + ) |
| 81 | + data['experience'].append(new_experience) |
| 82 | + return jsonify({"id": len(data['experience']) - 1}), 201 |
| 83 | + except (TypeError, ValueError, KeyError): |
| 84 | + return jsonify({"error": "Invalid data format"}), 400 |
82 | 85 |
|
83 | 86 | return jsonify({"error": "Method not allowed"}), 405 |
84 | 87 |
|
@@ -114,7 +117,18 @@ def education(): |
114 | 117 | return jsonify(data['education']), 200 |
115 | 118 |
|
116 | 119 | if request.method == 'POST': |
117 | | - return jsonify({}), 201 |
| 120 | + try: |
| 121 | + education_data = request.get_json() |
| 122 | + is_valid, error_message = validate_data('education', education_data) |
| 123 | + if not is_valid: |
| 124 | + return jsonify({"error": error_message}), 400 |
| 125 | + # pylint: disable=fixme |
| 126 | + # TODO: Create new Education object with education_data |
| 127 | + # TODO: Append new education to data['education'] |
| 128 | + # TODO: Return jsonify({"id": len(data['education']) - 1}), 201 |
| 129 | + return jsonify({}), 201 |
| 130 | + except (TypeError, ValueError, KeyError): |
| 131 | + return jsonify({"error": "Invalid data format"}), 400 |
118 | 132 |
|
119 | 133 | return jsonify({}) |
120 | 134 |
|
@@ -155,9 +169,20 @@ def skill(): |
155 | 169 | Returns 405 if method is not allowed. |
156 | 170 | """ |
157 | 171 | if request.method == 'GET': |
158 | | - return jsonify({}) |
| 172 | + return jsonify(data['skill']), 200 |
159 | 173 |
|
160 | 174 | if request.method == 'POST': |
161 | | - return jsonify({}) |
| 175 | + try: |
| 176 | + skill_data = request.get_json() |
| 177 | + is_valid, error_message = validate_data('skill', skill_data) |
| 178 | + if not is_valid: |
| 179 | + return jsonify({"error": error_message}), 400 |
| 180 | + # pylint: disable=fixme |
| 181 | + # TODO: Create new Skill object with skill_data |
| 182 | + # TODO: Append new skill to data['skill'] |
| 183 | + # TODO: Return jsonify({"id": len(data['skill']) - 1}), 201 |
| 184 | + return jsonify({}), 201 |
| 185 | + except (TypeError, ValueError, KeyError): |
| 186 | + return jsonify({"error": "Invalid data format"}), 400 |
162 | 187 |
|
163 | 188 | return jsonify({}) |
0 commit comments