use anyhow::Result;
use reblessive::tree::Stk;
use crate::catalog::providers::ApiProvider;
use crate::ctx::FrozenContext;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::expr::parameterize::expr_to_ident;
use crate::expr::{Base, Expr, Literal, Value};
use crate::iam::{Action, ResourceKind};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct RemoveApiStatement {
pub name: Expr,
pub if_exists: bool,
}
impl Default for RemoveApiStatement {
fn default() -> Self {
Self {
name: Expr::Literal(Literal::None),
if_exists: false,
}
}
}
impl RemoveApiStatement {
pub(crate) async fn compute(
&self,
stk: &mut Stk,
ctx: &FrozenContext,
opt: &Options,
doc: Option<&CursorDoc>,
) -> Result<Value> {
ctx.is_allowed(opt, Action::Edit, ResourceKind::Api, Base::Db)?;
let name = expr_to_ident(stk, ctx, opt, doc, &self.name, "api name").await?;
let txn = ctx.tx();
let (ns, db) = ctx.expect_ns_db_ids(opt).await?;
let Some(ap) = txn.get_db_api(ns, db, &name, None).await? else {
if self.if_exists {
return Ok(Value::None);
} else {
return Err(Error::ApNotFound {
value: name,
}
.into());
}
};
let name = ap.path.to_string();
let key = crate::key::database::ap::new(ns, db, &name);
txn.del(&key).await?;
txn.clear_cache();
Ok(Value::None)
}
}