Nattyboi's picture
added some routes
4f7effb
raw
history blame
5.51 kB
from gamification.objects import UserFeedback, UserLevel, UserPoints
from bson import ObjectId
from pymongo import MongoClient
import os
from typing import List
from dotenv import load_dotenv
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
# levels
def create_level_func(document:UserLevel,)->bool:
"""
Creates a UserLevel document in the MongoDB collection.
:param UserLevel: Actually accepts an Instance of UserLevel and deserializes it here
:param maxPoints: Maximum Amount of points for this level.
:param minPoints: Minimum Amount of points for this level.
:param levelNumber: Number for this level.
:param careerPath: Career Path Special for this level.
"""
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Level"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
should_proceed= (collection.find_one(document.model_dump()))
# Insert the document
if should_proceed==None:
if document!=None:
result = collection.insert_one(document.model_dump())
return True
else:
client.close()
return False
else:
# The exact document already exists so it can't be created again
return False
def get_all_levels_func() -> List[UserLevel]:
# MongoDB URI and configuration
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Level"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
# Fetch all documents from the collection
levels_cursor = collection.find() # This returns a cursor to the documents
# Convert the cursor to a list of UserLevel objects
levels = [UserLevel(**level) for level in levels_cursor]
return levels
def delete_level_func(level_id,)->bool:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Level"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
if isinstance(level_id, str):
level_id = ObjectId(level_id)
result = collection.delete_one(filter={"_id":level_id})
if result.acknowledged==True:
if result.deleted_count ==0:
return False
else:
return True
else:
return False
def edit_level_func(level_id, **kwargs):
"""
Edit a UserLevel document in the MongoDB collection.
:param level_id: The ObjectId or unique identifier of the UserLevel to edit.
:param maxPoints: Maximum Amount of points for this level.
:param minPoints: Minimum Amount of points for this level.
:param levelNumber: Number for this level.
:param careerPath: Career Path Special for this level.
"""
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Level"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
# Ensure that `level_id` is an ObjectId if using ObjectId as the identifier
if isinstance(level_id, str):
level_id = ObjectId(level_id)
# Prepare the update data
update_data = {key: value for key, value in kwargs.items() if value is not None}
# Perform the update operation in the collection
result = collection.update_one(
{"_id": level_id}, # Find the document by its unique identifier (ObjectId or other)
{"$set": update_data} # Update the document with the new values
)
return result.modified_count
# points
def create_points_func(document:UserPoints)->bool:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
# Insert the document
if document!=None:
result = collection.insert_one(document.model_dump())
return True
else:
client.close()
return False
def get_all_points_func() -> List[UserFeedback]:
# MongoDB URI and configuration
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
# Fetch all documents from the collection
point_cursor = collection.find() # This returns a cursor to the documents
# Convert the cursor to a list of UserLevel objects
points = [UserFeedback(**point) for point in point_cursor]
return points
# feedback
def create_feedback_func(document:UserFeedback)->bool:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Feedback"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
# Insert the document
if document!=None:
result = collection.insert_one(document.model_dump())
return True
else:
client.close()
return False
def get_all_feedback_func() -> List[UserFeedback]:
# MongoDB URI and configuration
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Feedback"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
# Fetch all documents from the collection
feedback_cursor = collection.find() # This returns a cursor to the documents
# Convert the cursor to a list of UserLevel objects
feedbacks = [UserFeedback(**feedback) for feedback in feedback_cursor]
return feedbacks