feat(libertas_api): add dependency on libertas_infra and async-nats refactor(libertas_api): consolidate config loading and add broker_url refactor(libertas_api): integrate NATS client into app state and services feat(libertas_core): introduce config module for database and server settings fix(libertas_core): enhance error handling with detailed messages feat(libertas_infra): create infrastructure layer with database repositories feat(libertas_infra): implement Postgres repositories for media and albums feat(libertas_worker): add worker service to process media jobs via NATS
97 lines
2.6 KiB
Rust
97 lines
2.6 KiB
Rust
use async_trait::async_trait;
|
|
use libertas_core::{
|
|
error::{CoreError, CoreResult},
|
|
models::User,
|
|
repositories::UserRepository,
|
|
};
|
|
use sqlx::{PgPool, SqlitePool, types::Uuid};
|
|
|
|
#[derive(Clone)]
|
|
pub struct PostgresUserRepository {
|
|
pool: PgPool,
|
|
}
|
|
|
|
impl PostgresUserRepository {
|
|
pub fn new(pool: PgPool) -> Self {
|
|
Self { pool }
|
|
}
|
|
}
|
|
|
|
#[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)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
"#,
|
|
user.id,
|
|
user.username,
|
|
user.email,
|
|
user.hashed_password,
|
|
user.created_at,
|
|
user.updated_at
|
|
)
|
|
.execute(&self.pool)
|
|
.await
|
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn find_by_email(&self, email: &str) -> CoreResult<Option<User>> {
|
|
sqlx::query_as!(User, "SELECT * FROM users WHERE email = $1", email)
|
|
.fetch_optional(&self.pool)
|
|
.await
|
|
.map_err(|e| CoreError::Database(e.to_string()))
|
|
}
|
|
|
|
async fn find_by_username(&self, username: &str) -> CoreResult<Option<User>> {
|
|
sqlx::query_as!(User, "SELECT * FROM users WHERE username = $1", username)
|
|
.fetch_optional(&self.pool)
|
|
.await
|
|
.map_err(|e| CoreError::Database(e.to_string()))
|
|
}
|
|
|
|
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<User>> {
|
|
sqlx::query_as!(User, "SELECT * FROM users WHERE id = $1", id)
|
|
.fetch_optional(&self.pool)
|
|
.await
|
|
.map_err(|e| CoreError::Database(e.to_string()))
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl UserRepository for SqliteUserRepository {
|
|
async fn create(&self, _user: User) -> CoreResult<()> {
|
|
println!("SQLITE REPO: Creating user");
|
|
Ok(())
|
|
}
|
|
|
|
async fn find_by_email(&self, _email: &str) -> CoreResult<Option<User>> {
|
|
println!("SQLITE REPO: Finding user by email");
|
|
Ok(None)
|
|
}
|
|
|
|
async fn find_by_username(&self, _username: &str) -> CoreResult<Option<User>> {
|
|
println!("SQLITE REPO: Finding user by username");
|
|
Ok(None)
|
|
}
|
|
|
|
async fn find_by_id(&self, _id: Uuid) -> CoreResult<Option<User>> {
|
|
println!("SQLITE REPO: Finding user by id");
|
|
Ok(None)
|
|
}
|
|
}
|