fix(api): top-friends endpoint returns full UserResponse — eliminates frontend N+1
This commit is contained in:
@@ -4,6 +4,8 @@ use crate::{
|
|||||||
state::AppState,
|
state::AppState,
|
||||||
};
|
};
|
||||||
use api_types::requests::SetTopFriendsRequest;
|
use api_types::requests::SetTopFriendsRequest;
|
||||||
|
use api_types::responses::TopFriendsResponse;
|
||||||
|
use crate::handlers::auth::to_user_response;
|
||||||
use application::use_cases::profile::{get_top_friends, get_user_by_username, set_top_friends};
|
use application::use_cases::profile::{get_top_friends, get_user_by_username, set_top_friends};
|
||||||
use application::use_cases::social::*;
|
use application::use_cases::social::*;
|
||||||
use axum::{
|
use axum::{
|
||||||
@@ -155,15 +157,17 @@ pub async fn put_top_friends(
|
|||||||
set_top_friends(&*d.top_friends, &uid, ids).await?;
|
set_top_friends(&*d.top_friends, &uid, ids).await?;
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
#[utoipa::path(get, path = "/users/{username}/top-friends", params(("username" = String, Path, description = "Username")), responses((status = 200, description = "Top friends list")))]
|
#[utoipa::path(get, path = "/users/{username}/top-friends",
|
||||||
|
params(("username" = String, Path, description = "Username")),
|
||||||
|
responses((status = 200, description = "Top friends list", body = TopFriendsResponse)))]
|
||||||
pub async fn get_top_friends_handler(
|
pub async fn get_top_friends_handler(
|
||||||
Deps(d): Deps<SocialDeps>,
|
Deps(d): Deps<SocialDeps>,
|
||||||
Path(username): Path<String>,
|
Path(username): Path<String>,
|
||||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
) -> Result<Json<TopFriendsResponse>, ApiError> {
|
||||||
let user = get_user_by_username(&*d.users, &username).await?;
|
let user = get_user_by_username(&*d.users, &username).await?;
|
||||||
let friends = get_top_friends(&*d.top_friends, &user.id).await?;
|
let friends = get_top_friends(&*d.top_friends, &user.id).await?;
|
||||||
let usernames: Vec<&str> = friends.iter().map(|(_, u)| u.username.as_str()).collect();
|
let top_friends = friends.iter().map(|(_, u)| to_user_response(u)).collect();
|
||||||
Ok(Json(serde_json::json!({ "topFriends": usernames })))
|
Ok(Json(TopFriendsResponse { top_friends }))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user