|
1 |
| -from crud import CRUDStrategy |
2 |
| - |
3 |
| - |
4 |
| -class UserCRUD(CRUDStrategy): |
5 |
| - """ |
6 |
| - Implements CRUD operations specifically for user documents in a MongoDB collection. |
7 |
| -
|
8 |
| - This class inherits from CRUDStrategy and provides concrete implementations of the |
9 |
| - create, read, update, and delete operations for managing user documents within the |
10 |
| - 'users' collection of MongoDB. |
11 |
| -
|
12 |
| - Methods: |
13 |
| - create(data): Inserts a new user document into the collection. |
14 |
| - read(query): Retrieves a user document from the collection based on a query. |
15 |
| - update(query, data): Updates a user document in the collection. |
16 |
| - delete(query): Deletes a user document from the collection. |
17 |
| - """ |
18 |
| - def __init__(self): |
19 |
| - """Initializes the UserCRUD class to work with the 'users' collection.""" |
20 |
| - super().__init__("users") |
21 |
| - |
22 |
| - def create(self, data): |
23 |
| - """ |
24 |
| - Inserts a new user document into the collection. |
25 |
| -
|
26 |
| - Args: |
27 |
| - data (dict): The user data to insert. |
28 |
| -
|
29 |
| - Returns: |
30 |
| - InsertOneResult: The result object which includes the ID of the newly inserted document. |
31 |
| - """ |
32 |
| - return self.collection.insert_one(data) |
33 |
| - |
34 |
| - def read(self, query): |
35 |
| - """ |
36 |
| - Retrieves a user document from the collection based on a query. |
37 |
| -
|
38 |
| - Args: |
39 |
| - query (dict): The query to select the document. |
40 |
| -
|
41 |
| - Returns: |
42 |
| - dict: The first document found matching the query. None if no document matches. |
43 |
| - """ |
44 |
| - return self.collection.find_one(query) |
45 |
| - |
46 |
| - def update(self, query, data): |
47 |
| - """ |
48 |
| - Updates a user document in the collection. |
49 |
| -
|
50 |
| - Args: |
51 |
| - query (dict): The query to select the document for update. |
52 |
| - data (dict): The data to update in the selected document. |
53 |
| -
|
54 |
| - Returns: |
55 |
| - UpdateResult: The result object of the update operation. |
56 |
| - """ |
57 |
| - return self.collection.update_one(query, {"$set": data}) |
58 |
| - |
59 |
| - def delete(self, query): |
60 |
| - """ |
61 |
| - Deletes a user document from the collection. |
62 |
| -
|
63 |
| - Args: |
64 |
| - query (dict): The query to select the document for deletion. |
65 |
| -
|
66 |
| - Returns: |
67 |
| - DeleteResult: The result object of the delete operation. |
68 |
| - """ |
69 |
| - return self.collection.delete_one(query) |
70 |
| - |
71 |
| - |
72 |
| -if __name__ == "__main__": |
73 |
| - # Using the class directly |
74 |
| - user_crud = UserCRUD() |
75 |
| - |
76 |
| - # Creating a user |
77 |
| - user_id = user_crud.create( |
78 |
| - { "username": "jonhdoe", "name": "John Doe", "email": "[email protected]"} |
79 |
| - ) |
80 |
| - |
81 |
| - # Reading the newly created user |
82 |
| - user = user_crud.read({"_id": user_id}) |
83 |
| - print(user) |
84 |
| - |
85 |
| - # Updating the user's name |
86 |
| - user_crud.update({"_id": user_id}, {"name": "John Dollar"}) |
87 |
| - user = user_crud.read({"_id": user_id}) |
88 |
| - print(user) |
89 |
| - |
90 |
| - # Deleting the user |
91 |
| - user_crud.delete({"_id": user_id}) |
92 |
| - user = user_crud.read({"_id": user_id}) |
93 |
| - print(user) |
| 1 | +from crud import CRUDStrategy |
| 2 | + |
| 3 | + |
| 4 | +class UserCRUD(CRUDStrategy): |
| 5 | + """ |
| 6 | + Implements CRUD operations specifically for user documents in a MongoDB collection. |
| 7 | +
|
| 8 | + This class inherits from CRUDStrategy and provides concrete implementations of the |
| 9 | + create, read, update, and delete operations for managing user documents within the |
| 10 | + 'users' collection of MongoDB. |
| 11 | +
|
| 12 | + Methods: |
| 13 | + create(data): Inserts a new user document into the collection. |
| 14 | + read(query): Retrieves a user document from the collection based on a query. |
| 15 | + update(query, data): Updates a user document in the collection. |
| 16 | + delete(query): Deletes a user document from the collection. |
| 17 | + """ |
| 18 | + def __init__(self): |
| 19 | + """Initializes the UserCRUD class to work with the 'users' collection.""" |
| 20 | + super().__init__("users") |
| 21 | + |
| 22 | + def create(self, data): |
| 23 | + """ |
| 24 | + Inserts a new user document into the collection. |
| 25 | +
|
| 26 | + Args: |
| 27 | + data (dict): The user data to insert. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + InsertOneResult: The result object which includes the ID of the newly inserted document. |
| 31 | + """ |
| 32 | + return self.collection.insert_one(data) |
| 33 | + |
| 34 | + def read(self, query): |
| 35 | + """ |
| 36 | + Retrieves a user document from the collection based on a query. |
| 37 | +
|
| 38 | + Args: |
| 39 | + query (dict): The query to select the document. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + dict: The first document found matching the query. None if no document matches. |
| 43 | + """ |
| 44 | + return self.collection.find_one(query) |
| 45 | + |
| 46 | + def update(self, query, data): |
| 47 | + """ |
| 48 | + Updates a user document in the collection. |
| 49 | +
|
| 50 | + Args: |
| 51 | + query (dict): The query to select the document for update. |
| 52 | + data (dict): The data to update in the selected document. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + UpdateResult: The result object of the update operation. |
| 56 | + """ |
| 57 | + return self.collection.update_one(query, {"$set": data}) |
| 58 | + |
| 59 | + def delete(self, query): |
| 60 | + """ |
| 61 | + Deletes a user document from the collection. |
| 62 | +
|
| 63 | + Args: |
| 64 | + query (dict): The query to select the document for deletion. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + DeleteResult: The result object of the delete operation. |
| 68 | + """ |
| 69 | + return self.collection.delete_one(query) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + # Using the class directly |
| 74 | + user_crud = UserCRUD() |
| 75 | + |
| 76 | + # Creating a user |
| 77 | + user_id = user_crud.create( |
| 78 | + { "username": "jonhdoe", "name": "John Doe", "email": "[email protected]"} |
| 79 | + ).inserted_id |
| 80 | + |
| 81 | + # Reading the newly created user |
| 82 | + user = user_crud.read({"_id": user_id}) |
| 83 | + print(user) |
| 84 | + |
| 85 | + # Updating the user's name |
| 86 | + user_crud.update({"_id": user_id}, {"name": "John Dollar"}) |
| 87 | + user = user_crud.read({"_id": user_id}) |
| 88 | + print(user) |
| 89 | + |
| 90 | + # Deleting the user |
| 91 | + user_crud.delete({"_id": user_id}) |
| 92 | + user = user_crud.read({"_id": user_id}) |
| 93 | + print(user) |
0 commit comments