feat: implement pagination for feed retrieval and update frontend components
All checks were successful
Build and Deploy Thoughts / build-and-deploy-local (push) Successful in 2m7s

This commit is contained in:
2025-09-09 03:43:06 +02:00
parent 4ea4f3149f
commit 64806f8bd4
5 changed files with 236 additions and 29 deletions

View File

@@ -1,18 +1,27 @@
use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use axum::{
extract::{Query, State},
response::IntoResponse,
routing::get,
Json, Router,
};
use app::{
persistence::{follow::get_following_ids, thought::get_feed_for_users_and_self},
persistence::{follow::get_following_ids, thought::get_feed_for_users_and_self_paginated},
state::AppState,
};
use models::schemas::thought::{ThoughtListSchema, ThoughtSchema};
use models::{
queries::pagination::PaginationQuery,
schemas::{pagination::PaginatedResponse, thought::ThoughtSchema},
};
use crate::{error::ApiError, extractor::AuthUser};
#[utoipa::path(
get,
path = "",
params(PaginationQuery),
responses(
(status = 200, description = "Authenticated user's feed", body = ThoughtListSchema)
(status = 200, description = "Authenticated user's feed", body = PaginatedResponse<ThoughtSchema>)
),
security(
("api_key" = []),
@@ -22,17 +31,35 @@ use crate::{error::ApiError, extractor::AuthUser};
async fn feed_get(
State(state): State<AppState>,
auth_user: AuthUser,
Query(pagination): Query<PaginationQuery>,
) -> Result<impl IntoResponse, ApiError> {
let following_ids = get_following_ids(&state.conn, auth_user.id).await?;
let thoughts_with_authors =
get_feed_for_users_and_self(&state.conn, auth_user.id, following_ids).await?;
let (thoughts_with_authors, total_items) = get_feed_for_users_and_self_paginated(
&state.conn,
auth_user.id,
following_ids,
&pagination,
)
.await?;
let thoughts_schema: Vec<ThoughtSchema> = thoughts_with_authors
.into_iter()
.map(ThoughtSchema::from)
.collect();
Ok(Json(ThoughtListSchema::from(thoughts_schema)))
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: thoughts_schema,
total_items,
total_pages,
page,
page_size,
};
Ok(Json(response))
}
pub fn create_feed_router() -> Router<AppState> {