use chrono::{DateTime, Utc};
use serde_json::{Value, json};

pub struct Log {
    pub id: i32,
    pub msg: String,
    pub params: Value,
    pub create_date: Option<DateTime<Utc>>,
}

impl Log {
    pub async fn insert(&self) -> bool {
        let res = crate::db::db_query(
            r#"insert into tb_log(msg,params,create_date) values(?,?::jsonb,now());"#,
            vec![json!(&self.msg), json!(&self.params)],
        )
        .await;
        match res {
            Ok(res) => {
                // dbg!(&res);
                true
            }
            Err(err) => {
                // dbg!(&err);
                false
            }
        }
    }
}
