Constant REPOSITORIES
Source pub const REPOSITORIES: &str = r#"use crate::models::UserEntity;
use chrono::Utc;
use rbatis::{raw_sql, RBatis};
use uuid::Uuid;
pub struct UserRepository {
rb: RBatis,
}
impl UserRepository {
pub fn new(rb: RBatis) -> Self {
Self { rb }
}
//----------------------------------
// Create a new user
//----------------------------------
raw_sql!(insert_user(rb: &RBatis, user: &UserEntity) -> rbatis::rbdc::db::ExecResult =>
"INSERT INTO users (id, username, email, password, created_at) VALUES (?, ?, ?, ?, ?)"
);
pub async fn create_user(
&self,
username: &str,
email: &str,
password: &str,
) -> Result<UserEntity, rbatis::Error> {
//-------------------------------------------
// Check for existing user
//-------------------------------------------
if let Some(_) = self.get_user_by_email(email).await? {
return Err(rbatis::Error::from("A user with this email already exists"));
}
let user = UserEntity {
id: Uuid::new_v4(),
username: username.to_string(),
email: email.to_string(),
password: password.to_string(),
created_at: Utc::now(),
};
Self::insert_user(&self.rb, &user).await?;
Ok(user)
}
//----------------------------------------------
// Get user by id
//----------------------------------------------
raw_sql!(get_by_id(rb: &RBatis, id: Uuid) -> Option<UserEntity> =>
"SELECT id, username, email, password, created_at FROM users WHERE id = ?"
);
pub async fn get_user_by_id(&self, id: Uuid) -> Result<Option<UserEntity>, rbatis::Error> {
Self::get_by_id(&self.rb, id).await
}
//-------------------------------------------------
// Get user by email
//-------------------------------------------------
raw_sql!(get_by_email(rb: &RBatis, email: &str) -> Option<UserEntity> =>
"SELECT id, username, email, password, created_at FROM users WHERE email = ?"
);
pub async fn get_user_by_email(
&self,
email: &str,
) -> Result<Option<UserEntity>, rbatis::Error> {
Self::get_by_email(&self.rb, email).await
}
//----------------------------------
// Update user
//----------------------------------
raw_sql!(update_user_sql(
rb: &RBatis,
id: Uuid,
username: &str,
email: &str,
password: &str
) -> rbatis::rbdc::db::ExecResult =>
"UPDATE users SET username = ?, email = ?, password = ? WHERE id = ?"
);
pub async fn update_user(
&self,
id: Uuid,
username: Option<&str>,
email: Option<&str>,
password: Option<&str>,
) -> Result<Option<UserEntity>, rbatis::Error> {
let mut user = match self.get_user_by_id(id).await? {
Some(u) => u,
None => return Ok(None),
};
if let Some(u) = username {
user.username = u.to_string();
}
if let Some(e) = email {
user.email = e.to_string();
}
if let Some(p) = password {
user.password = p.to_string();
}
Self::update_user_sql(
&self.rb,
user.id,
&user.username,
&user.email,
&user.password,
)
.await?;
Ok(Some(user))
}
//-------------------------
// Delete user
//-------------------------
raw_sql!(delete_user_sql(rb: &RBatis, id: Uuid) -> rbatis::rbdc::db::ExecResult =>
"DELETE FROM users WHERE id = ?"
);
pub async fn delete_user(&self, id: Uuid) -> Result<Option<UserEntity>, rbatis::Error> {
if let Some(user) = self.get_user_by_id(id).await? {
Self::delete_user_sql(&self.rb, id).await?;
Ok(Some(user))
} else {
Ok(None)
}
}
//--------------------------------------
// List all users
//--------------------------------------
raw_sql!(list_users_sql(rb: &RBatis) -> Vec<UserEntity> =>
"SELECT id, username, email, password, created_at FROM users"
);
pub async fn list_users(&self) -> Result<Vec<UserEntity>, rbatis::Error> {
Self::list_users_sql(&self.rb).await
}
}
"#;