#![cfg(any(feature = "mysql", feature = "postgres"))]
use crate::action::builtin::{add::AffectedResult, get::GetByPk};
use crate::action::{ActionContext, TypedHandler};
use crate::error::BaseError;
use async_trait::async_trait;
use yang_base_derive::Action;
#[derive(Action)]
#[action(
name = "del",
display_name = "删除数据",
description = "按主键删除记录"
)]
pub struct DelAction;
impl DelAction {
pub fn new() -> Self {
Self
}
}
impl Default for DelAction {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl TypedHandler for DelAction {
type Input = GetByPk;
type Output = AffectedResult;
async fn handle(
&self,
ctx: ActionContext,
input: GetByPk,
) -> Result<AffectedResult, BaseError> {
let affected = ctx
.table_query()?
.where_primary_key_eq(input.id)?
.delete()
.await?;
Ok(AffectedResult { affected })
}
}