app: add storage commands/queries + missing in-memory test repos
This commit is contained in:
45
crates/application/src/storage/queries/check_quota.rs
Normal file
45
crates/application/src/storage/queries/check_quota.rs
Normal 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("a, query.usage_type, current, query.requested_amount))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user