Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamBrem committed Oct 4, 2023
1 parent a496ddb commit 3c6fc79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
19 changes: 10 additions & 9 deletions views/familyViews.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

familyBP = Blueprint("familyBP", __name__)


@familyBP.route("/family/get_family_id", methods=["GET"])
def get_family_id():
data = request.get_json()
Expand Down Expand Up @@ -36,24 +37,25 @@ def create_family():
collection = mongo.db.families

# Check if the family already exists with the given student_id and parent_id - this may need to be changed if students have more than one family
existing_family = collection.find_one({"students": student_id, "parents": parent_id})
existing_family = collection.find_one(
{"students": student_id, "parents": parent_id}
)
if existing_family:
return jsonify({"message": "Family with this student and parent already exists"}), 400
return (
jsonify({"message": "Family with this student and parent already exists"}),
400,
)


# Create a new family document with the provided student and parent
new_family = {
"students": [student_id],
"parents": [parent_id]
}
new_family = {"students": [student_id], "parents": [parent_id]}

result = collection.insert_one(new_family)
return jsonify({"message": "Family Created Successfully"}), 201


# now make a route that adds a student to an existing family
@familyBP.route("/family/update_family", methods=["PUT"])
def update_family():

data = request.get_json()
user_id = data.get("user_id")
role = data.get("role")
Expand All @@ -75,6 +77,5 @@ def update_family():
collection.update_one({"_id": family_id}, {"$addToSet": {"parents": user_id}})
else:
return jsonify({"message": "Invalid role"}), 400


return jsonify({"message": "Family Updated Successfully"}), 201
27 changes: 15 additions & 12 deletions views/userViews.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
userBP = Blueprint("userBP", __name__)


@app.route('/user/get_user_id', methods=['GET'])
@app.route("/user/get_user_id", methods=["GET"])
def get_student_id():
data = request.get_json()
firstname = data.get("firstname")
lastname = data.get("lastname")
role = data.get("role")

#select the collection based on the role
# select the collection based on the role
if role == "student":
collection = mongo.db.students
elif role == "teacher":
Expand All @@ -25,16 +25,15 @@ def get_student_id():
return jsonify({"message": "Invalid role"}), 400

# Query the MongoDB collection to find the student by first and last name
user_data = collection.find_one({'firstname': firstname, 'lastname': lastname})
user_data = collection.find_one({"firstname": firstname, "lastname": lastname})

if user_data:
user_id = str(user_data['_id'])
user_id = str(user_data["_id"])
return f"The ID of {firstname} {lastname} is {user_id}"
else:
return f"No user found with the name {firstname} {lastname}"



@userBP.route("/user", methods=["POST"])
def create_user():
data = request.get_json()
Expand All @@ -48,30 +47,35 @@ def create_user():

if not firstname or not lastname or not email:
return jsonify({"message": "Missing required fields"}), 400

print("ROLE:", role)

if role == "teacher":
# create new teacher
collection = mongo.db.teachers
new_user = Teacher(firstname, lastname, email, password, date_of_birth)
new_user = Teacher(firstname, lastname, email, password, date_of_birth)
elif role == "student":
# create student
collection = mongo.db.students
new_user = Student(firstname, lastname, email, password, date_of_birth, data.get("parent_id"), data.get("grade"))
new_user = Student(
firstname,
lastname,
email,
password,
date_of_birth,
data.get("parent_id"),
data.get("grade"),
)
elif role == "parent":
collection = mongo.db.parents
new_user = Parent(firstname, lastname, email, password, date_of_birth)
else:
return jsonify({"message": "Invalid role"}), 400


result = collection.insert_one(new_user.__dict__)
return jsonify({"message": f"{role} Created Successfully"}), 201




# Token generation function
def generate_token(username):
payload = {
Expand All @@ -91,7 +95,6 @@ def login():
password = data.get("password")



# Protected route
# API Endpoints: In a RESTful API, the /protected route might represent a specific API endpoint
# that provides access to a resource. For instance, it could be used to retrieve a list of user-specific items,
Expand Down

0 comments on commit 3c6fc79

Please sign in to comment.