feat: enhance database configuration and media handling with PostgreSQL integration and storage quota management
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1231,7 +1231,9 @@ name = "libertas_infra"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
"chrono",
|
||||||
"libertas_core",
|
"libertas_core",
|
||||||
|
"serde",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|||||||
14
compose.yml
14
compose.yml
@@ -7,3 +7,17 @@ services:
|
|||||||
- "6222:6222"
|
- "6222:6222"
|
||||||
- "8222:8222"
|
- "8222:8222"
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
db:
|
||||||
|
image: postgres:17
|
||||||
|
container_name: libertas_db
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: libertas
|
||||||
|
POSTGRES_PASSWORD: libertas_password
|
||||||
|
POSTGRES_DB: libertas_db
|
||||||
|
ports:
|
||||||
|
- "5436:5432"
|
||||||
|
volumes:
|
||||||
|
- libertas_db_data:/var/lib/postgresql/data
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
libertas_db_data:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pub fn load_config() -> CoreResult<Config> {
|
|||||||
Ok(Config {
|
Ok(Config {
|
||||||
database: DatabaseConfig {
|
database: DatabaseConfig {
|
||||||
db_type: DatabaseType::Postgres,
|
db_type: DatabaseType::Postgres,
|
||||||
url: "postgres://postgres:postgres@localhost:5432/libertas_db".to_string(),
|
url: "postgres://libertas:libertas_password@localhost:5436/libertas_db".to_string(),
|
||||||
},
|
},
|
||||||
server_address: "127.0.0.1:8080".to_string(),
|
server_address: "127.0.0.1:8080".to_string(),
|
||||||
jwt_secret: "super_secret_jwt_key".to_string(),
|
jwt_secret: "super_secret_jwt_key".to_string(),
|
||||||
|
|||||||
@@ -48,13 +48,17 @@ impl MediaService for MediaServiceImpl {
|
|||||||
async fn upload_media(&self, mut data: UploadMediaData<'_>) -> CoreResult<Media> {
|
async fn upload_media(&self, mut data: UploadMediaData<'_>) -> CoreResult<Media> {
|
||||||
let (file_bytes, hash, file_size) = self.hash_and_buffer_stream(&mut data).await?;
|
let (file_bytes, hash, file_size) = self.hash_and_buffer_stream(&mut data).await?;
|
||||||
|
|
||||||
self.check_upload_prerequisites(data.owner_id, file_size, &hash)
|
let owner_id = data.owner_id;
|
||||||
|
let filename = data.filename;
|
||||||
|
let mime_type = data.mime_type;
|
||||||
|
|
||||||
|
self.check_upload_prerequisites(owner_id, file_size, &hash)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let storage_path = self.persist_media_file(&file_bytes, &data.filename).await?;
|
let storage_path = self.persist_media_file(&file_bytes, &filename).await?;
|
||||||
|
|
||||||
let media = self
|
let media = self
|
||||||
.persist_media_metadata(&data, storage_path, hash, file_size)
|
.persist_media_metadata(owner_id, filename, mime_type, storage_path, hash, file_size)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.publish_new_media_job(media.id).await?;
|
self.publish_new_media_job(media.id).await?;
|
||||||
@@ -237,17 +241,19 @@ impl MediaServiceImpl {
|
|||||||
|
|
||||||
async fn persist_media_metadata(
|
async fn persist_media_metadata(
|
||||||
&self,
|
&self,
|
||||||
data: &UploadMediaData<'_>,
|
owner_id: Uuid,
|
||||||
|
filename: String,
|
||||||
|
mime_type: String,
|
||||||
storage_path: String,
|
storage_path: String,
|
||||||
hash: String,
|
hash: String,
|
||||||
file_size: i64,
|
file_size: i64,
|
||||||
) -> CoreResult<Media> {
|
) -> CoreResult<Media> {
|
||||||
let media_model = Media {
|
let media_model = Media {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
owner_id: data.owner_id,
|
owner_id,
|
||||||
storage_path,
|
storage_path,
|
||||||
original_filename: data.filename.clone(),
|
original_filename: filename,
|
||||||
mime_type: data.mime_type.clone(),
|
mime_type,
|
||||||
hash,
|
hash,
|
||||||
created_at: chrono::Utc::now(),
|
created_at: chrono::Utc::now(),
|
||||||
extracted_location: None,
|
extracted_location: None,
|
||||||
@@ -257,7 +263,7 @@ impl MediaServiceImpl {
|
|||||||
|
|
||||||
self.repo.create(&media_model).await?;
|
self.repo.create(&media_model).await?;
|
||||||
self.user_repo
|
self.user_repo
|
||||||
.update_storage_used(data.owner_id, file_size)
|
.update_storage_used(owner_id, file_size)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(media_model)
|
Ok(media_model)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ bytes = "1.10.1"
|
|||||||
chrono = "0.4.42"
|
chrono = "0.4.42"
|
||||||
futures = "0.3.31"
|
futures = "0.3.31"
|
||||||
thiserror = "2.0.17"
|
thiserror = "2.0.17"
|
||||||
uuid = "1.18.1"
|
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
||||||
sqlx = { version = "0.8.6", features = [
|
sqlx = { version = "0.8.6", features = [
|
||||||
"runtime-tokio",
|
"runtime-tokio",
|
||||||
"postgres",
|
"postgres",
|
||||||
|
|||||||
@@ -14,3 +14,5 @@ sqlx = { version = "0.8.6", features = [
|
|||||||
] }
|
] }
|
||||||
async-trait = "0.1.89"
|
async-trait = "0.1.89"
|
||||||
uuid = { version = "1.18.1", features = ["v4"] }
|
uuid = { version = "1.18.1", features = ["v4"] }
|
||||||
|
chrono = "0.4.42"
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
|||||||
64
libertas_infra/src/db_models.rs
Normal file
64
libertas_infra/src/db_models.rs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
use chrono::Utc;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
|
||||||
|
#[sqlx(rename_all = "lowercase")]
|
||||||
|
#[sqlx(type_name = "TEXT")]
|
||||||
|
pub enum PostgresRole {
|
||||||
|
User,
|
||||||
|
Admin,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
pub struct PostgresUser {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub username: String,
|
||||||
|
pub email: String,
|
||||||
|
pub hashed_password: String,
|
||||||
|
pub created_at: chrono::DateTime<Utc>,
|
||||||
|
pub updated_at: chrono::DateTime<Utc>,
|
||||||
|
pub role: String,
|
||||||
|
pub storage_quota: i64,
|
||||||
|
pub storage_used: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
pub struct PostgresAlbum {
|
||||||
|
pub id: uuid::Uuid,
|
||||||
|
pub owner_id: uuid::Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub is_public: bool,
|
||||||
|
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||||
|
pub updated_at: chrono::DateTime<chrono::Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
pub struct PostgresMedia {
|
||||||
|
pub id: uuid::Uuid,
|
||||||
|
pub owner_id: uuid::Uuid,
|
||||||
|
pub storage_path: String,
|
||||||
|
pub original_filename: String,
|
||||||
|
pub mime_type: String,
|
||||||
|
pub hash: String,
|
||||||
|
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||||
|
pub extracted_location: Option<String>,
|
||||||
|
pub width: Option<i32>,
|
||||||
|
pub height: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, sqlx::Type, PartialEq, Eq, Deserialize)]
|
||||||
|
#[sqlx(rename_all = "lowercase")]
|
||||||
|
#[sqlx(type_name = "album_permission")]
|
||||||
|
pub enum PostgresAlbumPermission {
|
||||||
|
View,
|
||||||
|
Contribute,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PostgresAlbumShare {
|
||||||
|
pub album_id: uuid::Uuid,
|
||||||
|
pub user_id: uuid::Uuid,
|
||||||
|
pub permission: PostgresAlbumPermission,
|
||||||
|
}
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
pub mod factory;
|
pub mod factory;
|
||||||
pub mod repositories;
|
pub mod repositories;
|
||||||
|
pub mod db_models;
|
||||||
|
pub mod mappers;
|
||||||
96
libertas_infra/src/mappers.rs
Normal file
96
libertas_infra/src/mappers.rs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
use libertas_core::models::{Album, AlbumPermission, AlbumShare, Media, Role, User};
|
||||||
|
|
||||||
|
use crate::db_models::{PostgresAlbum, PostgresAlbumPermission, PostgresAlbumShare, PostgresMedia, PostgresRole, PostgresUser};
|
||||||
|
|
||||||
|
impl From<PostgresRole> for Role {
|
||||||
|
fn from(pg_role: PostgresRole) -> Self {
|
||||||
|
match pg_role {
|
||||||
|
PostgresRole::User => Role::User,
|
||||||
|
PostgresRole::Admin => Role::Admin,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Role> for PostgresRole {
|
||||||
|
fn from(role: Role) -> Self {
|
||||||
|
match role {
|
||||||
|
Role::User => PostgresRole::User,
|
||||||
|
Role::Admin => PostgresRole::Admin,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PostgresUser> for User {
|
||||||
|
fn from(pg_user: PostgresUser) -> Self {
|
||||||
|
User {
|
||||||
|
id: pg_user.id,
|
||||||
|
username: pg_user.username,
|
||||||
|
email: pg_user.email,
|
||||||
|
hashed_password: pg_user.hashed_password,
|
||||||
|
created_at: pg_user.created_at,
|
||||||
|
updated_at: pg_user.updated_at,
|
||||||
|
role: Role::from(pg_user.role.as_str()),
|
||||||
|
storage_quota: pg_user.storage_quota,
|
||||||
|
storage_used: pg_user.storage_used,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PostgresAlbum> for Album {
|
||||||
|
fn from(pg_album: PostgresAlbum) -> Self {
|
||||||
|
Album {
|
||||||
|
id: pg_album.id,
|
||||||
|
owner_id: pg_album.owner_id,
|
||||||
|
name: pg_album.name,
|
||||||
|
description: pg_album.description,
|
||||||
|
is_public: pg_album.is_public,
|
||||||
|
created_at: pg_album.created_at,
|
||||||
|
updated_at: pg_album.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PostgresMedia> for Media {
|
||||||
|
fn from(pg_media: PostgresMedia) -> Self {
|
||||||
|
Media {
|
||||||
|
id: pg_media.id,
|
||||||
|
owner_id: pg_media.owner_id,
|
||||||
|
storage_path: pg_media.storage_path,
|
||||||
|
original_filename: pg_media.original_filename,
|
||||||
|
mime_type: pg_media.mime_type,
|
||||||
|
hash: pg_media.hash,
|
||||||
|
created_at: pg_media.created_at,
|
||||||
|
extracted_location: pg_media.extracted_location,
|
||||||
|
width: pg_media.width,
|
||||||
|
height: pg_media.height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PostgresAlbumPermission> for AlbumPermission {
|
||||||
|
fn from(pg_permission: PostgresAlbumPermission) -> Self {
|
||||||
|
match pg_permission {
|
||||||
|
PostgresAlbumPermission::View => AlbumPermission::View,
|
||||||
|
PostgresAlbumPermission::Contribute => AlbumPermission::Contribute,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<AlbumPermission> for PostgresAlbumPermission {
|
||||||
|
fn from(permission: AlbumPermission) -> Self {
|
||||||
|
match permission {
|
||||||
|
AlbumPermission::View => PostgresAlbumPermission::View,
|
||||||
|
AlbumPermission::Contribute => PostgresAlbumPermission::Contribute,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PostgresAlbumShare> for AlbumShare {
|
||||||
|
fn from(pg_share: PostgresAlbumShare) -> Self {
|
||||||
|
AlbumShare {
|
||||||
|
album_id: pg_share.album_id,
|
||||||
|
user_id: pg_share.user_id,
|
||||||
|
permission: AlbumPermission::from(pg_share.permission),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ use libertas_core::{
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::db_models::PostgresAlbum;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PostgresAlbumRepository {
|
pub struct PostgresAlbumRepository {
|
||||||
pool: PgPool,
|
pool: PgPool,
|
||||||
@@ -42,8 +44,8 @@ impl AlbumRepository for PostgresAlbumRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Album>> {
|
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Album>> {
|
||||||
sqlx::query_as!(
|
let pg_album = sqlx::query_as!(
|
||||||
Album,
|
PostgresAlbum,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, name, description, is_public, created_at, updated_at
|
SELECT id, owner_id, name, description, is_public, created_at, updated_at
|
||||||
FROM albums
|
FROM albums
|
||||||
@@ -53,12 +55,13 @@ impl AlbumRepository for PostgresAlbumRepository {
|
|||||||
)
|
)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
Ok(pg_album.map(|a| a.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Album>> {
|
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Album>> {
|
||||||
sqlx::query_as!(
|
let pg_albums = sqlx::query_as!(
|
||||||
Album,
|
PostgresAlbum,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, name, description, is_public, created_at, updated_at
|
SELECT id, owner_id, name, description, is_public, created_at, updated_at
|
||||||
FROM albums
|
FROM albums
|
||||||
@@ -68,7 +71,9 @@ impl AlbumRepository for PostgresAlbumRepository {
|
|||||||
)
|
)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(pg_albums.into_iter().map(|a| a.into()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_media_to_album(&self, album_id: Uuid, media_ids: &[Uuid]) -> CoreResult<()> {
|
async fn add_media_to_album(&self, album_id: Uuid, media_ids: &[Uuid]) -> CoreResult<()> {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use libertas_core::{
|
use libertas_core::{
|
||||||
error::{CoreError, CoreResult},
|
error::{CoreError, CoreResult}, models::AlbumPermission, repositories::AlbumShareRepository
|
||||||
models::AlbumPermission,
|
|
||||||
repositories::AlbumShareRepository,
|
|
||||||
};
|
};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use crate::db_models::PostgresAlbumPermission;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PostgresAlbumShareRepository {
|
pub struct PostgresAlbumShareRepository {
|
||||||
@@ -35,7 +34,7 @@ impl AlbumShareRepository for PostgresAlbumShareRepository {
|
|||||||
"#,
|
"#,
|
||||||
album_id,
|
album_id,
|
||||||
user_id,
|
user_id,
|
||||||
permission.as_str() as "album_permission"
|
PostgresAlbumPermission::from(permission) as PostgresAlbumPermission,
|
||||||
)
|
)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.await
|
||||||
@@ -49,9 +48,12 @@ impl AlbumShareRepository for PostgresAlbumShareRepository {
|
|||||||
album_id: Uuid,
|
album_id: Uuid,
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
) -> CoreResult<Option<AlbumPermission>> {
|
) -> CoreResult<Option<AlbumPermission>> {
|
||||||
let row = sqlx::query!(
|
let result = sqlx::query!(
|
||||||
// --- FIX 2: CAST the enum to TEXT in the SQL ---
|
r#"
|
||||||
"SELECT permission::TEXT as permission FROM album_shares WHERE album_id = $1 AND user_id = $2",
|
SELECT permission as "permission: PostgresAlbumPermission"
|
||||||
|
FROM album_shares
|
||||||
|
WHERE album_id = $1 AND user_id = $2
|
||||||
|
"#,
|
||||||
album_id,
|
album_id,
|
||||||
user_id
|
user_id
|
||||||
)
|
)
|
||||||
@@ -59,8 +61,7 @@ impl AlbumShareRepository for PostgresAlbumShareRepository {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
// This now works because r.permission is a String
|
Ok(result.map(|row| row.permission.into()))
|
||||||
Ok(row.map(|r| AlbumPermission::from(r.permission.as_str())))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn is_media_in_shared_album(&self, media_id: Uuid, user_id: Uuid) -> CoreResult<bool> {
|
async fn is_media_in_shared_album(&self, media_id: Uuid, user_id: Uuid) -> CoreResult<bool> {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ use libertas_core::{
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::db_models::PostgresMedia;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PostgresMediaRepository {
|
pub struct PostgresMediaRepository {
|
||||||
pool: PgPool,
|
pool: PgPool,
|
||||||
@@ -44,8 +46,8 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn find_by_hash(&self, hash: &str) -> CoreResult<Option<Media>> {
|
async fn find_by_hash(&self, hash: &str) -> CoreResult<Option<Media>> {
|
||||||
sqlx::query_as!(
|
let pg_media = sqlx::query_as!(
|
||||||
Media,
|
PostgresMedia,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||||
extracted_location, width, height
|
extracted_location, width, height
|
||||||
@@ -56,12 +58,14 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
)
|
)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(pg_media.map(|m| m.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Media>> {
|
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Media>> {
|
||||||
sqlx::query_as!(
|
let pg_media = sqlx::query_as!(
|
||||||
Media,
|
PostgresMedia,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||||
extracted_location, width, height
|
extracted_location, width, height
|
||||||
@@ -72,12 +76,14 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
)
|
)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(pg_media.map(|m| m.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Media>> {
|
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Media>> {
|
||||||
sqlx::query_as!(
|
let pg_media = sqlx::query_as!(
|
||||||
Media,
|
PostgresMedia,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||||
extracted_location, width, height
|
extracted_location, width, height
|
||||||
@@ -88,7 +94,9 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
)
|
)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(pg_media.into_iter().map(|m| m.into()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_metadata(
|
async fn update_metadata(
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use libertas_core::{
|
use libertas_core::{
|
||||||
error::{CoreError, CoreResult},
|
error::{CoreError, CoreResult},
|
||||||
models::{Role, User},
|
models::User,
|
||||||
repositories::UserRepository,
|
repositories::UserRepository,
|
||||||
};
|
};
|
||||||
use sqlx::{PgPool, SqlitePool, types::Uuid};
|
use sqlx::{PgPool, SqlitePool, types::Uuid};
|
||||||
|
|
||||||
|
use crate::db_models::PostgresUser;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PostgresUserRepository {
|
pub struct PostgresUserRepository {
|
||||||
pool: PgPool,
|
pool: PgPool,
|
||||||
@@ -54,69 +56,61 @@ impl UserRepository for PostgresUserRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn find_by_email(&self, email: &str) -> CoreResult<Option<User>> {
|
async fn find_by_email(&self, email: &str) -> CoreResult<Option<User>> {
|
||||||
let row = sqlx::query!(
|
let pg_user = sqlx::query_as!(
|
||||||
r#"SELECT id, username, email, hashed_password, created_at, updated_at, role, storage_quota, storage_used FROM users WHERE email = $1"#,
|
PostgresUser,
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
id, username, email, hashed_password, created_at, updated_at,
|
||||||
|
role,
|
||||||
|
storage_quota, storage_used
|
||||||
|
FROM users
|
||||||
|
WHERE email = $1
|
||||||
|
"#,
|
||||||
email
|
email
|
||||||
)
|
)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
Ok(row.map(|r| User {
|
Ok(pg_user.map(|u| u.into()))
|
||||||
id: r.id,
|
|
||||||
username: r.username,
|
|
||||||
email: r.email,
|
|
||||||
hashed_password: r.hashed_password,
|
|
||||||
created_at: r.created_at,
|
|
||||||
updated_at: r.updated_at,
|
|
||||||
role: Role::from(r.role.as_str()),
|
|
||||||
storage_quota: r.storage_quota,
|
|
||||||
storage_used: r.storage_used,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_by_username(&self, username: &str) -> CoreResult<Option<User>> {
|
async fn find_by_username(&self, username: &str) -> CoreResult<Option<User>> {
|
||||||
let row = sqlx::query!(
|
let pg_user = sqlx::query_as!(
|
||||||
r#"SELECT id, username, email, hashed_password, created_at, updated_at, role, storage_quota, storage_used FROM users WHERE username = $1"#,
|
PostgresUser,
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
id, username, email, hashed_password, created_at, updated_at,
|
||||||
|
role, storage_quota, storage_used
|
||||||
|
FROM users
|
||||||
|
WHERE username = $1
|
||||||
|
"#,
|
||||||
username
|
username
|
||||||
)
|
)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
Ok(row.map(|r| User {
|
Ok(pg_user.map(|u| u.into()))
|
||||||
id: r.id,
|
|
||||||
username: r.username,
|
|
||||||
email: r.email,
|
|
||||||
hashed_password: r.hashed_password,
|
|
||||||
created_at: r.created_at,
|
|
||||||
updated_at: r.updated_at,
|
|
||||||
role: Role::from(r.role.as_str()),
|
|
||||||
storage_quota: r.storage_quota,
|
|
||||||
storage_used: r.storage_used,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<User>> {
|
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<User>> {
|
||||||
let row = sqlx::query!(
|
let pg_user = sqlx::query_as!(
|
||||||
r#"SELECT id, username, email, hashed_password, created_at, updated_at, role, storage_quota, storage_used FROM users WHERE id = $1"#,
|
PostgresUser,
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
id, username, email, hashed_password, created_at, updated_at,
|
||||||
|
role,
|
||||||
|
storage_quota, storage_used
|
||||||
|
FROM users
|
||||||
|
WHERE id = $1
|
||||||
|
"#,
|
||||||
id
|
id
|
||||||
)
|
)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
Ok(pg_user.map(|u| u.into()))
|
||||||
Ok(row.map(|r| User {
|
|
||||||
id: r.id,
|
|
||||||
username: r.username,
|
|
||||||
email: r.email,
|
|
||||||
hashed_password: r.hashed_password,
|
|
||||||
created_at: r.created_at,
|
|
||||||
updated_at: r.updated_at,
|
|
||||||
role: Role::from(r.role.as_str()),
|
|
||||||
storage_quota: r.storage_quota,
|
|
||||||
storage_used: r.storage_used,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_storage_used(&self, user_id: Uuid, bytes: i64) -> CoreResult<()> {
|
async fn update_storage_used(&self, user_id: Uuid, bytes: i64) -> CoreResult<()> {
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ pub fn load_config() -> CoreResult<Config> {
|
|||||||
Ok(Config {
|
Ok(Config {
|
||||||
database: DatabaseConfig {
|
database: DatabaseConfig {
|
||||||
db_type: DatabaseType::Postgres,
|
db_type: DatabaseType::Postgres,
|
||||||
url: "postgres://postgres:postgres@localhost:5432/libertas_db".to_string(),
|
url: "postgres://libertas:libertas_password@localhost:5436/libertas_db".to_string(),
|
||||||
},
|
},
|
||||||
server_address: "127.0.0.1:8080".to_string(),
|
server_address: "127.0.0.1:8080".to_string(),
|
||||||
jwt_secret: "super_secret_jwt_key".to_string(),
|
jwt_secret: "super_secret_jwt_key".to_string(),
|
||||||
media_library_path: "media_library".to_string(),
|
media_library_path: "media_library".to_string(),
|
||||||
broker_url: "nats://localhost:4222".to_string(),
|
broker_url: "nats://localhost:4222".to_string(),
|
||||||
max_upload_size_mb: Some(100),
|
max_upload_size_mb: Some(100),
|
||||||
|
default_storage_quota_gb: Some(10),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user