app: add organization + sharing commands/queries

This commit is contained in:
2026-05-31 05:17:51 +02:00
parent 536bf3463a
commit d1394ce7bb
29 changed files with 740 additions and 1 deletions

View File

@@ -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)
}
}

View File

@@ -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};

View 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))
}
}

View File

@@ -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};

View 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)))
}
}

View File

@@ -0,0 +1 @@
pub mod get_album;