async def get_user(_id: str, cli: AsyncIOMotorClient) -> BaseModel: if not bson.ObjectId.is_valid(_id): return UserOut() row = await cli[mongo_db]['users'].find_one({'_id': bson.ObjectId(_id)}) if not row: raise HTTPException(status_code=404, detail="Item not found") row['id'] = str(row['_id']) return UserOut(**row)
async def update_user(_id: str, api_user: UserBase, cli: AsyncIOMotorClient) -> BaseModel: if not bson.ObjectId.is_valid(_id): return UserOut() row = await cli[mongo_db]['users'].find_one_and_update( {'_id': bson.ObjectId(_id)}, {'$set': api_user.dict()}, return_document=ReturnDocument.AFTER) if not row: raise HTTPException(status_code=404, detail="Item not found") row['id'] = str(row['_id']) return UserOut(**row)
def get_user(uid): try: user = auth.get_user(uid) except firebase_admin._auth_utils.UserNotFoundError: return "User not found", None except Exception as e: return "Firebase Error: " + str(e), None m_user = db.users.find_one({"uid": uid}) # 최초 등록시 if not m_user: db.users.update( {"uid": uid}, { "uid": uid, "user_name": user.display_name, "point": 0 }, upsert=True, ) m_user = db.users.find_one({"uid": uid}) m_user = UserOut.parse_obj(m_user) return "success", m_user
async def create_user(api_user: UserIn, cli: AsyncIOMotorClient) -> BaseModel: hashed_password = generate_hash(api_user.password) api_user = UserInDB(**api_user.dict(), created_at=epoch_time(), last_login=0, hashed_password=hashed_password) row = await cli[mongo_db]['users'].insert_one(api_user.dict()) return UserOut(**api_user.dict(), id=str(row.inserted_id))
async def organization_user_list(organization_name: str): print( db.users.find({ "organization_name": organization_name }).sort("point", -1)) return [ UserOut(**i) for i in db.users.find({ "organization_name": organization_name }).sort("point", -1) ]
async def get_users(skip: int, take: int, cli: AsyncIOMotorClient) -> [BaseModel]: rows = await cli[mongo_db]['users'].find({}).sort( 'created_at', pymongo.DESCENDING).skip(skip).limit(take).to_list(length=take) result = [] for row in rows: row['id'] = str(row['_id']) result.append(UserOut(**row)) return result
async def get_user_by_id(user_id: int) -> UserOut: query = str(users.select().where(users.c.id == 'id_1')) values = {'id_1': user_id} row = await db.fetch_one(query=str(query), values=values) user_out = UserOut(id=int(row[0]), name=str(row[1])) return user_out
async def organization_user_list(organization: PyObjectId): print(db.users.find({"organization": organization}).sort("point", -1)) return [ UserOut(**i) for i in db.users.find({"organization": organization}).sort("point", -1) ]