use std::collections::BTreeMap;
use chrono::{DateTime, Utc};
use keepsake::{RelationDefinition, RelationId, RelationSpec, SubjectRef};
use uuid::Uuid;
use super::{
AppliedKeepsake, KeepsakeRepository, NoopRelationCache, RelationCache, RepositoryResult,
TimedExpiryCandidate,
};
#[derive(Debug, Clone, Copy)]
pub struct TimedKeepsakeRepository<'repo, C = NoopRelationCache> {
pub(super) repository: &'repo KeepsakeRepository<C>,
pub(super) at: DateTime<Utc>,
}
impl<'repo, C> TimedKeepsakeRepository<'repo, C>
where
C: RelationCache,
{
#[must_use]
pub const fn repository(&self) -> &'repo KeepsakeRepository<C> {
self.repository
}
#[must_use]
pub const fn timestamp(&self) -> DateTime<Utc> {
self.at
}
pub async fn upsert_relation(
&self,
relation: &RelationDefinition,
) -> RepositoryResult<RelationDefinition> {
self.repository.upsert_relation(relation, self.at).await
}
pub async fn upsert_relation_spec<Spec>(&self) -> RepositoryResult<RelationDefinition>
where
Spec: RelationSpec,
{
self.repository.upsert_relation_spec::<Spec>(self.at).await
}
pub async fn set_relation_enabled(
&self,
relation_id: RelationId,
enabled: bool,
) -> RepositoryResult<bool> {
self.repository
.set_relation_enabled(relation_id, enabled, self.at)
.await
}
pub async fn apply(
&self,
subject: &SubjectRef,
relation_id: RelationId,
metadata: &BTreeMap<String, String>,
) -> RepositoryResult<AppliedKeepsake> {
self.repository
.apply(subject, relation_id, self.at, metadata)
.await
}
pub async fn apply_spec<Spec>(
&self,
subject: &SubjectRef,
metadata: &BTreeMap<String, String>,
) -> RepositoryResult<AppliedKeepsake>
where
Spec: RelationSpec,
{
self.repository
.apply_spec::<Spec>(subject, self.at, metadata)
.await
}
pub async fn apply_without_metadata(
&self,
subject: &SubjectRef,
relation_id: RelationId,
) -> RepositoryResult<AppliedKeepsake> {
self.repository
.apply_without_metadata(subject, relation_id, self.at)
.await
}
pub async fn apply_spec_without_metadata<Spec>(
&self,
subject: &SubjectRef,
) -> RepositoryResult<AppliedKeepsake>
where
Spec: RelationSpec,
{
self.repository
.apply_spec_without_metadata::<Spec>(subject, self.at)
.await
}
pub async fn revoke(&self, keepsake_id: Uuid) -> RepositoryResult<bool> {
self.repository.revoke(keepsake_id, self.at).await
}
pub async fn due_timed_expiry(
&self,
limit: i64,
) -> RepositoryResult<Vec<TimedExpiryCandidate>> {
self.repository.due_timed_expiry(self.at, limit).await
}
pub async fn expire_due_timed(&self, limit: i64) -> RepositoryResult<u64> {
self.repository.expire_due_timed(self.at, limit).await
}
#[cfg(feature = "fulfillment-counters")]
pub async fn upsert_counter_projection(
&self,
keepsake_id: Uuid,
key: &str,
value: i64,
) -> RepositoryResult<()> {
self.repository
.upsert_counter_projection(keepsake_id, key, value, self.at)
.await
}
}