use async_trait::async_trait;
use libertas_core::{
error::{CoreError, CoreResult},
models::User,
repositories::UserRepository,
};
use sqlx::{Executor, PgPool, Postgres, SqlitePool, types::Uuid};
use crate::db_models::PostgresUser;
#[derive(Clone)]
pub struct PostgresUserRepository {
pool: PgPool,
}
impl PostgresUserRepository {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
pub(crate) async fn update_storage_used_internal<'a>(
exec: impl Executor<'a, Database = Postgres>,
user_id: Uuid,
bytes: i64,
) -> CoreResult<()> {
sqlx::query!(
r#"
UPDATE users
SET storage_used = storage_used + $1, updated_at = NOW()
WHERE id = $2
"#,
bytes,
user_id
)
.execute(exec)
.await
.map_err(|e| CoreError::Database(e.to_string()))?;
Ok(())
}
}
#[derive(Clone)]
pub struct SqliteUserRepository {
_pool: SqlitePool,
}
impl SqliteUserRepository {
pub fn new(pool: SqlitePool) -> Self {
Self { _pool: pool }
}
}
#[async_trait]
impl UserRepository for PostgresUserRepository {
async fn create(&self, user: User) -> CoreResult<()> {
sqlx::query!(
r#"
INSERT INTO users (id, username, email, hashed_password, created_at, updated_at, role, storage_quota, storage_used)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
"#,
user.id,
user.username,
user.email,
user.hashed_password,
user.created_at,
user.updated_at,
user.role.as_str(),
user.storage_quota,
user.storage_used
)
.execute(&self.pool)
.await
.map_err(|e| CoreError::Database(e.to_string()))?;
Ok(())
}
async fn find_by_email(&self, email: &str) -> CoreResult