use super::edit_log; use sea_orm::{entity::prelude::*, ConnectionTrait, Set, TryIntoModel}; use serde::Serialize; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize)] #[sea_orm(table_name = "repository")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: String, pub owner: String, pub name: String, pub description: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} #[async_trait::async_trait] impl ActiveModelBehavior for ActiveModel { async fn before_save(self, db: &C, _: bool) -> Result where C: ConnectionTrait, { let model = self.clone().try_into_model()?; insert_edit_log("before_save", &model, db).await?; Ok(self) } async fn after_save(model: Model, db: &C, _: bool) -> Result where C: ConnectionTrait, { insert_edit_log("after_save", &model, db).await?; Ok(model) } async fn before_delete(self, db: &C) -> Result where C: ConnectionTrait, { let model = self.clone().try_into_model()?; insert_edit_log("before_delete", &model, db).await?; Ok(self) } async fn after_delete(self, db: &C) -> Result where C: ConnectionTrait, { let model = self.clone().try_into_model()?; insert_edit_log("after_delete", &model, db).await?; Ok(self) } } async fn insert_edit_log(action: T, model: &M, db: &C) -> Result<(), DbErr> where T: Into, M: Serialize, C: ConnectionTrait, { edit_log::ActiveModel { action: Set(action.into()), values: Set(serde_json::json!(model)), ..Default::default() } .insert(db) .await?; Ok(()) }