refactor: restructure application to CQRS, update api-types + presentation

- application: replace flat use_cases/ with identity/{commands,queries}/ and organization/commands/
- each use case now split into Command/Query struct + Handler struct
- api-types: add username to RegisterRequest/UserResponse, add CreateAlbumRequest/AlbumResponse
- presentation: update state, handlers, factory to use new handler types
- tests: restructured to match CQRS module layout, added get_profile tests
This commit is contained in:
2026-05-31 05:00:34 +02:00
parent d62d8157a8
commit fa36bb8c0e
43 changed files with 305 additions and 168 deletions

View File

@@ -11,3 +11,4 @@ thiserror = { workspace = true }
uuid = { workspace = true }
tokio = { workspace = true }
bytes = { workspace = true }
serde = { workspace = true }

View File

@@ -0,0 +1 @@
// Catalog commands/queries (future: SearchAssets, UpdateMetadata, etc.)

View File

@@ -6,13 +6,19 @@ use domain::{
value_objects::Email,
};
pub struct LoginUser {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LoginUserCommand {
pub email: String,
pub password: String,
}
pub struct LoginUserHandler {
repo: Arc<dyn UserRepository>,
hasher: Arc<dyn PasswordHasher>,
issuer: Arc<dyn TokenIssuer>,
}
impl LoginUser {
impl LoginUserHandler {
pub fn new(
repo: Arc<dyn UserRepository>,
hasher: Arc<dyn PasswordHasher>,
@@ -21,11 +27,11 @@ impl LoginUser {
Self { repo, hasher, issuer }
}
pub async fn execute(&self, email: &str, password: &str) -> Result<(User, String), DomainError> {
let email = Email::new(email)?;
pub async fn execute(&self, cmd: LoginUserCommand) -> Result<(User, String), DomainError> {
let email = Email::new(&cmd.email)?;
let user = self.repo.find_by_email(&email).await?
.ok_or_else(|| DomainError::Unauthorized("Invalid credentials".to_string()))?;
let valid = self.hasher.verify(password, &user.password_hash).await?;
let valid = self.hasher.verify(&cmd.password, &user.password_hash).await?;
if !valid {
return Err(DomainError::Unauthorized("Invalid credentials".to_string()));
}

View File

@@ -0,0 +1,5 @@
pub mod register_user;
pub mod login_user;
pub use register_user::{RegisterUserCommand, RegisterUserHandler};
pub use login_user::{LoginUserCommand, LoginUserHandler};

View File

@@ -0,0 +1,45 @@
use std::sync::Arc;
use domain::{
entities::User,
errors::DomainError,
ports::{PasswordHasher, UserRepository},
value_objects::Email,
};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RegisterUserCommand {
pub username: String,
pub email: String,
pub password: String,
}
pub struct RegisterUserHandler {
user_repo: Arc<dyn UserRepository>,
hasher: Arc<dyn PasswordHasher>,
}
impl RegisterUserHandler {
pub fn new(user_repo: Arc<dyn UserRepository>, hasher: Arc<dyn PasswordHasher>) -> Self {
Self { user_repo, hasher }
}
pub async fn execute(&self, cmd: RegisterUserCommand) -> Result<User, DomainError> {
if cmd.username.is_empty() {
return Err(DomainError::Validation("Username must not be empty".to_string()));
}
if cmd.password.len() < 8 {
return Err(DomainError::Validation("Password must be at least 8 characters".to_string()));
}
let email = Email::new(&cmd.email)?;
if self.user_repo.find_by_email(&email).await?.is_some() {
return Err(DomainError::Conflict(format!("Email {} is already registered", email.as_str())));
}
if self.user_repo.find_by_username(&cmd.username).await?.is_some() {
return Err(DomainError::Conflict(format!("Username {} is already taken", cmd.username)));
}
let hash = self.hasher.hash(&cmd.password).await?;
let user = User::new(&cmd.username, email, hash);
self.user_repo.save(&user).await?;
Ok(user)
}
}

View File

@@ -0,0 +1,5 @@
pub mod commands;
pub mod queries;
pub use commands::{RegisterUserCommand, RegisterUserHandler, LoginUserCommand, LoginUserHandler};
pub use queries::{GetProfileQuery, GetProfileHandler};

View File

@@ -0,0 +1,22 @@
use std::sync::Arc;
use domain::{entities::User, errors::DomainError, ports::UserRepository, value_objects::SystemId};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct GetProfileQuery {
pub user_id: SystemId,
}
pub struct GetProfileHandler {
repo: Arc<dyn UserRepository>,
}
impl GetProfileHandler {
pub fn new(repo: Arc<dyn UserRepository>) -> Self {
Self { repo }
}
pub async fn execute(&self, query: GetProfileQuery) -> Result<User, DomainError> {
self.repo.find_by_id(&query.user_id).await?
.ok_or_else(|| DomainError::NotFound(format!("User {} not found", query.user_id)))
}
}

View File

@@ -0,0 +1,3 @@
pub mod get_profile;
pub use get_profile::{GetProfileQuery, GetProfileHandler};

View File

@@ -1,2 +1,8 @@
pub mod identity;
pub mod organization;
pub mod storage;
pub mod catalog;
pub mod sharing;
pub mod sidecar;
pub mod processing;
pub mod testing;
pub mod use_cases;

View File

@@ -6,20 +6,26 @@ use domain::{
value_objects::SystemId,
};
pub struct CreateAlbum {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateAlbumCommand {
pub title: String,
pub creator_id: SystemId,
}
pub struct CreateAlbumHandler {
album_repo: Arc<dyn AlbumRepository>,
}
impl CreateAlbum {
impl CreateAlbumHandler {
pub fn new(album_repo: Arc<dyn AlbumRepository>) -> Self {
Self { album_repo }
}
pub async fn execute(&self, title: &str, creator_id: SystemId) -> Result<Album, DomainError> {
if title.is_empty() {
pub async fn execute(&self, cmd: CreateAlbumCommand) -> Result<Album, DomainError> {
if cmd.title.is_empty() {
return Err(DomainError::Validation("Album title must not be empty".to_string()));
}
let album = Album::new(title, creator_id);
let album = Album::new(&cmd.title, cmd.creator_id);
self.album_repo.save(&album).await?;
Ok(album)
}

View File

@@ -0,0 +1,3 @@
pub mod create_album;
pub use create_album::{CreateAlbumCommand, CreateAlbumHandler};

View File

@@ -0,0 +1,3 @@
pub mod commands;
pub use commands::{CreateAlbumCommand, CreateAlbumHandler};

View File

@@ -0,0 +1 @@
// Processing commands/queries (future: EnqueueJob, ProcessBatch, etc.)

View File

@@ -0,0 +1 @@
// Sharing commands/queries (future: CreateShareLink, ManageAccess, etc.)

View File

@@ -0,0 +1 @@
// Sidecar commands/queries (future: SyncSidecar, ExportMetadata, etc.)

View File

@@ -0,0 +1 @@
// Storage commands/queries (future: IngestAsset, ManageVolume, etc.)

View File

@@ -1 +0,0 @@
// Catalog use cases (future: SearchAssets, UpdateMetadata, etc.)

View File

@@ -1,15 +0,0 @@
use std::sync::Arc;
use domain::{entities::User, errors::DomainError, ports::UserRepository, value_objects::SystemId};
pub struct GetProfile {
repo: Arc<dyn UserRepository>,
}
impl GetProfile {
pub fn new(repo: Arc<dyn UserRepository>) -> Self { Self { repo } }
pub async fn execute(&self, user_id: &SystemId) -> Result<User, DomainError> {
self.repo.find_by_id(user_id).await?
.ok_or_else(|| DomainError::NotFound(format!("User {user_id} not found")))
}
}

View File

@@ -1,7 +0,0 @@
pub mod register_user;
pub mod login_user;
pub mod get_profile;
pub use register_user::RegisterUser;
pub use login_user::LoginUser;
pub use get_profile::GetProfile;

View File

@@ -1,38 +0,0 @@
use std::sync::Arc;
use domain::{
entities::User,
errors::DomainError,
ports::{PasswordHasher, UserRepository},
value_objects::Email,
};
pub struct RegisterUser {
repo: Arc<dyn UserRepository>,
hasher: Arc<dyn PasswordHasher>,
}
impl RegisterUser {
pub fn new(repo: Arc<dyn UserRepository>, hasher: Arc<dyn PasswordHasher>) -> Self {
Self { repo, hasher }
}
pub async fn execute(&self, username: &str, email: &str, password: &str) -> Result<User, DomainError> {
if username.is_empty() {
return Err(DomainError::Validation("Username must not be empty".to_string()));
}
if password.len() < 8 {
return Err(DomainError::Validation("Password must be at least 8 characters".to_string()));
}
let email = Email::new(email)?;
if self.repo.find_by_email(&email).await?.is_some() {
return Err(DomainError::Conflict(format!("Email {} is already registered", email.as_str())));
}
if self.repo.find_by_username(username).await?.is_some() {
return Err(DomainError::Conflict(format!("Username {username} is already taken")));
}
let hash = self.hasher.hash(password).await?;
let user = User::new(username, email, hash);
self.repo.save(&user).await?;
Ok(user)
}
}

View File

@@ -1,10 +0,0 @@
pub mod identity;
pub mod organization;
pub mod storage;
pub mod catalog;
pub mod sharing;
pub mod sidecar;
pub mod processing;
pub use identity::{RegisterUser, LoginUser, GetProfile};
pub use organization::CreateAlbum;

View File

@@ -1,3 +0,0 @@
pub mod create_album;
pub use create_album::CreateAlbum;

View File

@@ -1 +0,0 @@
// Processing use cases (future: EnqueueJob, ProcessBatch, etc.)

View File

@@ -1 +0,0 @@
// Sharing use cases (future: CreateShareLink, ManageAccess, etc.)

View File

@@ -1 +0,0 @@
// Sidecar Sync use cases (future: SyncSidecar, ExportMetadata, etc.)

View File

@@ -1 +0,0 @@
// Storage use cases (future: IngestAsset, ManageVolume, etc.)

View File

@@ -1 +1,2 @@
mod use_cases;
mod identity;
mod organization;

View File

@@ -0,0 +1,77 @@
use std::sync::Arc;
use application::testing::{InMemoryUserRepository, StubPasswordHasher};
use application::identity::{RegisterUserCommand, RegisterUserHandler};
use domain::errors::DomainError;
#[tokio::test]
async fn registers_new_user() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
let cmd = RegisterUserCommand {
username: "testuser".into(),
email: "test@example.com".into(),
password: "password123".into(),
};
let user = handler.execute(cmd).await.unwrap();
assert_eq!(user.username, "testuser");
assert_eq!(user.email.as_str(), "test@example.com");
assert_eq!(repo.all().await.len(), 1);
}
#[tokio::test]
async fn rejects_duplicate_email() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
handler.execute(RegisterUserCommand {
username: "user1".into(),
email: "test@example.com".into(),
password: "password123".into(),
}).await.unwrap();
let result = handler.execute(RegisterUserCommand {
username: "user2".into(),
email: "test@example.com".into(),
password: "different1".into(),
}).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}
#[tokio::test]
async fn rejects_duplicate_username() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
handler.execute(RegisterUserCommand {
username: "sameuser".into(),
email: "a@example.com".into(),
password: "password123".into(),
}).await.unwrap();
let result = handler.execute(RegisterUserCommand {
username: "sameuser".into(),
email: "b@example.com".into(),
password: "password123".into(),
}).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}
#[tokio::test]
async fn rejects_short_password() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo, Arc::new(StubPasswordHasher));
let result = handler.execute(RegisterUserCommand {
username: "user".into(),
email: "test@example.com".into(),
password: "short".into(),
}).await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}
#[tokio::test]
async fn rejects_empty_username() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo, Arc::new(StubPasswordHasher));
let result = handler.execute(RegisterUserCommand {
username: "".into(),
email: "test@example.com".into(),
password: "password123".into(),
}).await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}

View File

@@ -0,0 +1,2 @@
mod commands;
mod queries;

View File

@@ -0,0 +1,28 @@
use std::sync::Arc;
use application::testing::{InMemoryUserRepository, StubPasswordHasher};
use application::identity::{RegisterUserCommand, RegisterUserHandler, GetProfileQuery, GetProfileHandler};
use domain::errors::DomainError;
use domain::value_objects::SystemId;
#[tokio::test]
async fn returns_existing_user() {
let repo = Arc::new(InMemoryUserRepository::new());
let reg = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
let user = reg.execute(RegisterUserCommand {
username: "alice".into(),
email: "alice@example.com".into(),
password: "password123".into(),
}).await.unwrap();
let handler = GetProfileHandler::new(repo);
let found = handler.execute(GetProfileQuery { user_id: user.id }).await.unwrap();
assert_eq!(found.username, "alice");
}
#[tokio::test]
async fn returns_not_found_for_missing_user() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = GetProfileHandler::new(repo);
let result = handler.execute(GetProfileQuery { user_id: SystemId::new() }).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}

View File

@@ -0,0 +1 @@
mod get_profile;

View File

@@ -1,15 +1,18 @@
use std::sync::Arc;
use application::testing::InMemoryAlbumRepository;
use application::use_cases::CreateAlbum;
use application::organization::{CreateAlbumCommand, CreateAlbumHandler};
use domain::errors::DomainError;
use domain::value_objects::SystemId;
#[tokio::test]
async fn creates_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let uc = CreateAlbum::new(repo);
let handler = CreateAlbumHandler::new(repo);
let creator = SystemId::new();
let album = uc.execute("Vacation 2024", creator).await.unwrap();
let album = handler.execute(CreateAlbumCommand {
title: "Vacation 2024".into(),
creator_id: creator,
}).await.unwrap();
assert_eq!(album.title, "Vacation 2024");
assert_eq!(album.creator_user_id, creator);
assert_eq!(album.asset_count(), 0);
@@ -18,7 +21,10 @@ async fn creates_album() {
#[tokio::test]
async fn rejects_empty_title() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let uc = CreateAlbum::new(repo);
let result = uc.execute("", SystemId::new()).await;
let handler = CreateAlbumHandler::new(repo);
let result = handler.execute(CreateAlbumCommand {
title: "".into(),
creator_id: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}

View File

@@ -0,0 +1 @@
mod commands;

View File

@@ -1,48 +0,0 @@
use std::sync::Arc;
use application::testing::{InMemoryUserRepository, StubPasswordHasher};
use application::use_cases::RegisterUser;
use domain::errors::DomainError;
#[tokio::test]
async fn registers_new_user() {
let repo = Arc::new(InMemoryUserRepository::new());
let uc = RegisterUser::new(repo.clone(), Arc::new(StubPasswordHasher));
let user = uc.execute("testuser", "test@example.com", "password123").await.unwrap();
assert_eq!(user.username, "testuser");
assert_eq!(user.email.as_str(), "test@example.com");
assert_eq!(repo.all().await.len(), 1);
}
#[tokio::test]
async fn rejects_duplicate_email() {
let repo = Arc::new(InMemoryUserRepository::new());
let uc = RegisterUser::new(repo.clone(), Arc::new(StubPasswordHasher));
uc.execute("user1", "test@example.com", "password123").await.unwrap();
let result = uc.execute("user2", "test@example.com", "different1").await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}
#[tokio::test]
async fn rejects_duplicate_username() {
let repo = Arc::new(InMemoryUserRepository::new());
let uc = RegisterUser::new(repo.clone(), Arc::new(StubPasswordHasher));
uc.execute("sameuser", "a@example.com", "password123").await.unwrap();
let result = uc.execute("sameuser", "b@example.com", "password123").await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}
#[tokio::test]
async fn rejects_short_password() {
let repo = Arc::new(InMemoryUserRepository::new());
let uc = RegisterUser::new(repo, Arc::new(StubPasswordHasher));
let result = uc.execute("user", "test@example.com", "short").await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}
#[tokio::test]
async fn rejects_empty_username() {
let repo = Arc::new(InMemoryUserRepository::new());
let uc = RegisterUser::new(repo, Arc::new(StubPasswordHasher));
let result = uc.execute("", "test@example.com", "password123").await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}

View File

@@ -1,2 +0,0 @@
mod identity;
mod organization;