use chrono::{DateTime, Utc};
use sea_orm::prelude::DateTimeWithTimeZone;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseEntityFields {
pub id: i64,
pub uid: Uuid,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl BaseEntityFields {
pub fn new() -> Self {
let now = Utc::now();
Self {
id: 0,
uid: Uuid::new_v4(),
created_at: now,
updated_at: now,
}
}
}
impl Default for BaseEntityFields {
fn default() -> Self {
Self::new()
}
}
pub trait BaseEntity: Send + Sync {
type LoaderType;
fn load() -> Self::LoaderType;
fn id(&self) -> i64;
fn uid(&self) -> Uuid;
fn created_at(&self) -> DateTimeWithTimeZone;
fn updated_at(&self) -> DateTimeWithTimeZone;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseAuditableFields<U> {
pub base: BaseEntityFields,
pub created_by: U,
pub updated_by: U,
}
pub trait BaseAuditableEntity: BaseEntity {
type User: Send + Sync + Clone;
fn created_by(&self) -> &Self::User;
fn updated_by(&self) -> &Self::User;
}