feat: add get_latest_haiku_or_default to store

Resf: OPS-65
This commit is contained in:
Christoph J. Scherr 2025-03-23 14:09:08 +01:00
parent 2c7394edfa
commit 69db48af8b
No known key found for this signature in database
GPG key ID: 9EB784BB202BB7BB
2 changed files with 15 additions and 1 deletions

View file

@ -7,7 +7,8 @@ from typing import Optional
from tinydb import TinyDB
from tinydb.queries import QueryImpl
from senju.haiku import Haiku
from senju import haiku
from senju.haiku import DEFAULT_HAIKU, Haiku
DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json")
@ -51,3 +52,12 @@ class StoreManager:
except IndexError as e:
self.logger.error(f"The database seems to be empty: {e}")
return None
def get_latest_haiku_or_default(self) -> Haiku:
id = self.get_id_of_latest_haiku()
if id is None:
return DEFAULT_HAIKU
haiku = self.load_haiku(id)
if haiku is None:
return DEFAULT_HAIKU
return haiku