feat: implement pagination for user retrieval and update feed fetching logic
All checks were successful
Build and Deploy Thoughts / build-and-deploy-local (push) Successful in 2m30s

This commit is contained in:
2025-09-09 02:53:24 +02:00
parent 863bc90c6f
commit d92c9a747e
10 changed files with 229 additions and 31 deletions

View File

@@ -1,7 +1,7 @@
use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use app::{
persistence::{follow::get_following_ids, thought::get_feed_for_user},
persistence::{follow::get_following_ids, thought::get_feed_for_users_and_self},
state::AppState,
};
use models::schemas::thought::{ThoughtListSchema, ThoughtSchema};
@@ -24,12 +24,8 @@ async fn feed_get(
auth_user: AuthUser,
) -> Result<impl IntoResponse, ApiError> {
let following_ids = get_following_ids(&state.conn, auth_user.id).await?;
let mut thoughts_with_authors =
get_feed_for_user(&state.conn, following_ids, Some(auth_user.id)).await?;
let own_thoughts =
get_feed_for_user(&state.conn, vec![auth_user.id], Some(auth_user.id)).await?;
thoughts_with_authors.extend(own_thoughts);
let thoughts_with_authors =
get_feed_for_users_and_self(&state.conn, auth_user.id, following_ids).await?;
let thoughts_schema: Vec<ThoughtSchema> = thoughts_with_authors
.into_iter()

View File

@@ -17,8 +17,14 @@ use app::persistence::{
};
use app::state::AppState;
use app::{error::UserError, persistence::user::get_user_by_username};
use models::schemas::user::{MeSchema, UserListSchema, UserSchema};
use models::{params::user::UpdateUserParams, schemas::thought::ThoughtListSchema};
use models::{
params::user::UpdateUserParams,
schemas::{pagination::PaginatedResponse, thought::ThoughtListSchema},
};
use models::{
queries::pagination::PaginationQuery,
schemas::user::{MeSchema, UserListSchema, UserSchema},
};
use models::{queries::user::UserQuery, schemas::thought::ThoughtSchema};
use crate::{error::ApiError, extractor::AuthUser};
@@ -418,16 +424,31 @@ async fn get_user_followers(
#[utoipa::path(
get,
path = "/all",
params(PaginationQuery),
responses(
(status = 200, description = "A public list of all users", body = UserListSchema)
(status = 200, description = "A public, paginated list of all users", body = PaginatedResponse<UserSchema>)
),
tag = "user"
)]
async fn get_all_users_public(
State(state): State<AppState>,
Query(pagination): Query<PaginationQuery>,
) -> Result<impl IntoResponse, ApiError> {
let users = get_all_users(&state.conn).await?;
Ok(Json(UserListSchema::from(users)))
let (users, total_items) = get_all_users(&state.conn, &pagination).await?;
let page = pagination.page();
let page_size = pagination.page_size();
let total_pages = (total_items as f64 / page_size as f64).ceil() as u64;
let response = PaginatedResponse {
items: users.into_iter().map(UserSchema::from).collect(),
page,
page_size,
total_pages,
total_items,
};
Ok(Json(response))
}
pub fn create_user_router() -> Router<AppState> {