Spaces:
Paused
Paused
Update backend/mongodb/operations/users.py
Browse files
backend/mongodb/operations/users.py
CHANGED
@@ -7,69 +7,63 @@ from bson import ObjectId
|
|
7 |
import re
|
8 |
|
9 |
|
10 |
-
def
|
11 |
-
test = request.app.database["user_records"]
|
12 |
-
print(type(test))
|
13 |
-
return test
|
14 |
-
|
15 |
-
|
16 |
-
def create_user(request: Request, user: User = Body(...)):
|
17 |
user = jsonable_encoder(user)
|
18 |
-
new_user =
|
19 |
-
created_user =
|
20 |
print("NEW ID IS:.........", new_user.inserted_id)
|
21 |
return created_user
|
22 |
|
23 |
|
24 |
-
def list_users(
|
25 |
try:
|
26 |
-
users = list(
|
27 |
return users
|
28 |
except:
|
29 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No users found!")
|
30 |
|
31 |
|
32 |
-
def find_user(
|
33 |
-
if (user :=
|
34 |
return user
|
35 |
else:
|
36 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with user_id {user_id} not found!")
|
37 |
|
38 |
|
39 |
-
def find_user_name(
|
40 |
# search for name in lowercase
|
41 |
-
if (user :=
|
42 |
return user
|
43 |
else:
|
44 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with name {name} not found!")
|
45 |
|
46 |
|
47 |
-
def find_user_email(
|
48 |
-
if (user :=
|
49 |
return user
|
50 |
else:
|
51 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with Email Address {email} not found!")
|
52 |
|
53 |
|
54 |
''' Update user record based on user object/json'''
|
55 |
-
def update_user(
|
56 |
try:
|
57 |
user = {k: v for k, v in user.model_dump().items() if v is not None}
|
58 |
if len(user) >= 1:
|
59 |
-
update_result =
|
60 |
|
61 |
if update_result.modified_count == 0:
|
62 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with user_id: '{user_id}' not found and updated!")
|
63 |
|
64 |
-
if (existing_users :=
|
65 |
return existing_users
|
66 |
except:
|
67 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with user_id: '{user_id}' not found and updated!")
|
68 |
|
69 |
|
70 |
-
def delete_user(
|
71 |
try:
|
72 |
-
deleted_user =
|
73 |
|
74 |
if deleted_user.deleted_count == 1:
|
75 |
return f"User with user_id {user_id} deleted sucessfully"
|
|
|
7 |
import re
|
8 |
|
9 |
|
10 |
+
def create_user(collection, user: User = Body(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
user = jsonable_encoder(user)
|
12 |
+
new_user = collection.insert_one(user)
|
13 |
+
created_user = collection.find_one({"_id": new_user.inserted_id})
|
14 |
print("NEW ID IS:.........", new_user.inserted_id)
|
15 |
return created_user
|
16 |
|
17 |
|
18 |
+
def list_users(collection, limit: int):
|
19 |
try:
|
20 |
+
users = list(collection.find(limit = limit))
|
21 |
return users
|
22 |
except:
|
23 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No users found!")
|
24 |
|
25 |
|
26 |
+
def find_user(collection, user_id: str):
|
27 |
+
if (user := collection.find_one({"user_id": user_id})):
|
28 |
return user
|
29 |
else:
|
30 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with user_id {user_id} not found!")
|
31 |
|
32 |
|
33 |
+
def find_user_name(collection, name: str):
|
34 |
# search for name in lowercase
|
35 |
+
if (user := collection.find_one({"name": re.compile('^' + re.escape(name) + '$', re.IGNORECASE)})):
|
36 |
return user
|
37 |
else:
|
38 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with name {name} not found!")
|
39 |
|
40 |
|
41 |
+
def find_user_email(collection, email: str):
|
42 |
+
if (user := collection.find_one({"email": re.compile('^' + re.escape(email) + '$', re.IGNORECASE)})):
|
43 |
return user
|
44 |
else:
|
45 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with Email Address {email} not found!")
|
46 |
|
47 |
|
48 |
''' Update user record based on user object/json'''
|
49 |
+
def update_user(collection, user_id: str, user: UpdateUser):
|
50 |
try:
|
51 |
user = {k: v for k, v in user.model_dump().items() if v is not None}
|
52 |
if len(user) >= 1:
|
53 |
+
update_result = collection.update_one({"user_id": user_id}, {"$set": user})
|
54 |
|
55 |
if update_result.modified_count == 0:
|
56 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with user_id: '{user_id}' not found and updated!")
|
57 |
|
58 |
+
if (existing_users := collection.find_one({"user_id": user_id})) is not None:
|
59 |
return existing_users
|
60 |
except:
|
61 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with user_id: '{user_id}' not found and updated!")
|
62 |
|
63 |
|
64 |
+
def delete_user(collection, user_id: str):
|
65 |
try:
|
66 |
+
deleted_user = collection.delete_one({"user_id": user_id})
|
67 |
|
68 |
if deleted_user.deleted_count == 1:
|
69 |
return f"User with user_id {user_id} deleted sucessfully"
|