refactor(presentation): replace impl FromAppState boilerplate with deps_struct! macro in remaining handlers

This commit is contained in:
2026-05-16 11:19:04 +02:00
parent e9f7851400
commit 5618da7d37
5 changed files with 38 additions and 96 deletions

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
deps_struct,
errors::ApiError, errors::ApiError,
extractors::{AuthUser, Deps, FromAppState}, extractors::{AuthUser, Deps},
state::AppState,
}; };
use api_types::{ use api_types::{
requests::CreateApiKeyRequest, requests::CreateApiKeyRequest,
@@ -14,20 +14,11 @@ use axum::{
Json, Json,
}; };
use domain::{ports::ApiKeyRepository, value_objects::ApiKeyId}; use domain::{ports::ApiKeyRepository, value_objects::ApiKeyId};
use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
pub struct ApiKeysDeps { deps_struct!(ApiKeysDeps {
pub api_keys: Arc<dyn ApiKeyRepository>, api_keys: ApiKeyRepository,
} });
impl FromAppState for ApiKeysDeps {
fn from_state(s: &AppState) -> Self {
Self {
api_keys: s.api_keys.clone(),
}
}
}
#[utoipa::path(get, path = "/api-keys", responses((status = 200, description = "API keys", body = Vec<ApiKeyResponse>)), security(("bearer_auth" = [])))] #[utoipa::path(get, path = "/api-keys", responses((status = 200, description = "API keys", body = Vec<ApiKeyResponse>)), security(("bearer_auth" = [])))]
pub async fn get_api_keys( pub async fn get_api_keys(

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
deps_struct,
errors::ApiError, errors::ApiError,
extractors::{Deps, FromAppState}, extractors::Deps,
state::AppState,
}; };
use api_types::{ use api_types::{
requests::{LoginRequest, RegisterRequest}, requests::{LoginRequest, RegisterRequest},
@@ -10,25 +10,13 @@ use api_types::{
use application::use_cases::auth::{login, register, LoginInput, RegisterInput}; use application::use_cases::auth::{login, register, LoginInput, RegisterInput};
use axum::{http::StatusCode, response::IntoResponse, Json}; use axum::{http::StatusCode, response::IntoResponse, Json};
use domain::ports::{AuthService, EventPublisher, PasswordHasher, UserRepository}; use domain::ports::{AuthService, EventPublisher, PasswordHasher, UserRepository};
use std::sync::Arc;
pub struct AuthDeps { deps_struct!(AuthDeps {
pub users: Arc<dyn UserRepository>, users: UserRepository,
pub hasher: Arc<dyn PasswordHasher>, hasher: PasswordHasher,
pub auth: Arc<dyn AuthService>, auth: AuthService,
pub events: Arc<dyn EventPublisher>, events: EventPublisher,
} });
impl FromAppState for AuthDeps {
fn from_state(s: &AppState) -> Self {
Self {
users: s.users.clone(),
hasher: s.hasher.clone(),
auth: s.auth.clone(),
events: s.events.clone(),
}
}
}
pub fn to_user_response(u: &domain::models::user::User) -> UserResponse { pub fn to_user_response(u: &domain::models::user::User) -> UserResponse {
UserResponse { UserResponse {

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
deps_struct,
errors::ApiError, errors::ApiError,
extractors::{AuthUser, Deps, FromAppState}, extractors::{AuthUser, Deps},
state::AppState,
}; };
use api_types::responses::{ProfileField, RemoteActorResponse}; use api_types::responses::{ProfileField, RemoteActorResponse};
use application::use_cases::federation_management::{ use application::use_cases::federation_management::{
@@ -11,7 +11,6 @@ use application::use_cases::federation_management::{
use axum::{http::StatusCode, Json}; use axum::{http::StatusCode, Json};
use domain::ports::{EventPublisher, FederationActionPort, FollowRepository, UserRepository}; use domain::ports::{EventPublisher, FederationActionPort, FollowRepository, UserRepository};
use serde::Deserialize; use serde::Deserialize;
use std::sync::Arc;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct ActorUrlBody { pub struct ActorUrlBody {
@@ -23,23 +22,12 @@ pub struct HandleBody {
pub handle: String, pub handle: String,
} }
pub struct FederationManagementDeps { deps_struct!(FederationManagementDeps {
pub federation: Arc<dyn FederationActionPort>, federation: FederationActionPort,
pub follows: Arc<dyn FollowRepository>, follows: FollowRepository,
pub users: Arc<dyn UserRepository>, users: UserRepository,
pub events: Arc<dyn EventPublisher>, events: EventPublisher,
} });
impl FromAppState for FederationManagementDeps {
fn from_state(s: &AppState) -> Self {
Self {
federation: s.federation.clone(),
follows: s.follows.clone(),
users: s.users.clone(),
events: s.events.clone(),
}
}
}
fn to_response(a: domain::models::remote_actor::RemoteActor) -> RemoteActorResponse { fn to_response(a: domain::models::remote_actor::RemoteActor) -> RemoteActorResponse {
RemoteActorResponse { RemoteActorResponse {

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
deps_struct,
errors::ApiError, errors::ApiError,
extractors::{AuthUser, Deps, FromAppState}, extractors::{AuthUser, Deps},
state::AppState,
}; };
use api_types::requests::NotificationUpdateRequest; use api_types::requests::NotificationUpdateRequest;
use application::use_cases::notifications::{ use application::use_cases::notifications::{
@@ -16,20 +16,11 @@ use axum::{
use domain::{ use domain::{
models::feed::PageParams, ports::NotificationRepository, value_objects::NotificationId, models::feed::PageParams, ports::NotificationRepository, value_objects::NotificationId,
}; };
use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
pub struct NotificationsDeps { deps_struct!(NotificationsDeps {
pub notifications: Arc<dyn NotificationRepository>, notifications: NotificationRepository,
} });
impl FromAppState for NotificationsDeps {
fn from_state(s: &AppState) -> Self {
Self {
notifications: s.notifications.clone(),
}
}
}
#[utoipa::path(get, path = "/notifications", responses((status = 200, description = "Notification summary")), security(("bearer_auth" = [])))] #[utoipa::path(get, path = "/notifications", responses((status = 200, description = "Notification summary")), security(("bearer_auth" = [])))]
pub async fn list_notifications( pub async fn list_notifications(

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
deps_struct,
errors::ApiError, errors::ApiError,
extractors::{AuthUser, Deps, FromAppState}, extractors::{AuthUser, Deps},
state::AppState,
}; };
use api_types::requests::SetTopFriendsRequest; use api_types::requests::SetTopFriendsRequest;
use api_types::responses::TopFriendsResponse; use api_types::responses::TopFriendsResponse;
@@ -20,34 +20,18 @@ use domain::{
}, },
value_objects::{ThoughtId, UserId}, value_objects::{ThoughtId, UserId},
}; };
use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
pub struct SocialDeps { deps_struct!(SocialDeps {
pub likes: Arc<dyn LikeRepository>, likes: LikeRepository,
pub boosts: Arc<dyn BoostRepository>, boosts: BoostRepository,
pub follows: Arc<dyn FollowRepository>, follows: FollowRepository,
pub users: Arc<dyn UserRepository>, users: UserRepository,
pub federation: Arc<dyn FederationActionPort>, federation: FederationActionPort,
pub events: Arc<dyn EventPublisher>, events: EventPublisher,
pub blocks: Arc<dyn BlockRepository>, blocks: BlockRepository,
pub top_friends: Arc<dyn TopFriendRepository>, top_friends: TopFriendRepository,
} });
impl FromAppState for SocialDeps {
fn from_state(s: &AppState) -> Self {
Self {
likes: s.likes.clone(),
boosts: s.boosts.clone(),
follows: s.follows.clone(),
users: s.users.clone(),
federation: s.federation.clone(),
events: s.events.clone(),
blocks: s.blocks.clone(),
top_friends: s.top_friends.clone(),
}
}
}
#[utoipa::path(post, path = "/thoughts/{id}/like", params(("id" = uuid::Uuid, Path, description = "Thought ID")), responses((status = 204, description = "Liked")), security(("bearer_auth" = [])))] #[utoipa::path(post, path = "/thoughts/{id}/like", params(("id" = uuid::Uuid, Path, description = "Thought ID")), responses((status = 204, description = "Liked")), security(("bearer_auth" = [])))]
pub async fn post_like( pub async fn post_like(