#![cfg(any(feature = "mysql", feature = "postgres"))]
use crate::action::{ActionContext, TypedHandler};
use crate::error::BaseError;
use crate::table::Record;
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::Value;
use yang_base_derive::Action;
#[derive(Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetByPk {
pub id: Value,
}
#[derive(Action)]
#[action(
name = "get",
display_name = "获取数据",
description = "根据主键获取单条记录"
)]
pub struct GetAction;
impl GetAction {
pub fn new() -> Self {
Self
}
}
impl Default for GetAction {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl TypedHandler for GetAction {
type Input = GetByPk;
type Output = Record;
async fn handle(&self, ctx: ActionContext, input: GetByPk) -> Result<Record, BaseError> {
let definition = ctx.table_definition()?;
let table_name = definition.name().to_string();
let primary_key = definition.primary_key().to_string();
let pk_value = input.id;
if ctx.user.is_none() {
return Err(BaseError::Unauthorized("需要登录".to_string()));
}
let query = ctx.table_query()?;
let query = query.where_primary_key_eq(pk_value.clone())?;
query.optional().await?.ok_or_else(|| {
BaseError::RecordNotFound(format!(
"{} 中主键 {}={} 的记录不存在",
table_name, primary_key, pk_value
))
})
}
}