Posts management widget

This commit is contained in:
Alphonse Paix
2025-10-03 18:30:09 +02:00
parent 9296187181
commit ce8c602ddb
10 changed files with 203 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
use crate::routes::get_users;
use crate::routes::{POSTS_PER_PAGE, SUBS_PER_PAGE, get_posts_count, get_posts_page, get_users};
use crate::{
authentication::AuthenticatedUser,
routes::{AppError, get_max_page, get_subs, get_total_subs},
@@ -45,10 +45,18 @@ pub async fn admin_dashboard(
let count = get_total_subs(&connection_pool)
.await
.context("Could not fetch total subscribers count from the database.")?;
let max_page = get_max_page(count);
let max_page = get_max_page(count, SUBS_PER_PAGE);
let users = get_users(&connection_pool)
.await
.context("Could not fetch users")?;
let posts = get_posts_page(&connection_pool, 1)
.await
.context("Could not fetch posts.")?;
let posts_current_page = 1;
let count = get_posts_count(&connection_pool)
.await
.context("Could not fetch posts count.")?;
let posts_max_page = get_max_page(count, POSTS_PER_PAGE);
let template = DashboardTemplate {
user,
idempotency_key_1,
@@ -58,6 +66,9 @@ pub async fn admin_dashboard(
current_page,
max_page,
users,
posts,
posts_current_page,
posts_max_page,
};
Ok(Html(template.render().unwrap()).into_response())
}

View File

@@ -13,7 +13,7 @@ use axum::{
use sqlx::PgPool;
use uuid::Uuid;
const SUBS_PER_PAGE: i64 = 5;
pub const SUBS_PER_PAGE: i64 = 5;
#[tracing::instrument(name = "Retrieving subscribers from database", skip(connection_pool))]
pub async fn get_subscribers_page(
@@ -26,7 +26,7 @@ pub async fn get_subscribers_page(
.await
.context("Could not fetch total subscribers count from the database.")
.map_err(AppError::unexpected_message)?;
let max_page = get_max_page(count);
let max_page = get_max_page(count, SUBS_PER_PAGE);
let subscribers = get_subs(&connection_pool, page)
.await
.context("Could not fetch subscribers data.")
@@ -102,9 +102,9 @@ pub async fn get_total_subs(connection_pool: &PgPool) -> Result<i64, sqlx::Error
Ok(count)
}
pub fn get_max_page(count: i64) -> i64 {
let mut max_page = count.div_euclid(SUBS_PER_PAGE);
if count % SUBS_PER_PAGE > 0 {
pub fn get_max_page(count: i64, num_per_page: i64) -> i64 {
let mut max_page = count.div_euclid(num_per_page);
if count % num_per_page > 0 {
max_page += 1;
}
max_page