app: add storage commands/queries + missing in-memory test repos

This commit is contained in:
2026-05-31 05:11:02 +02:00
parent fa36bb8c0e
commit 4549d746c3
16 changed files with 1040 additions and 5 deletions

View File

@@ -0,0 +1,45 @@
use std::sync::Arc;
use domain::{
entities::UsageType,
errors::DomainError,
ports::{QuotaRepository, UsageLedgerRepository},
storage::services::{check_quota, QuotaCheckResult},
value_objects::SystemId,
};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CheckQuotaQuery {
pub user_id: SystemId,
pub usage_type: UsageType,
pub requested_amount: u64,
}
pub struct CheckQuotaHandler {
quota_repo: Arc<dyn QuotaRepository>,
ledger_repo: Arc<dyn UsageLedgerRepository>,
}
impl CheckQuotaHandler {
pub fn new(
quota_repo: Arc<dyn QuotaRepository>,
ledger_repo: Arc<dyn UsageLedgerRepository>,
) -> Self {
Self { quota_repo, ledger_repo }
}
pub async fn execute(&self, query: CheckQuotaQuery) -> Result<QuotaCheckResult, DomainError> {
let quota = self.quota_repo.find_by_owner(&query.user_id).await?;
let Some(quota) = quota else {
return Ok(QuotaCheckResult {
allowed: true,
current_usage: 0,
limit: 0,
is_unlimited: true,
});
};
let current = self.ledger_repo.sum_usage(&query.user_id, query.usage_type, None).await?;
Ok(check_quota(&quota, query.usage_type, current, query.requested_amount))
}
}