app: add organization + sharing commands/queries
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{LinkAccessLevel, ScopeType, ShareLink, ShareScope, ShareableType},
|
||||
errors::DomainError,
|
||||
ports::ShareRepository,
|
||||
value_objects::{DateTimeStamp, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct GenerateShareLinkCommand {
|
||||
pub shareable_type: ShareableType,
|
||||
pub shareable_id: SystemId,
|
||||
pub access_level: LinkAccessLevel,
|
||||
pub created_by: SystemId,
|
||||
pub expires_at: Option<DateTimeStamp>,
|
||||
pub max_uses: Option<u32>,
|
||||
}
|
||||
|
||||
pub struct GenerateShareLinkHandler {
|
||||
share_repo: Arc<dyn ShareRepository>,
|
||||
}
|
||||
|
||||
impl GenerateShareLinkHandler {
|
||||
pub fn new(share_repo: Arc<dyn ShareRepository>) -> Self {
|
||||
Self { share_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: GenerateShareLinkCommand) -> Result<(ShareScope, ShareLink), DomainError> {
|
||||
let scope = ShareScope::new(ScopeType::Link, cmd.shareable_type, cmd.shareable_id, cmd.created_by);
|
||||
let token = uuid::Uuid::new_v4().to_string();
|
||||
let mut link = ShareLink::new(scope.scope_id, token, cmd.access_level);
|
||||
link.expires_at = cmd.expires_at;
|
||||
link.max_uses = cmd.max_uses;
|
||||
|
||||
self.share_repo.save_scope(&scope).await?;
|
||||
self.share_repo.save_link(&link).await?;
|
||||
|
||||
Ok((scope, link))
|
||||
}
|
||||
}
|
||||
7
crates/application/src/sharing/commands/mod.rs
Normal file
7
crates/application/src/sharing/commands/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod share_resource;
|
||||
pub mod generate_share_link;
|
||||
pub mod revoke_share;
|
||||
|
||||
pub use share_resource::{ShareResourceCommand, ShareResourceHandler};
|
||||
pub use generate_share_link::{GenerateShareLinkCommand, GenerateShareLinkHandler};
|
||||
pub use revoke_share::{RevokeShareCommand, RevokeShareHandler};
|
||||
39
crates/application/src/sharing/commands/revoke_share.rs
Normal file
39
crates/application/src/sharing/commands/revoke_share.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, ShareRepository},
|
||||
value_objects::{DateTimeStamp, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct RevokeShareCommand {
|
||||
pub scope_id: SystemId,
|
||||
pub revoked_by: SystemId,
|
||||
}
|
||||
|
||||
pub struct RevokeShareHandler {
|
||||
share_repo: Arc<dyn ShareRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl RevokeShareHandler {
|
||||
pub fn new(share_repo: Arc<dyn ShareRepository>, event_pub: Arc<dyn EventPublisher>) -> Self {
|
||||
Self { share_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: RevokeShareCommand) -> Result<(), DomainError> {
|
||||
self.share_repo.find_scope_by_id(&cmd.scope_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Share scope {} not found", cmd.scope_id)))?;
|
||||
|
||||
self.share_repo.delete_scope(&cmd.scope_id).await?;
|
||||
|
||||
self.event_pub.publish(DomainEvent::ShareRevoked {
|
||||
scope_id: cmd.scope_id,
|
||||
revoked_by: cmd.revoked_by,
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
51
crates/application/src/sharing/commands/share_resource.rs
Normal file
51
crates/application/src/sharing/commands/share_resource.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{ScopeType, ShareScope, ShareTarget, ShareableType, TargetType},
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, ShareRepository},
|
||||
value_objects::{DateTimeStamp, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ShareResourceCommand {
|
||||
pub shareable_type: ShareableType,
|
||||
pub shareable_id: SystemId,
|
||||
pub target_type: TargetType,
|
||||
pub target_id: SystemId,
|
||||
pub role_id: SystemId,
|
||||
pub created_by: SystemId,
|
||||
}
|
||||
|
||||
pub struct ShareResourceHandler {
|
||||
share_repo: Arc<dyn ShareRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl ShareResourceHandler {
|
||||
pub fn new(share_repo: Arc<dyn ShareRepository>, event_pub: Arc<dyn EventPublisher>) -> Self {
|
||||
Self { share_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ShareResourceCommand) -> Result<(ShareScope, ShareTarget), DomainError> {
|
||||
let scope_type = match cmd.target_type {
|
||||
TargetType::User => ScopeType::User,
|
||||
TargetType::Group => ScopeType::Group,
|
||||
};
|
||||
|
||||
let scope = ShareScope::new(scope_type, cmd.shareable_type, cmd.shareable_id, cmd.created_by);
|
||||
let target = ShareTarget::new(scope.scope_id, cmd.target_type, cmd.target_id, cmd.role_id);
|
||||
|
||||
self.share_repo.save_scope(&scope).await?;
|
||||
self.share_repo.save_target(&target).await?;
|
||||
|
||||
self.event_pub.publish(DomainEvent::ShareCreated {
|
||||
scope_id: scope.scope_id,
|
||||
shareable_id: cmd.shareable_id,
|
||||
created_by: cmd.created_by,
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
|
||||
Ok((scope, target))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user