use br_fields::Field;
use json::{JsonValue, object};
use br_addon::action::{Action, InterfaceType};
use br_addon::module::Module;
use br_addon::request::Request;
use br_addon::ApiResponse;

pub struct {{action}} {
    pub module: {{model}},
}

impl Action for {{action}} {
    fn title(&self) -> &'static str { "{{title}}" }
    fn tags(&self) -> &'static [&'static str] { &[] }
    fn interface_type(&self) -> InterfaceType {
        InterfaceType::API
    }
    fn params(&mut self) -> JsonValue {
        let mut params = object! {};
        params[self.module.table_key()] = br_fields::str::Key::new(true, self.module.table_key(), "ID", 32).field();
        params
    }

    fn index(&mut self, request: Request) -> ApiResponse {
        let id = request.body["id"].clone();
        let count = self.tools().db.table(self.module._table_name())
            .where_and("id", "=", id.clone())
            .delete();
        if count.as_i32().unwrap() == 0 {
            return ApiResponse::fail(111111,"{{title}}失败");
        }
        ApiResponse::success(count, "{{title}}成功")
    }
}