use crate::action::{Action, ActionContext, ApiResponse};
use crate::error::BaseError;
use crate::table::TableConfig;
use async_trait::async_trait;
use std::sync::Arc;
pub struct DelAction {
table_config: Arc<TableConfig>,
}
impl DelAction {
pub fn new(table_config: Arc<TableConfig>) -> Self {
Self { table_config }
}
}
#[async_trait]
impl Action for DelAction {
async fn execute(&self, context: ActionContext) -> Result<ApiResponse, BaseError> {
let pk_field = &self.table_config.primary_key;
let pk_value: serde_json::Value = context.param(pk_field)?;
let mut query = context.table_query()?;
query = query.where_eq(pk_field.as_str(), pk_value)?;
let affected = query.delete().await?;
Ok(ApiResponse::success(
serde_json::json!({ "affected": affected }),
"删除成功",
))
}
fn name(&self) -> &str {
"del"
}
fn display_name(&self) -> &str {
"删除数据"
}
fn description(&self) -> &str {
"根据主键删除数据表中的记录(支持软删除)"
}
}