2026-07-16 12:32:17 +07:00
|
|
|
//! Session management use-cases.
|
|
|
|
|
//!
|
2026-07-19 17:05:27 +07:00
|
|
|
//! `SessionServiceImpl` implements [`SessionService`] by delegating to
|
2026-07-16 12:32:17 +07:00
|
|
|
//! injected repository implementations, keeping the orchestration logic
|
|
|
|
|
//! independent of any concrete persistence mechanism.
|
2026-07-19 17:05:27 +07:00
|
|
|
//!
|
|
|
|
|
//! # Flow
|
|
|
|
|
//!
|
|
|
|
|
//! - **`create_session`** — generates a UUID v4 id, creates a `Session` entity,
|
|
|
|
|
//! delegates persistence to `SessionRepository`.
|
|
|
|
|
//! - **`list_all`** — delegates to `SessionRepository::list_sessions`.
|
|
|
|
|
//! - **`archive_session`** — loads session, sets `archived = true`, persists.
|
|
|
|
|
//!
|
|
|
|
|
//! # Components
|
|
|
|
|
//!
|
|
|
|
|
//! - `SessionServiceImpl<R, L>` — service over two generic repositories
|
|
|
|
|
//! - `new` / `create_session` / `list_all` / `archive_session` — lifecycle ops
|
2026-07-16 12:32:17 +07:00
|
|
|
use std::path::PathBuf;
|
2026-07-20 06:39:30 +07:00
|
|
|
use zesdex_entities::domain::auth::SessionId;
|
|
|
|
|
use zesdex_utils::CastOr;
|
2026-07-19 17:05:27 +07:00
|
|
|
use tracing;
|
2026-07-16 12:32:17 +07:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2026-07-20 06:14:23 +07:00
|
|
|
use crate::domain::error::ServiceError;
|
2026-07-16 12:32:17 +07:00
|
|
|
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> {
|
2026-07-19 17:05:27 +07:00
|
|
|
/// Repository for session CRUD operations.
|
2026-07-16 12:32:17 +07:00
|
|
|
pub session_repo: R,
|
2026-07-19 17:05:27 +07:00
|
|
|
/// Repository for session lock acquire/release.
|
2026-07-16 12:32:17 +07:00
|
|
|
pub lock_repo: L,
|
2026-07-19 17:05:27 +07:00
|
|
|
/// Base data directory passed to repository methods.
|
2026-07-16 12:32:17 +07:00
|
|
|
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> {
|
2026-07-20 06:14:23 +07:00
|
|
|
fn create_session(&self, title: &str) -> Result<Session, ServiceError> {
|
2026-07-20 06:39:30 +07:00
|
|
|
let id = SessionId::new(&Uuid::new_v4().to_string())
|
|
|
|
|
.expect("UUID is always a valid session id");
|
2026-07-16 12:32:17 +07:00
|
|
|
let title_owned = if title.is_empty() {
|
|
|
|
|
"New Session".to_string()
|
|
|
|
|
} else {
|
|
|
|
|
title.to_string()
|
|
|
|
|
};
|
2026-07-20 06:39:30 +07:00
|
|
|
let session = Session::new(id.into_string(), title_owned);
|
2026-07-19 17:05:27 +07:00
|
|
|
tracing::debug!(session_id = %session.id, title = %session.title, "creating new session");
|
2026-07-20 06:14:23 +07:00
|
|
|
self.session_repo
|
|
|
|
|
.save_session(&self.base_dir, &session)?; // RepositoryError → ServiceError via From
|
2026-07-16 12:32:17 +07:00
|
|
|
Ok(session)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 06:14:23 +07:00
|
|
|
fn list_all(&self) -> Result<Vec<Session>, ServiceError> {
|
2026-07-19 17:05:27 +07:00
|
|
|
tracing::debug!("listing all sessions");
|
2026-07-20 06:14:23 +07:00
|
|
|
self.session_repo
|
|
|
|
|
.list_sessions(&self.base_dir)
|
|
|
|
|
.map_err(ServiceError::Repository)
|
2026-07-16 12:32:17 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-20 06:39:30 +07:00
|
|
|
fn archive_session(&self, id: SessionId) -> Result<(), ServiceError> {
|
2026-07-19 17:05:27 +07:00
|
|
|
tracing::debug!(session_id = %id, "archiving session");
|
2026-07-20 06:14:23 +07:00
|
|
|
let mut session = self.session_repo
|
2026-07-20 06:39:30 +07:00
|
|
|
.load_session(&self.base_dir, &id)?; // RepositoryError → ServiceError
|
2026-07-16 12:32:17 +07:00
|
|
|
session.archived = true;
|
2026-07-20 06:14:23 +07:00
|
|
|
let millis = std::time::SystemTime::now()
|
2026-07-16 12:32:17 +07:00
|
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
2026-07-20 06:14:23 +07:00
|
|
|
.as_millis();
|
2026-07-20 06:39:30 +07:00
|
|
|
session.updated_at = millis.cast_or(i64::MAX);
|
2026-07-20 06:14:23 +07:00
|
|
|
self.session_repo
|
|
|
|
|
.save_session(&self.base_dir, &session)?; // RepositoryError → ServiceError
|
2026-07-16 12:32:17 +07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|