|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | from flask import Flask, jsonify, request
|
6 |
| -from models import Experience, Education, Skill, User |
| 6 | +from models import Experience, Education, Project, Skill, User |
7 | 7 | from utils import get_suggestion, check_phone_number, correct_spelling
|
8 | 8 |
|
9 | 9 |
|
|
39 | 39 | "example-logo.png",
|
40 | 40 | )
|
41 | 41 | ],
|
42 |
| - "skill": [Skill("Python", "1-2 Years", "example-logo.png")], |
| 42 | + "skill": [ |
| 43 | + Skill("Python", |
| 44 | + "1-2 Years", |
| 45 | + "example-logo.png") |
| 46 | + ], |
| 47 | + "project": [ |
| 48 | + Project( |
| 49 | + title="Sample Project", |
| 50 | + description="A sample project", |
| 51 | + technologies=["Python", "Flask"], |
| 52 | + link="https://github.com/username/sample-project" |
| 53 | + ) |
| 54 | + ] |
43 | 55 | }
|
44 | 56 |
|
45 | 57 |
|
@@ -168,6 +180,114 @@ def skill():
|
168 | 180 | return jsonify({})
|
169 | 181 |
|
170 | 182 |
|
| 183 | +@app.route('/resume/project', methods=['GET', 'POST', 'PUT', 'DELETE']) |
| 184 | +def project(): |
| 185 | + ''' |
| 186 | + Handles Project requests |
| 187 | + ''' |
| 188 | + def validate_id(project_id): |
| 189 | + ''' |
| 190 | + Validates the id |
| 191 | + ''' |
| 192 | + if project_id is None: |
| 193 | + raise ValueError("Missing id") |
| 194 | + |
| 195 | + if not project_id.isdigit(): |
| 196 | + raise ValueError("Invalid id") |
| 197 | + |
| 198 | + # check if the id is within the range of the project list |
| 199 | + int_id = int(project_id) |
| 200 | + if int_id < 0 or int_id >= len(data['project']): |
| 201 | + raise ValueError("Project not found") |
| 202 | + |
| 203 | + return int_id |
| 204 | + |
| 205 | + def get_project(project_id): |
| 206 | + ''' |
| 207 | + Get project by id |
| 208 | + ''' |
| 209 | + if project_id is not None: |
| 210 | + try: |
| 211 | + project_id = validate_id(project_id) |
| 212 | + return jsonify(data['project'][project_id]), 200 |
| 213 | + except ValueError as error: |
| 214 | + return jsonify({"error": str(error)}), 400 |
| 215 | + |
| 216 | + return jsonify([ |
| 217 | + {**project.__dict__, "id": str(index)} |
| 218 | + for index, project in enumerate(data['project']) |
| 219 | + ]), 200 |
| 220 | + |
| 221 | + def add_project(body): |
| 222 | + ''' |
| 223 | + Add project |
| 224 | + ''' |
| 225 | + mandatory_fields = ['title', 'description', 'technologies', 'link'] |
| 226 | + missing_fields = [field for field in mandatory_fields if field not in body] |
| 227 | + |
| 228 | + if missing_fields: |
| 229 | + return jsonify({"error": f"Missing fields: {', '.join(missing_fields)}"}), 400 |
| 230 | + |
| 231 | + new_project = Project( |
| 232 | + body['title'], |
| 233 | + body['description'], |
| 234 | + body['technologies'], |
| 235 | + body['link'] |
| 236 | + ) |
| 237 | + data['project'].append(new_project) |
| 238 | + |
| 239 | + return jsonify({**new_project.__dict__, "id": str(len(data['project']) - 1)}), 201 |
| 240 | + |
| 241 | + def edit_project(project_id, body): |
| 242 | + ''' |
| 243 | + Edit project |
| 244 | + ''' |
| 245 | + try: |
| 246 | + project_id = validate_id(project_id) |
| 247 | + except ValueError as error: |
| 248 | + return jsonify({"error": str(error)}), 400 |
| 249 | + |
| 250 | + for key, value in body.items(): |
| 251 | + if hasattr(data['project'][project_id], key): |
| 252 | + setattr(data['project'][project_id], key, value) |
| 253 | + else: |
| 254 | + return jsonify({"error": f"invalid field: {key}"}), 400 |
| 255 | + |
| 256 | + return jsonify({**data['project'][project_id].__dict__, "id": str(project_id)}), 200 |
| 257 | + |
| 258 | + def delete_project(project_id): |
| 259 | + ''' |
| 260 | + Delete project |
| 261 | + ''' |
| 262 | + try: |
| 263 | + project_id = validate_id(project_id) |
| 264 | + except ValueError as error: |
| 265 | + return jsonify({"error": str(error)}), 400 |
| 266 | + |
| 267 | + del data['project'][project_id] |
| 268 | + return jsonify({}), 204 |
| 269 | + |
| 270 | + if request.method == 'GET': |
| 271 | + project_id = request.args.get('id', None) |
| 272 | + return get_project(project_id) |
| 273 | + |
| 274 | + if request.method == 'POST': |
| 275 | + body = request.get_json() |
| 276 | + return add_project(body) |
| 277 | + |
| 278 | + if request.method == 'PUT': |
| 279 | + project_id = request.args.get('id', None) |
| 280 | + body = request.get_json() |
| 281 | + |
| 282 | + return edit_project(project_id, body) |
| 283 | + |
| 284 | + if request.method == 'DELETE': |
| 285 | + project_id = request.args.get('id', None) |
| 286 | + |
| 287 | + return delete_project(project_id) |
| 288 | + |
| 289 | + return jsonify({"error": "Unsupported request method"}), 405 |
| 290 | + |
171 | 291 | @app.route("/resume/spellcheck", methods=["POST"])
|
172 | 292 | def spellcheck():
|
173 | 293 | """
|
|
0 commit comments