use br_db::Mode;
use br_fields::Field;
use json::{JsonValue, object};
use crate::{Action, Model, Response};
use crate::test::test::TestTest;

pub struct TestTestDelete {
    pub model: TestTest,
}

impl Action for TestTestDelete {
    fn title(&mut self) -> &'static str { "删除" }
    fn name(&mut self) -> &'static str { "test.test.delete" }
    fn token(&mut self) -> bool { true }
    fn params(&mut self) -> JsonValue {
        let mut params = object! {};
        params[self.model.primary_key()] = br_fields::str::Key::new(true, self.model.primary_key(), "ID", 20).field();
        return params;
    }

    fn index(&mut self, _header: JsonValue, request: JsonValue) -> Response {
        let id = request["id"].clone();
        let count = self.tools().db.table(self.model.table())
            .where_and("id", "=", id.clone())
            .delete();
        if count.as_i32().unwrap() > 0 {
            return self.success(object! {}, "删除成功");
        }
        return self.fail("删除失败");
    }
}