app: add organization + sharing commands/queries
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::Album,
|
||||
errors::DomainError,
|
||||
ports::AlbumRepository,
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub enum AlbumAction {
|
||||
Add { asset_id: SystemId },
|
||||
Remove { asset_id: SystemId },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ManageAlbumEntriesCommand {
|
||||
pub album_id: SystemId,
|
||||
pub action: AlbumAction,
|
||||
pub user_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct ManageAlbumEntriesHandler {
|
||||
album_repo: Arc<dyn AlbumRepository>,
|
||||
}
|
||||
|
||||
impl ManageAlbumEntriesHandler {
|
||||
pub fn new(album_repo: Arc<dyn AlbumRepository>) -> Self {
|
||||
Self { album_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ManageAlbumEntriesCommand) -> Result<Album, DomainError> {
|
||||
let mut album = self.album_repo.find_by_id(&cmd.album_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Album {} not found", cmd.album_id)))?;
|
||||
|
||||
match cmd.action {
|
||||
AlbumAction::Add { asset_id } => album.add_asset(asset_id, cmd.user_id)?,
|
||||
AlbumAction::Remove { asset_id } => album.remove_asset(&asset_id)?,
|
||||
}
|
||||
|
||||
self.album_repo.save(&album).await?;
|
||||
Ok(album)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
pub mod create_album;
|
||||
pub mod manage_album_entries;
|
||||
pub mod tag_asset;
|
||||
|
||||
pub use create_album::{CreateAlbumCommand, CreateAlbumHandler};
|
||||
pub use manage_album_entries::{AlbumAction, ManageAlbumEntriesCommand, ManageAlbumEntriesHandler};
|
||||
pub use tag_asset::{TagAssetCommand, TagAssetHandler};
|
||||
|
||||
44
crates/application/src/organization/commands/tag_asset.rs
Normal file
44
crates/application/src/organization/commands/tag_asset.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{AssetTag, Tag},
|
||||
errors::DomainError,
|
||||
ports::{AssetRepository, TagRepository},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct TagAssetCommand {
|
||||
pub asset_id: SystemId,
|
||||
pub tag_name: String,
|
||||
pub user_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct TagAssetHandler {
|
||||
asset_repo: Arc<dyn AssetRepository>,
|
||||
tag_repo: Arc<dyn TagRepository>,
|
||||
}
|
||||
|
||||
impl TagAssetHandler {
|
||||
pub fn new(asset_repo: Arc<dyn AssetRepository>, tag_repo: Arc<dyn TagRepository>) -> Self {
|
||||
Self { asset_repo, tag_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: TagAssetCommand) -> Result<(Tag, AssetTag), DomainError> {
|
||||
self.asset_repo.find_by_id(&cmd.asset_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Asset {} not found", cmd.asset_id)))?;
|
||||
|
||||
let tag = match self.tag_repo.find_by_name(&cmd.tag_name).await? {
|
||||
Some(existing) => existing,
|
||||
None => {
|
||||
let new_tag = Tag::new_manual(&cmd.tag_name);
|
||||
self.tag_repo.save_tag(&new_tag).await?;
|
||||
new_tag
|
||||
}
|
||||
};
|
||||
|
||||
let asset_tag = AssetTag::new_manual(cmd.asset_id, tag.tag_id, cmd.user_id);
|
||||
self.tag_repo.save_asset_tag(&asset_tag).await?;
|
||||
|
||||
Ok((tag, asset_tag))
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
pub mod commands;
|
||||
pub mod queries;
|
||||
|
||||
pub use commands::{CreateAlbumCommand, CreateAlbumHandler};
|
||||
pub use commands::{AlbumAction, ManageAlbumEntriesCommand, ManageAlbumEntriesHandler};
|
||||
pub use commands::{TagAssetCommand, TagAssetHandler};
|
||||
pub use queries::get_album::{GetAlbumQuery, GetAlbumHandler};
|
||||
|
||||
27
crates/application/src/organization/queries/get_album.rs
Normal file
27
crates/application/src/organization/queries/get_album.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::Album,
|
||||
errors::DomainError,
|
||||
ports::AlbumRepository,
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct GetAlbumQuery {
|
||||
pub album_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct GetAlbumHandler {
|
||||
album_repo: Arc<dyn AlbumRepository>,
|
||||
}
|
||||
|
||||
impl GetAlbumHandler {
|
||||
pub fn new(album_repo: Arc<dyn AlbumRepository>) -> Self {
|
||||
Self { album_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, query: GetAlbumQuery) -> Result<Album, DomainError> {
|
||||
self.album_repo.find_by_id(&query.album_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Album {} not found", query.album_id)))
|
||||
}
|
||||
}
|
||||
1
crates/application/src/organization/queries/mod.rs
Normal file
1
crates/application/src/organization/queries/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod get_album;
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -1 +1,7 @@
|
||||
// Sharing commands/queries (future: CreateShareLink, ManageAccess, etc.)
|
||||
pub mod commands;
|
||||
pub mod queries;
|
||||
|
||||
pub use commands::{ShareResourceCommand, ShareResourceHandler};
|
||||
pub use commands::{GenerateShareLinkCommand, GenerateShareLinkHandler};
|
||||
pub use commands::{RevokeShareCommand, RevokeShareHandler};
|
||||
pub use queries::access_shared_resource::{AccessSharedResourceQuery, AccessSharedResourceHandler};
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{LinkAccessLevel, ShareScope},
|
||||
errors::DomainError,
|
||||
ports::ShareRepository,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct AccessSharedResourceQuery {
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
pub struct AccessSharedResourceHandler {
|
||||
share_repo: Arc<dyn ShareRepository>,
|
||||
}
|
||||
|
||||
impl AccessSharedResourceHandler {
|
||||
pub fn new(share_repo: Arc<dyn ShareRepository>) -> Self {
|
||||
Self { share_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, query: AccessSharedResourceQuery) -> Result<(ShareScope, LinkAccessLevel), DomainError> {
|
||||
let mut link = self.share_repo.find_link_by_token(&query.token).await?
|
||||
.ok_or_else(|| DomainError::NotFound("Share link not found".to_string()))?;
|
||||
|
||||
if !link.is_valid() {
|
||||
return Err(DomainError::Forbidden("Link expired or exhausted".to_string()));
|
||||
}
|
||||
|
||||
link.record_use();
|
||||
self.share_repo.save_link(&link).await?;
|
||||
|
||||
let scope = self.share_repo.find_scope_by_id(&link.scope_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound("Share scope not found".to_string()))?;
|
||||
|
||||
Ok((scope, link.access_level))
|
||||
}
|
||||
}
|
||||
1
crates/application/src/sharing/queries/mod.rs
Normal file
1
crates/application/src/sharing/queries/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod access_shared_resource;
|
||||
Reference in New Issue
Block a user