diff --git a/src/api/comments/check.rs b/src/api/comments/check.rs new file mode 100644 index 0000000..63425e9 --- /dev/null +++ b/src/api/comments/check.rs @@ -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) -> Result, 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 = rows + .iter() + .map(|r| (r.get::<_, i64>(0), r.get::<_, String>(1))) + .collect(); + + let result: Vec = 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!() +} diff --git a/src/api/comments/mod.rs b/src/api/comments/mod.rs index 6a91202..42730fc 100644 --- a/src/api/comments/mod.rs +++ b/src/api/comments/mod.rs @@ -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;