feat: admin panel

This commit is contained in:
Per Stark
2025-01-23 14:35:13 +01:00
parent 225dd41bd6
commit ce5effc0bf
6 changed files with 81 additions and 16 deletions

View File

@@ -18,7 +18,8 @@ use crate::{
page_data!(AdminPanelData, "auth/admin_panel.html", {
user: User,
settings: SystemSettings,
analytics: Analytics
analytics: Analytics,
users: i64,
});
pub async fn show_admin_panel(
@@ -27,23 +28,29 @@ pub async fn show_admin_panel(
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/").into_response()),
Some(user) if user.admin => user,
_ => return Ok(Redirect::to("/").into_response()),
};
let settings = SystemSettings::get_current(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let analytics = Analytics::get_current(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let users_count = Analytics::get_users_amount(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
AdminPanelData::template_name(),
AdminPanelData {
user,
settings,
analytics,
users: users_count,
},
state.templates.clone(),
)?;

View File

@@ -1,5 +1,6 @@
use crate::storage::types::file_info::deserialize_flexible_id;
use crate::storage::types::{file_info::deserialize_flexible_id, user::User, StoredObject};
use serde::{Deserialize, Serialize};
use tracing::info;
use crate::{error::AppError, storage::db::SurrealDbClient};
@@ -7,6 +8,7 @@ use crate::{error::AppError, storage::db::SurrealDbClient};
pub struct Analytics {
#[serde(deserialize_with = "deserialize_flexible_id")]
pub id: String,
pub page_loads: i64,
pub visitors: i64,
}
@@ -20,6 +22,7 @@ impl Analytics {
.content(Analytics {
id: "current".to_string(),
visitors: 0,
page_loads: 0,
})
.await?;
@@ -47,4 +50,30 @@ impl Analytics {
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
}
pub async fn increment_page_loads(db: &SurrealDbClient) -> Result<Self, AppError> {
let updated: Option<Self> = db
.client
.query("UPDATE type::thing('analytics', 'current') SET page_loads += 1 RETURN AFTER")
.await?
.take(0)?;
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
}
pub async fn get_users_amount(db: &SurrealDbClient) -> Result<i64, AppError> {
#[derive(Debug, Deserialize)]
struct CountResult {
count: i64,
}
let result: Option<CountResult> = db
.client
.query("SELECT count() as count FROM type::table($table) GROUP ALL")
.bind(("table", User::table_name()))
.await?
.take(0)?;
Ok(result.map(|r| r.count).unwrap_or(0))
}
}