fix: new_id in store_manager generated a bad id, only returns the actual data now

This commit is contained in:
Christoph J. Scherr 2025-02-25 15:22:05 +01:00
parent e4cabf9964
commit 180d12d68b
No known key found for this signature in database
GPG key ID: 9EB784BB202BB7BB

View file

@ -17,11 +17,11 @@ class StoreManager:
self._db = TinyDB(path_to_db) self._db = TinyDB(path_to_db)
def new_id(self) -> UUID: def new_id(self) -> UUID:
unix_timestamp: int = int(time.time())
_guard: int = 0 _guard: int = 0
while True: while True:
unix_timestamp: int = int(time.time())
id = uuid1(node=None, clock_seq=unix_timestamp) id = uuid1(node=None, clock_seq=unix_timestamp)
if len(self._query(Query().id == id)) > 0: if len(self._query(Query().id == id)) < 1:
break break
_guard += 1 _guard += 1
if _guard > 100: if _guard > 100:
@ -33,18 +33,18 @@ class StoreManager:
return self._db.search(query) return self._db.search(query)
def load(self, key: UUID) -> Optional[dict]: def load(self, key: UUID) -> Optional[dict]:
results = self._query(Query().id == key) results = self._query(Query().id == str(key))
if len(results) < 1: if len(results) < 1:
raise Exception("foobar") raise Exception("foobar")
elif len(results) > 2: elif len(results) > 2:
raise KeyError("The requested item did not exist in our database") raise KeyError("The requested item did not exist in our database")
else: else:
return results[0] return results[0]["data"]
def save(self, data: dict) -> UUID: def save(self, data: dict) -> UUID:
id = self.new_id() id = self.new_id()
self._db.insert({ self._db.insert({
"id": id, "id": str(id),
"data": data "data": data
}) })
return id return id