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
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:
@@ -1,11 +1,32 @@
|
||||
use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, Json};
|
||||
use uuid::Uuid;
|
||||
use api_types::{requests::{CreateThoughtRequest, EditThoughtRequest}, responses::ErrorResponse};
|
||||
use application::use_cases::thoughts::{create_thought, delete_thought, edit_thought, get_thought, get_thread, CreateThoughtInput};
|
||||
use crate::{
|
||||
errors::ApiError,
|
||||
extractors::{AuthUser, OptionalAuthUser},
|
||||
handlers::auth::to_user_response,
|
||||
state::AppState,
|
||||
};
|
||||
use api_types::{
|
||||
requests::{CreateThoughtRequest, EditThoughtRequest},
|
||||
responses::ErrorResponse,
|
||||
};
|
||||
use application::use_cases::thoughts::{
|
||||
create_thought, delete_thought, edit_thought, get_thought, get_thread, CreateThoughtInput,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
Json,
|
||||
};
|
||||
use domain::value_objects::ThoughtId;
|
||||
use crate::{errors::ApiError, extractors::{AuthUser, OptionalAuthUser}, handlers::auth::to_user_response, state::AppState};
|
||||
use uuid::Uuid;
|
||||
|
||||
fn thought_to_json(t: &domain::models::thought::Thought, author: &domain::models::user::User, like_count: i64, boost_count: i64, reply_count: i64) -> serde_json::Value {
|
||||
fn thought_to_json(
|
||||
t: &domain::models::thought::Thought,
|
||||
author: &domain::models::user::User,
|
||||
like_count: i64,
|
||||
boost_count: i64,
|
||||
reply_count: i64,
|
||||
) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"id": t.id.as_uuid(),
|
||||
"content": t.content.as_str(),
|
||||
@@ -32,18 +53,35 @@ fn thought_to_json(t: &domain::models::thought::Thought, author: &domain::models
|
||||
),
|
||||
security(("bearer_auth" = []))
|
||||
)]
|
||||
pub async fn post_thought(State(s): State<AppState>, AuthUser(uid): AuthUser, Json(body): Json<CreateThoughtRequest>) -> Result<impl IntoResponse, ApiError> {
|
||||
pub async fn post_thought(
|
||||
State(s): State<AppState>,
|
||||
AuthUser(uid): AuthUser,
|
||||
Json(body): Json<CreateThoughtRequest>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let in_reply_to = body.in_reply_to_id.map(ThoughtId::from_uuid);
|
||||
let out = create_thought(&*s.thoughts, &*s.users, &*s.events, CreateThoughtInput {
|
||||
user_id: uid.clone(),
|
||||
content: body.content,
|
||||
in_reply_to_id: in_reply_to,
|
||||
visibility: body.visibility,
|
||||
content_warning: body.content_warning,
|
||||
sensitive: body.sensitive.unwrap_or(false),
|
||||
}).await?;
|
||||
let author = s.users.find_by_id(&uid).await?.ok_or(domain::errors::DomainError::NotFound)?;
|
||||
Ok((StatusCode::CREATED, Json(thought_to_json(&out.thought, &author, 0, 0, 0))))
|
||||
let out = create_thought(
|
||||
&*s.thoughts,
|
||||
&*s.users,
|
||||
&*s.events,
|
||||
CreateThoughtInput {
|
||||
user_id: uid.clone(),
|
||||
content: body.content,
|
||||
in_reply_to_id: in_reply_to,
|
||||
visibility: body.visibility,
|
||||
content_warning: body.content_warning,
|
||||
sensitive: body.sensitive.unwrap_or(false),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
let author = s
|
||||
.users
|
||||
.find_by_id(&uid)
|
||||
.await?
|
||||
.ok_or(domain::errors::DomainError::NotFound)?;
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(thought_to_json(&out.thought, &author, 0, 0, 0)),
|
||||
))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
@@ -54,9 +92,17 @@ pub async fn post_thought(State(s): State<AppState>, AuthUser(uid): AuthUser, Js
|
||||
(status = 404, description = "Not found", body = ErrorResponse),
|
||||
)
|
||||
)]
|
||||
pub async fn get_thought_handler(State(s): State<AppState>, Path(id): Path<Uuid>, OptionalAuthUser(_viewer): OptionalAuthUser) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
pub async fn get_thought_handler(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
OptionalAuthUser(_viewer): OptionalAuthUser,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let thought = get_thought(&*s.thoughts, &ThoughtId::from_uuid(id)).await?;
|
||||
let author = s.users.find_by_id(&thought.user_id).await?.ok_or(domain::errors::DomainError::NotFound)?;
|
||||
let author = s
|
||||
.users
|
||||
.find_by_id(&thought.user_id)
|
||||
.await?
|
||||
.ok_or(domain::errors::DomainError::NotFound)?;
|
||||
Ok(Json(thought_to_json(&thought, &author, 0, 0, 0)))
|
||||
}
|
||||
|
||||
@@ -70,7 +116,11 @@ pub async fn get_thought_handler(State(s): State<AppState>, Path(id): Path<Uuid>
|
||||
),
|
||||
security(("bearer_auth" = []))
|
||||
)]
|
||||
pub async fn delete_thought_handler(State(s): State<AppState>, AuthUser(uid): AuthUser, Path(id): Path<Uuid>) -> Result<StatusCode, ApiError> {
|
||||
pub async fn delete_thought_handler(
|
||||
State(s): State<AppState>,
|
||||
AuthUser(uid): AuthUser,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
delete_thought(&*s.thoughts, &*s.events, &ThoughtId::from_uuid(id), &uid).await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
@@ -86,8 +136,20 @@ pub async fn delete_thought_handler(State(s): State<AppState>, AuthUser(uid): Au
|
||||
),
|
||||
security(("bearer_auth" = []))
|
||||
)]
|
||||
pub async fn patch_thought(State(s): State<AppState>, AuthUser(uid): AuthUser, Path(id): Path<Uuid>, Json(body): Json<EditThoughtRequest>) -> Result<StatusCode, ApiError> {
|
||||
edit_thought(&*s.thoughts, &*s.events, &ThoughtId::from_uuid(id), &uid, body.content).await?;
|
||||
pub async fn patch_thought(
|
||||
State(s): State<AppState>,
|
||||
AuthUser(uid): AuthUser,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(body): Json<EditThoughtRequest>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
edit_thought(
|
||||
&*s.thoughts,
|
||||
&*s.events,
|
||||
&ThoughtId::from_uuid(id),
|
||||
&uid,
|
||||
body.content,
|
||||
)
|
||||
.await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
@@ -98,7 +160,10 @@ pub async fn patch_thought(State(s): State<AppState>, AuthUser(uid): AuthUser, P
|
||||
(status = 200, description = "Thread (root + replies)"),
|
||||
)
|
||||
)]
|
||||
pub async fn get_thread_handler(State(s): State<AppState>, Path(id): Path<Uuid>) -> Result<Json<Vec<serde_json::Value>>, ApiError> {
|
||||
pub async fn get_thread_handler(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<Vec<serde_json::Value>>, ApiError> {
|
||||
let thoughts = get_thread(&*s.thoughts, &ThoughtId::from_uuid(id)).await?;
|
||||
let mut items = Vec::new();
|
||||
for t in &thoughts {
|
||||
|
||||
Reference in New Issue
Block a user