Refactor handlers and OpenAPI documentation for improved readability and consistency
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 6m49s
test / unit (pull_request) Successful in 16m24s
test / integration (pull_request) Failing after 17m7s

- Reorganized imports in health, notifications, social, thoughts, and users handlers for clarity.
- Updated function signatures in handlers to improve readability by aligning parameters.
- Enhanced JSON response formatting in notifications and thoughts handlers.
- Improved error handling in user-related functions.
- Refactored OpenAPI documentation to maintain consistent formatting and structure.
- Cleaned up unnecessary code and comments across various files.
- Ensured consistent use of `Arc` for shared state in AppState and WorkerHandlers.
This commit is contained in:
2026-05-14 16:28:57 +02:00
parent 004bfb427b
commit 10c4a66de5
47 changed files with 2406 additions and 723 deletions

View File

@@ -1,9 +1,17 @@
use axum::{extract::{Path, Query, State}, Json};
use api_types::{requests::UpdateProfileRequest, responses::{ErrorResponse, UserResponse}};
use crate::{
errors::ApiError, extractors::AuthUser, handlers::auth::to_user_response, state::AppState,
};
use api_types::{
requests::UpdateProfileRequest,
responses::{ErrorResponse, UserResponse},
};
use application::use_cases::feed::list_users;
use application::use_cases::profile::{get_user_by_username, update_profile};
use application::use_cases::search::search_users;
use application::use_cases::feed::list_users;
use crate::{errors::ApiError, extractors::AuthUser, handlers::auth::to_user_response, state::AppState};
use axum::{
extract::{Path, Query, State},
Json,
};
#[utoipa::path(
get, path = "/users/{username}",
@@ -13,7 +21,10 @@ use crate::{errors::ApiError, extractors::AuthUser, handlers::auth::to_user_resp
(status = 404, description = "User not found", body = ErrorResponse),
)
)]
pub async fn get_user(State(s): State<AppState>, Path(username): Path<String>) -> Result<Json<UserResponse>, ApiError> {
pub async fn get_user(
State(s): State<AppState>,
Path(username): Path<String>,
) -> Result<Json<UserResponse>, ApiError> {
let user = get_user_by_username(&*s.users, &username).await?;
Ok(Json(to_user_response(&user)))
}
@@ -27,9 +38,26 @@ pub async fn get_user(State(s): State<AppState>, Path(username): Path<String>) -
),
security(("bearer_auth" = []))
)]
pub async fn patch_profile(State(s): State<AppState>, AuthUser(uid): AuthUser, Json(body): Json<UpdateProfileRequest>) -> Result<Json<UserResponse>, ApiError> {
update_profile(&*s.users, &uid, body.display_name, body.bio, body.avatar_url, body.header_url, body.custom_css).await?;
let user = s.users.find_by_id(&uid).await?.ok_or(domain::errors::DomainError::NotFound)?;
pub async fn patch_profile(
State(s): State<AppState>,
AuthUser(uid): AuthUser,
Json(body): Json<UpdateProfileRequest>,
) -> Result<Json<UserResponse>, ApiError> {
update_profile(
&*s.users,
&uid,
body.display_name,
body.bio,
body.avatar_url,
body.header_url,
body.custom_css,
)
.await?;
let user = s
.users
.find_by_id(&uid)
.await?
.ok_or(domain::errors::DomainError::NotFound)?;
Ok(Json(to_user_response(&user)))
}
@@ -41,8 +69,15 @@ pub async fn patch_profile(State(s): State<AppState>, AuthUser(uid): AuthUser, J
),
security(("bearer_auth" = []))
)]
pub async fn get_me(State(s): State<AppState>, AuthUser(uid): AuthUser) -> Result<Json<UserResponse>, ApiError> {
let user = s.users.find_by_id(&uid).await?.ok_or(domain::errors::DomainError::NotFound)?;
pub async fn get_me(
State(s): State<AppState>,
AuthUser(uid): AuthUser,
) -> Result<Json<UserResponse>, ApiError> {
let user = s
.users
.find_by_id(&uid)
.await?
.ok_or(domain::errors::DomainError::NotFound)?;
Ok(Json(to_user_response(&user)))
}
@@ -51,13 +86,23 @@ pub async fn get_users(
Query(params): Query<std::collections::HashMap<String, String>>,
) -> Result<Json<serde_json::Value>, ApiError> {
use domain::models::feed::PageParams;
let page = params.get("page").and_then(|v| v.parse::<u64>().ok()).unwrap_or(1);
let per_page = params.get("per_page").and_then(|v| v.parse::<u64>().ok()).unwrap_or(20);
let page = params
.get("page")
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(1);
let per_page = params
.get("per_page")
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(20);
let page_params = PageParams { page, per_page };
if let Some(q) = params.get("q").filter(|q| !q.trim().is_empty()) {
let result = search_users(&*s.search, q, page_params).await?;
let users: Vec<_> = result.items.iter().map(|u| crate::handlers::auth::to_user_response(u)).collect();
let users: Vec<_> = result
.items
.iter()
.map(|u| crate::handlers::auth::to_user_response(u))
.collect();
return Ok(Json(serde_json::json!({
"items": users, "total": result.total, "page": result.page, "per_page": result.per_page
})));
@@ -66,18 +111,22 @@ pub async fn get_users(
let all = list_users(&*s.users).await?;
let total = all.len() as i64;
let start = ((page - 1) * per_page) as usize;
let items: Vec<_> = all.into_iter()
.skip(start).take(per_page as usize)
.map(|u| serde_json::json!({
"id": u.id.as_uuid(),
"username": u.username,
"display_name": u.display_name,
"avatar_url": u.avatar_url,
"bio": u.bio,
"thought_count": u.thought_count,
"follower_count": u.follower_count,
"following_count": u.following_count,
}))
let items: Vec<_> = all
.into_iter()
.skip(start)
.take(per_page as usize)
.map(|u| {
serde_json::json!({
"id": u.id.as_uuid(),
"username": u.username,
"display_name": u.display_name,
"avatar_url": u.avatar_url,
"bio": u.bio,
"thought_count": u.thought_count,
"follower_count": u.follower_count,
"following_count": u.following_count,
})
})
.collect();
Ok(Json(serde_json::json!({
"items": items, "total": total, "page": page, "per_page": per_page