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

Improvements: Move json data variable to a json object file #38

Merged
merged 15 commits into from
Oct 14, 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
168 changes: 60 additions & 108 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,13 @@
"""
Flask Application
"""

from flask import Flask, jsonify, request
from models import Experience, Education, Project, Skill, User
from utils import get_suggestion, check_phone_number, correct_spelling

from models import Experience, Education, Skill, Project
from utils import check_phone_number, correct_spelling, get_suggestion, load_data

app = Flask(__name__)

data = {
"user": [User("Jackie Stewart", "+4478322678", "[email protected]")],
"experience": [
Experience(
"Software Developer",
"A Cool Company",
"October 2022",
"Present",
"Writing Python Code",
"example-logo.png",
),
Experience(
"Intern",
"A Nice Company",
"October 2021",
"December 2021",
"Writing Scripts",
"example-logo.png",
),
],
"education": [
Education(
"Computer Science",
"University of Tech",
"September 2019",
"July 2022",
"80%",
"example-logo.png",
)
],
"skill": [
Skill("Python",
"1-2 Years",
"example-logo.png")
],
"project": [
Project(
title="Sample Project",
description="A sample project",
technologies=["Python", "Flask"],
link="https://github.com/username/sample-project"
)
]
}

data = load_data('data/resume.json')

@app.route("/test")
def hello_world():
Expand All @@ -64,59 +18,56 @@ def hello_world():


@app.route("/resume/user", methods=["GET", "POST", "PUT"])
def user():
def user_route():
"""
Handles User information
Handle GET, POST, and PUT requests for user data.
GET: Retrieve all users
POST: Create a new user
PUT: Update an existing user
"""
if request.method == 'GET':
return jsonify(data['user']), 200

# defining sub function to reduce number of returns
def get_users():
return jsonify([user.__dict__ for user in data["user"]]), 200

def add_user(body):
# retrieve user's information.
name = body["name"]
phone_number = body["phone_number"]
email = body["email_address"]
# store the new user information.
if not check_phone_number(phone_number):
return jsonify({"error": "Incorrect phone number !"}), 400
new_user = User(name, phone_number, email)
data["user"].append(new_user)
return jsonify(new_user.__dict__), 201

# edit the user information.
def edit_user(body):
name = body["name"]
phone_number = body["phone_number"]
email = body["email_address"]
for i, user_ in enumerate(data["user"]):
if user_.email_address == email:
if not check_phone_number(phone_number):
return jsonify({"error": "Incorrect phone number !"}), 400
data["user"][i] = User(name, phone_number, email)
return jsonify(data["user"][i].__dict__), 200
return jsonify({"error": "User not found !"}), 404
body = request.get_json()
if not body or not all(key in body for key in ['name', 'phone_number', 'email_address']):
return jsonify({"error": "Missing required fields"}), 400

if request.method == "GET":
return get_users()
if request.method == "POST":
body = request.get_json()
return add_user(body)
if request.method == "PUT":
body = request.get_json()
return edit_user(body)
return jsonify({"error": "Unsupported request method !"}), 405
name = body['name']
phone_number = body['phone_number']
email = body['email_address']
if not check_phone_number(phone_number):
return jsonify({"error": "Incorrect phone number"}), 400

if request.method == 'POST':
# Create a new user and add it to the data
new_user = {
'name': name,
'phone_number': phone_number,
'email_address': email
}
data['user'].append(new_user)
return jsonify(new_user), 201

# Handle PUT request
for i, current_user in enumerate(data['user']):
if current_user['email_address'] == email:
data['user'][i] = {
'name': name,
'phone_number': phone_number,
'email_address': email
}
return jsonify(data['user'][i]), 200

return jsonify({"error": "User not found !"}), 404


@app.route("/resume/experience", methods=["GET", "POST", 'PUT'])
def experience():
"""
Handle experience requests
"""
if request.method == 'GET':
return jsonify(
{"experience": [exp.__dict__ for exp in data["experience"]]})
if request.method == "GET":
return jsonify({"experience": list(data["experience"])}), 200

if request.method == "POST":
new_experience = request.json
Expand All @@ -130,7 +81,7 @@ def experience():
new_exp["logo"],
)
data["experience"].append(experience_instance)
return jsonify({"id": len(data["experience"]) - 1})
return jsonify({"id": len(data["experience"]) - 1}), 201

if request.method == 'PUT':
body = request.get_json()
Expand All @@ -145,10 +96,9 @@ def experience():

new_experience_order.append(
Experience(title, company, start_date, end_date, description, logo)
)
)
data['experience'] = new_experience_order

