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,92 @@
use std::sync::Arc;
use application::testing::InMemoryAlbumRepository;
use application::organization::{
AlbumAction, CreateAlbumCommand, CreateAlbumHandler,
ManageAlbumEntriesCommand, ManageAlbumEntriesHandler,
};
use domain::errors::DomainError;
use domain::value_objects::SystemId;
async fn setup_album(repo: &Arc<InMemoryAlbumRepository>, creator: SystemId) -> SystemId {
let handler = CreateAlbumHandler::new(repo.clone());
let album = handler.execute(CreateAlbumCommand {
title: "Test Album".into(),
creator_id: creator,
}).await.unwrap();
album.album_id
}
#[tokio::test]
async fn adds_asset_to_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let user = SystemId::new();
let album_id = setup_album(&repo, user).await;
let asset_id = SystemId::new();
let handler = ManageAlbumEntriesHandler::new(repo.clone());
let album = handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await.unwrap();
assert_eq!(album.asset_count(), 1);
assert_eq!(album.entries[0].asset_id, asset_id);
}
#[tokio::test]
async fn removes_asset_from_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let user = SystemId::new();
let album_id = setup_album(&repo, user).await;
let asset_id = SystemId::new();
let handler = ManageAlbumEntriesHandler::new(repo.clone());
handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await.unwrap();
let album = handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Remove { asset_id },
user_id: user,
}).await.unwrap();
assert_eq!(album.asset_count(), 0);
}
#[tokio::test]
async fn rejects_nonexistent_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let handler = ManageAlbumEntriesHandler::new(repo);
let result = handler.execute(ManageAlbumEntriesCommand {
album_id: SystemId::new(),
action: AlbumAction::Add { asset_id: SystemId::new() },
user_id: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}
#[tokio::test]
async fn rejects_duplicate_add() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let user = SystemId::new();
let album_id = setup_album(&repo, user).await;
let asset_id = SystemId::new();
let handler = ManageAlbumEntriesHandler::new(repo.clone());
handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await.unwrap();
let result = handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}

View File

@@ -1 +1,3 @@
mod create_album;
mod manage_album_entries;
mod tag_asset;

View File

@@ -0,0 +1,81 @@
use std::sync::Arc;
use application::testing::{InMemoryAssetRepository, InMemoryTagRepository};
use application::organization::{TagAssetCommand, TagAssetHandler};
use domain::entities::{Asset, AssetType, SourceReference};
use domain::errors::DomainError;
use domain::ports::{AssetRepository, TagRepository};
use domain::value_objects::{Checksum, SystemId};
async fn seed_asset(repo: &Arc<InMemoryAssetRepository>) -> SystemId {
let owner = SystemId::new();
let asset = Asset::new(
SourceReference {
volume_id: SystemId::new(),
relative_path: "photos/test.jpg".into(),
checksum: Checksum::new("a".repeat(64)).unwrap(),
},
AssetType::Image,
"image/jpeg",
1024,
owner,
);
let id = asset.asset_id;
repo.save(&asset).await.unwrap();
id
}
#[tokio::test]
async fn tags_asset_creates_new_tag() {
let asset_repo = Arc::new(InMemoryAssetRepository::new());
let tag_repo = Arc::new(InMemoryTagRepository::new());
let asset_id = seed_asset(&asset_repo).await;
let user = SystemId::new();
let handler = TagAssetHandler::new(asset_repo, tag_repo);
let (tag, asset_tag) = handler.execute(TagAssetCommand {
asset_id,
tag_name: "sunset".into(),
user_id: user,
}).await.unwrap();
assert_eq!(tag.name, "sunset");
assert_eq!(asset_tag.asset_id, asset_id);
assert_eq!(asset_tag.tag_id, tag.tag_id);
assert_eq!(asset_tag.tagged_by_user_id, Some(user));
}
#[tokio::test]
async fn reuses_existing_tag() {
let asset_repo = Arc::new(InMemoryAssetRepository::new());
let tag_repo = Arc::new(InMemoryTagRepository::new());
let asset_id = seed_asset(&asset_repo).await;
let user = SystemId::new();
// Pre-create tag
let existing = domain::entities::Tag::new_manual("sunset");
let existing_id = existing.tag_id;
tag_repo.save_tag(&existing).await.unwrap();
let handler = TagAssetHandler::new(asset_repo, tag_repo);
let (tag, _) = handler.execute(TagAssetCommand {
asset_id,
tag_name: "sunset".into(),
user_id: user,
}).await.unwrap();
assert_eq!(tag.tag_id, existing_id);
}
#[tokio::test]
async fn rejects_nonexistent_asset() {
let asset_repo = Arc::new(InMemoryAssetRepository::new());
let tag_repo = Arc::new(InMemoryTagRepository::new());
let handler = TagAssetHandler::new(asset_repo, tag_repo);
let result = handler.execute(TagAssetCommand {
asset_id: SystemId::new(),
tag_name: "sunset".into(),
user_id: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}