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:
1
crates/application/src/catalog/mod.rs
Normal file
1
crates/application/src/catalog/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
// Catalog commands/queries (future: SearchAssets, UpdateMetadata, etc.)
|
||||
@@ -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()));
|
||||
}
|
||||
5
crates/application/src/identity/commands/mod.rs
Normal file
5
crates/application/src/identity/commands/mod.rs
Normal 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};
|
||||
45
crates/application/src/identity/commands/register_user.rs
Normal file
45
crates/application/src/identity/commands/register_user.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
5
crates/application/src/identity/mod.rs
Normal file
5
crates/application/src/identity/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod commands;
|
||||
pub mod queries;
|
||||
|
||||
pub use commands::{RegisterUserCommand, RegisterUserHandler, LoginUserCommand, LoginUserHandler};
|
||||
pub use queries::{GetProfileQuery, GetProfileHandler};
|
||||
22
crates/application/src/identity/queries/get_profile.rs
Normal file
22
crates/application/src/identity/queries/get_profile.rs
Normal 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)))
|
||||
}
|
||||
}
|
||||
3
crates/application/src/identity/queries/mod.rs
Normal file
3
crates/application/src/identity/queries/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod get_profile;
|
||||
|
||||
pub use get_profile::{GetProfileQuery, GetProfileHandler};
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
3
crates/application/src/organization/commands/mod.rs
Normal file
3
crates/application/src/organization/commands/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod create_album;
|
||||
|
||||
pub use create_album::{CreateAlbumCommand, CreateAlbumHandler};
|
||||
3
crates/application/src/organization/mod.rs
Normal file
3
crates/application/src/organization/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod commands;
|
||||
|
||||
pub use commands::{CreateAlbumCommand, CreateAlbumHandler};
|
||||
1
crates/application/src/processing/mod.rs
Normal file
1
crates/application/src/processing/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
// Processing commands/queries (future: EnqueueJob, ProcessBatch, etc.)
|
||||
1
crates/application/src/sharing/mod.rs
Normal file
1
crates/application/src/sharing/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
// Sharing commands/queries (future: CreateShareLink, ManageAccess, etc.)
|
||||
1
crates/application/src/sidecar/mod.rs
Normal file
1
crates/application/src/sidecar/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
// Sidecar commands/queries (future: SyncSidecar, ExportMetadata, etc.)
|
||||
1
crates/application/src/storage/mod.rs
Normal file
1
crates/application/src/storage/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
// Storage commands/queries (future: IngestAsset, ManageVolume, etc.)
|
||||
@@ -1 +0,0 @@
|
||||
// Catalog use cases (future: SearchAssets, UpdateMetadata, etc.)
|
||||
@@ -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")))
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -1,3 +0,0 @@
|
||||
pub mod create_album;
|
||||
|
||||
pub use create_album::CreateAlbum;
|
||||
@@ -1 +0,0 @@
|
||||
// Processing use cases (future: EnqueueJob, ProcessBatch, etc.)
|
||||
@@ -1 +0,0 @@
|
||||
// Sharing use cases (future: CreateShareLink, ManageAccess, etc.)
|
||||
@@ -1 +0,0 @@
|
||||
// Sidecar Sync use cases (future: SyncSidecar, ExportMetadata, etc.)
|
||||
@@ -1 +0,0 @@
|
||||
// Storage use cases (future: IngestAsset, ManageVolume, etc.)
|
||||
Reference in New Issue
Block a user