return_data = [item.__dict__ for item in data['experience']]
return_data = list(data['experience'])
return jsonify(return_data), 200

return jsonify({"error": "Unsupported request method !"}), 405
Expand All @@ -158,9 +108,9 @@ def education():
"""
Handles education requests
"""
if request.method == 'GET':
return jsonify(
{"education": [edu.__dict__ for edu in data["education"]]})

if request.method == "GET":
return jsonify({"education": list(data["education"])}), 200

if request.method == "POST":
new_education = request.json
Expand All @@ -174,7 +124,7 @@ def education():
new_edu["logo"],
)
data["education"].append(education_instance)
return jsonify({"id": len(data["education"]) - 1})
return jsonify({"id": len(data["education"]) - 1}), 201

if request.method == 'PUT':
body = request.get_json()
Expand All @@ -189,9 +139,10 @@ def education():
new_education_order.append(Education(course, school, start_date, end_date, grade, logo))
data['education'] = new_education_order

return_data = [item.__dict__ for item in data['education']]
return_data = list(data['education'])
return jsonify(return_data), 200
return jsonify({})
return jsonify({}), 405



@app.route("/resume/skill", methods=["GET", "POST", 'PUT'])
Expand All @@ -201,7 +152,7 @@ def skill():
"""

if request.method == "GET":
return jsonify({"skills": [skill.__dict__ for skill in data["skill"]]})
return jsonify({"skills": list(data["skill"])}), 200

if request.method == "POST":
new_skill = request.json
Expand All @@ -212,7 +163,7 @@ def skill():
skill_data["logo"]
)
data["skill"].append(skill_instance)
return jsonify({"id": len(data["skill"]) - 1})
return jsonify({"id": len(data["skill"]) - 1}), 201

if request.method == 'PUT':
body = request.get_json()
Expand All @@ -227,7 +178,7 @@ def skill():
return_data = [item.__dict__ for item in data['skill']]
return jsonify(return_data), 200

return jsonify({})
return jsonify({}), 405


@app.route('/resume/project', methods=['GET', 'POST', 'PUT', 'DELETE'])
Expand All @@ -245,7 +196,7 @@ def validate_id(project_id):
if not project_id.isdigit():
raise ValueError("Invalid id")

# check if the id is within the range of the project list
# Check if the id is within the range of the project list
int_id = int(project_id)
if int_id < 0 or int_id >= len(data['project']):
raise ValueError("Project not found")
Expand All @@ -264,7 +215,7 @@ def get_project(project_id):
return jsonify({"error": str(error)}), 400

return jsonify([
{**project.__dict__, "id": str(index)}
{**project, "id": str(index)}
for index, project in enumerate(data['project'])
]), 200

Expand Down Expand Up @@ -301,7 +252,7 @@ def edit_project(project_id, body):
if hasattr(data['project'][project_id], key):
setattr(data['project'][project_id], key, value)
else:
return jsonify({"error": f"invalid field: {key}"}), 400
return jsonify({"error": f"Invalid field: {key}"}), 400

return jsonify({**data['project'][project_id].__dict__, "id": str(project_id)}), 200

Expand Down Expand Up @@ -338,6 +289,7 @@ def delete_project(project_id):

return jsonify({"error": "Unsupported request method"}), 405


@app.route("/resume/spellcheck", methods=["POST"])
def spellcheck():
"""
Expand All @@ -363,4 +315,4 @@ def get_description_suggestion():
if not description or not description_type:
return jsonify({"error": "Description and type are required"}), 400
suggestion = get_suggestion(description, description_type)
return jsonify({"suggestion": suggestion})
return jsonify({"suggestion": suggestion}), 200
44 changes: 44 additions & 0 deletions data/resume.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"user": [
{
"name": "Jackie Stewart",
"phone_number": "+4478322678",
"email_address": "[email protected]"
}
],
"experience": [
{
"title": "Software Developer",
"company": "A Cool Company",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing Python Code",
"logo": "example-logo.png"
}
],
"education": [
{
"degree": "Computer Science",
"institution": "University of Tech",
"start_date": "September 2019",
"end_date": "July 2022",
"grade": "80%",
"logo": "example-logo.png"
}
],
"skill": [
{
"name": "Python",
"experience": "1-2 Years",
"logo": "example-logo.png"
}
],
"project": [
{
"title": "My Title",
"description": "A sample project",
"technologies": ["Python", "Flask"],
"link": "https://github.com/username/sample-project"
}
]
}
Loading
Loading