async def update_user(idd: str, data: dict): if len(data) < 1: return False users_collection = get_db_client().get_collection("users") user = await users_collection.find_one({"_id": ObjectId(idd)}) if user: updated_user = await users_collection.update_one( {"_id": ObjectId(idd)}, {"$set": data}) if updated_user: return True return False
async def update_document(idd: str, data: dict): if len(data) < 1: return False doc_collection = get_db_client() doc = await doc_collection.find_one({"_id": ObjectId(idd)}) if doc: updated_document = await doc_collection.update_one( {"_id": ObjectId(idd)}, {"$set": data} ) if updated_document: return True return False
async def add_document(file, current_user) -> MDLDocument: s3_client = s3_auth() file_url = upload_file_to_bucket(s3_client, file.file, Settings.S3_BUCKET, "folder", object_name=file.filename) document_collection = get_db_client().get_collection("documents") doc_data = { "uuid": uuid4(), "uploader_sub": current_user.sub, "uploaded_by": current_user.name, "filename": file.filename, "destination_uri": file_url, "tags": ["mechanical"], } doc = await document_collection.insert_one(doc_data) new_document = await document_collection.find_one({"uuid": doc_data["uuid"]}) return MDLDocument(**new_document)
async def delete_document(idd: str): doc_collection = get_db_client() doc = await doc_collection.find_one({"_id": ObjectId(idd)}) if doc: await doc_collection.delete_one({"_id": ObjectId(idd)}) return True
async def retrieve_document(idd: str) -> MDLDocument: document_collection = get_db_client().get_collection("documents") doc = await document_collection.find_one({"uuid": idd}) if doc: return MDLDocument(**doc)
async def retrieve_documents(): document_collection = get_db_client().get_collection("documents") docs = [] async for doc in document_collection.find(): docs.append(MDLDocument(**doc)) return docs
async def retrieve_users(): users_collection = get_db_client().get_collection("users") users = [] async for user in users_collection.find(): users.append(MDLUser(**user)) return users
async def delete_user(idd: str): users_collection = get_db_client().get_collection("users") user = await users_collection.find_one({"_id": ObjectId(idd)}) if user: await users_collection.delete_one({"_id": ObjectId(idd)}) return True
async def retrieve_user(idd: str) -> MDLUser: users_collection = get_db_client().get_collection("users") user = await users_collection.find_one({"sub": idd}) if user: return MDLUser(**user)
async def add_user(user_data: dict) -> MDLUser: users_collection = get_db_client().get_collection("users") user = await users_collection.insert_one(user_data) new_user = await users_collection.find_one({"sub": user.sub}) return MDLUser(**new_user)