feat(api): add CheckPendingStatus server function
Accepts Vec<i64> of comment IDs, returns their current status. IDs not found in DB return status 'gone'. Empty vec returns early. Used by client to prune localStorage pending comments that are no longer pending (approved/spam/trash/deleted).
This commit is contained in:
parent
d63cee58c2
commit
7b3d894e96
47
src/api/comments/check.rs
Normal file
47
src/api/comments/check.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PendingStatusItem {
|
||||
pub id: i64,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[server(CheckPendingStatus, "/api")]
|
||||
pub async fn check_pending_status(ids: Vec<i64>) -> Result<Vec<PendingStatusItem>, ServerFnError> {
|
||||
#[cfg(feature = "server")]
|
||||
{
|
||||
use crate::db::pool::get_conn;
|
||||
use crate::api::error::AppError;
|
||||
|
||||
if ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let client = get_conn().await.map_err(AppError::db_conn)?;
|
||||
|
||||
let rows = client
|
||||
.query(
|
||||
"SELECT id, status FROM comments WHERE id = ANY($1)",
|
||||
&[&ids],
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::query)?;
|
||||
|
||||
let found: std::collections::HashMap<i64, String> = rows
|
||||
.iter()
|
||||
.map(|r| (r.get::<_, i64>(0), r.get::<_, String>(1)))
|
||||
.collect();
|
||||
|
||||
let result: Vec<PendingStatusItem> = ids
|
||||
.into_iter()
|
||||
.map(|id| {
|
||||
let status = found.get(&id).cloned().unwrap_or_else(|| "gone".to_string());
|
||||
PendingStatusItem { id, status }
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
#[cfg(not(feature = "server"))]
|
||||
unreachable!()
|
||||
}
|
||||
@ -7,12 +7,14 @@ mod create;
|
||||
mod read;
|
||||
mod update;
|
||||
mod list;
|
||||
mod check;
|
||||
|
||||
pub use types::*;
|
||||
pub use create::create_comment;
|
||||
pub use read::{get_comments, get_comment_count};
|
||||
pub use update::{approve_comment, spam_comment, trash_comment, batch_update_comment_status};
|
||||
pub use list::{get_pending_comments, get_pending_count, get_all_comments};
|
||||
pub use check::check_pending_status;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub use markdown::render_comment_markdown;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user