use super::connection::DatabasePool;
use async_trait::async_trait;
#[async_trait]
pub trait ModelLifecycle: Sized + Send + Sync {
async fn saving(&mut self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn saved(&self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn creating(&mut self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn created(&self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn updating(&mut self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn updated(&self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn deleting(&self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn deleted(&self, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
}
#[async_trait]
pub trait Observer<T>: Send + Sync {
async fn saving(&self, _model: &mut T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn saved(&self, _model: &T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn creating(&self, _model: &mut T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn created(&self, _model: &T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn updating(&self, _model: &mut T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn updated(&self, _model: &T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn deleting(&self, _model: &T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn deleted(&self, _model: &T, _pool: &DatabasePool) -> Result<(), sqlx::Error> {
Ok(())
}
}
pub struct AutoSlug;
impl AutoSlug {
pub fn slugify(text: &str) -> String {
let mut slug = String::with_capacity(text.len());
let mut prev_is_dash = false;
for c in text.chars() {
if c.is_alphanumeric() {
slug.push(c.to_ascii_lowercase());
prev_is_dash = false;
} else if (c == ' ' || c == '-' || c == '_' || c == '/') && !prev_is_dash && !slug.is_empty() {
slug.push('-');
prev_is_dash = true;
}
}
if slug.ends_with('-') {
slug.pop();
}
if slug.is_empty() {
"n-a".to_string()
} else {
slug
}
}
pub async fn generate(
pool: &DatabasePool,
table: &str,
source_text: &str,
exclude_id: Option<i64>,
) -> Result<String, sqlx::Error> {
let base_slug = Self::slugify(source_text);
let mut candidate = base_slug.clone();
let mut counter = 1;
loop {
let (query, count) = if let Some(id) = exclude_id {
let sql = format!("SELECT COUNT(*) as count FROM {} WHERE slug = ? AND id != ?", table);
let count: i64 = sqlx::query_scalar(&pool.prepare_sql(&sql))
.bind(&candidate)
.bind(id)
.fetch_one(&pool.pool)
.await?;
(sql, count)
} else {
let sql = format!("SELECT COUNT(*) as count FROM {} WHERE slug = ?", table);
let count: i64 = sqlx::query_scalar(&pool.prepare_sql(&sql))
.bind(&candidate)
.fetch_one(&pool.pool)
.await?;
(sql, count)
};
let _ = query;
if count == 0 {
return Ok(candidate);
}
candidate = format!("{}-{}", base_slug, counter);
counter += 1;
}
}
}
pub struct AutoHash;
impl AutoHash {
pub fn is_hashed(val: &str) -> bool {
val.starts_with("$2a$") || val.starts_with("$2b$") || val.starts_with("$2y$")
}
pub fn hash(plain_or_hashed: &str) -> Result<String, bcrypt::BcryptError> {
if Self::is_hashed(plain_or_hashed) {
Ok(plain_or_hashed.to_string())
} else {
bcrypt::hash(plain_or_hashed, 10)
}
}
}