Skip to content

Commit

Permalink
Refactor DB get() method to return a Python object instead of a dic…
Browse files Browse the repository at this point in the history
…tionary for easier handling.

Update `check_server_details()` to align with this change

Signed-off-by: ZorEl212 <[email protected]>
  • Loading branch information
ZorEl212 committed Sep 11, 2024
1 parent 586d1d0 commit 3083c9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create_token(cls, data, sid):
@classmethod
def check_server_details(cls, user_id, serverId):
server = storage.get('Server', serverId)
if (server is None or server.get('serverId') != serverId or
server.get('userId') != user_id):
return None
if (server is None or server.id != serverId or
server.userId != user_id):
return False
return server.to_dict()
22 changes: 16 additions & 6 deletions models/engine/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,32 @@ def all(self, cls=None):
return docs

def new(self, obj):
collection = self.database[obj.get_cls_name()]
collection = self.database[obj.cls_name()]
collection.insert_one(obj.to_dict())

def delete(self, obj):
collection = self.database[obj.get_cls_name()]
collection = self.database[obj.cls_name()]
collection.delete_one({'id': obj.id})

def get(self, cls, id):
collection = self.database[cls]
if isinstance(cls, str):
cls = next((c for c in self.classes if c.get_cls_name() == cls), None)

if cls is None:
return None

collection = self.database[cls.get_cls_name()]
document = collection.find_one({'id': id})

if document is not None:
document.pop('_id')
return document
document.pop('_id')
document.pop('cls_name')
return cls(**document)

return None # Return None if document is not found

def update(self, obj):
collection = self.database[obj.get_cls_name()]
collection = self.database[obj.cls_name()]
collection.update_one({'id': obj.id}, {'$set': obj.to_dict()})
return obj

Expand Down

0 comments on commit 3083c9e

Please sign in to comment.