use async_trait::async_trait;
use bamboo_domain::Session;
use super::errors::{SessionLoadError, SessionSaveError};
use crate::SessionRepository;
#[async_trait]
pub trait SessionAccess: Send + Sync {
async fn load_session(&self, id: &str) -> Result<Option<Session>, SessionLoadError>;
async fn load_or_create(&self, id: &str, model: &str) -> Result<Session, SessionLoadError>;
async fn load_merged(&self, id: &str) -> Result<Option<Session>, SessionLoadError>;
async fn save_session(&self, session: &mut Session) -> Result<(), SessionSaveError>;
async fn save_and_cache(&self, session: &mut Session) -> Result<(), SessionSaveError>;
}
#[async_trait]
impl SessionAccess for SessionRepository {
async fn load_session(&self, id: &str) -> Result<Option<Session>, SessionLoadError> {
match SessionRepository::load(self, id).await {
Some(session) => Ok(Some(session)),
None => Err(SessionLoadError::NotFound(id.to_string())),
}
}
async fn load_or_create(&self, id: &str, model: &str) -> Result<Session, SessionLoadError> {
Ok(SessionRepository::load_or_create(self, id, model).await)
}
async fn load_merged(&self, id: &str) -> Result<Option<Session>, SessionLoadError> {
Ok(SessionRepository::load_merged(self, id).await)
}
async fn save_session(&self, session: &mut Session) -> Result<(), SessionSaveError> {
self.persistence()
.merge_save_runtime(session)
.await
.map_err(|e| SessionSaveError::StorageError(e.to_string()))
}
async fn save_and_cache(&self, session: &mut Session) -> Result<(), SessionSaveError> {
SessionRepository::save_and_cache(self, session).await;
Ok(())
}
}