61 lines
2.1 KiB
Rust
61 lines
2.1 KiB
Rust
//! Session management use-cases.
|
|||
|
|
//!
|
||
|
|
//! `SessionServiceImpl` implements `SessionService` by delegating to
|
||
|
|
//! injected repository implementations, keeping the orchestration logic
|
||
|
|
//! independent of any concrete persistence mechanism.
|
||
|
|
use std::path::PathBuf;
|
||
|
|
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
use crate::domain::repository::{SessionLockRepository, SessionRepository};
|
||
|
|
use crate::domain::service::SessionService;
|
||
|
|
use crate::domain::session::Session;
|
||
|
|
|
||
|
|
/// Concrete session service backed by generic repository implementations.
|
||
|
|
pub struct SessionServiceImpl<R: SessionRepository, L: SessionLockRepository> {
|
||
|
|
pub session_repo: R,
|
||
|
|
pub lock_repo: L,
|
||
|
|
pub base_dir: PathBuf,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<R: SessionRepository, L: SessionLockRepository> SessionServiceImpl<R, L> {
|
||
|
|
/// Create a new session service with the given repositories and base
|
||
|
|
/// data directory.
|
||
|
|
pub fn new(session_repo: R, lock_repo: L, base_dir: PathBuf) -> Self {
|
||
|
|
SessionServiceImpl {
|
||
|
|
session_repo,
|
||
|
|
lock_repo,
|
||
|
|
base_dir,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<R: SessionRepository, L: SessionLockRepository> SessionService for SessionServiceImpl<R, L> {
|
||
|
|
fn create_session(&self, title: &str) -> anyhow::Result<Session> {
|
||
|
|
let id = Uuid::new_v4().to_string();
|
||
|
|
let title_owned = if title.is_empty() {
|
||
|
|
"New Session".to_string()
|
||
|
|
} else {
|
||
|
|
title.to_string()
|
||
|
|
};
|
||
|
|
let session = Session::new(id, title_owned);
|
||
|
|
self.session_repo.save_session(&self.base_dir, &session)?;
|
||
|
|
Ok(session)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn list_all(&self) -> anyhow::Result<Vec<Session>> {
|
||
|
|
self.session_repo.list_sessions(&self.base_dir)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn archive_session(&self, id: &str) -> anyhow::Result<()> {
|
||
|
|
let mut session = self.session_repo.load_session(&self.base_dir, id)?;
|
||
|
|
session.archived = true;
|
||
|
|
session.updated_at = std::time::SystemTime::now()
|
||
|
|
.duration_since(std::time::UNIX_EPOCH)
|
||
|
|
.unwrap_or_default()
|
||
|
|
.as_millis() as i64;
|
||
|
|
self.session_repo.save_session(&self.base_dir, &session)?;
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
}
|