-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_operations.py
115 lines (89 loc) · 3.34 KB
/
db_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from bson.objectid import ObjectId
from pymongo.errors import DuplicateKeyError
from env_setup import get_database
dbname = get_database()
users_collection = dbname["users"]
rooms_collection = dbname["rooms"]
rooms_collection.create_index("room_name", unique=True)
users_collection.create_index("username", unique=True)
def insert_user(username, password):
"""
Handles the 'login' route. It validates the input fields, checks the username and password,
and returns a JSON response with an access token.
:return: If an error occurs, returns a JSON response with an error message.
"""
try:
users_collection.insert_one(
{
"username": username,
"password": password,
}
)
except DuplicateKeyError:
return None
def get_user_rooms(username):
"""
Handles the 'login' route. It validates the input fields, checks the username and password,
and returns a JSON response with an access token.
:return: If an error occurs, returns a JSON response with an error message.
"""
return rooms_collection.find({"$or": [{"creator": username}, {"users": username}]})
def get_room(room_id, username=None):
"""
Retrieves a room by its ID. If a username is provided, it also checks that the user is the creator of the room.
:param room_id: The ID of the room.
:param username: The username of the user (optional).
:return: The room document, or None if no such room exists.
"""
if username is not None:
return rooms_collection.find_one(
{"_id": ObjectId(room_id), "creator": username}
)
return rooms_collection.find_one({"_id": ObjectId(room_id)})
def delete_room(room_id):
"""
Deletes a room by its ID.
:param room_id: The ID of the room.
:return: The result of the delete operation.
"""
return rooms_collection.delete_one({"_id": ObjectId(room_id)})
def get_user(username):
"""
Retrieves a user by their username.
:param username: The username of the user.
:return: The user document, or None if no such user exists.
"""
return users_collection.find_one({"username": username})
def insert_room(room_name, username):
"""
Inserts a new room into the rooms collection.
:param room_name: The name of the room.
:param username: The username of the user who is creating the room.
:return: The result of the insert operation, or None if a room with the same name already exists.
"""
try:
return rooms_collection.insert_one(
{"room_name": room_name, "users": [username], "creator": username}
)
except DuplicateKeyError:
return None
def insert_user_in_room(room_id, username):
"""
Adds a user to a room.
:param room_id: The ID of the room.
:param username: The username of the user.
:return: The result of the update operation.
"""
return rooms_collection.update_one(
{"_id": ObjectId(room_id)}, {"$addToSet": {"users": username}}
)
def remove_user_from_room(room_id, username):
"""
Removes a user from a room.
:param room_id: The ID of the room.
:param username: The username of the user.
:return: The result of the update operation.
"""
return rooms_collection.update_one(
{"_id": ObjectId(room_id)}, {"$pull": {"users": username}}
)