use chrono::{DateTime, Utc}; use serde::Serialize; use sqlx::PgPool; use uuid::Uuid; #[derive(Debug, sqlx::FromRow)] pub struct Upload { pub id: Uuid, pub event_id: Uuid, pub user_id: Uuid, pub original_path: String, pub preview_path: Option, pub thumbnail_path: Option, pub mime_type: String, pub original_size_bytes: i64, pub caption: Option, pub created_at: DateTime, pub deleted_at: Option>, } #[derive(Debug, Serialize)] pub struct UploadDto { pub id: Uuid, pub user_id: Uuid, pub uploader_name: String, pub preview_url: Option, pub thumbnail_url: Option, pub mime_type: String, pub caption: Option, pub hashtags: Vec, pub like_count: i64, pub comment_count: i64, pub liked_by_me: bool, pub created_at: DateTime, } impl Upload { pub async fn create( pool: &PgPool, event_id: Uuid, user_id: Uuid, original_path: &str, mime_type: &str, original_size_bytes: i64, caption: Option<&str>, ) -> Result { sqlx::query_as::<_, Self>( "INSERT INTO upload (event_id, user_id, original_path, mime_type, original_size_bytes, caption) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *", ) .bind(event_id) .bind(user_id) .bind(original_path) .bind(mime_type) .bind(original_size_bytes) .bind(caption) .fetch_one(pool) .await } pub async fn find_by_id(pool: &PgPool, id: Uuid) -> Result, sqlx::Error> { sqlx::query_as::<_, Self>( "SELECT * FROM upload WHERE id = $1 AND deleted_at IS NULL", ) .bind(id) .fetch_optional(pool) .await } pub async fn set_preview_path( pool: &PgPool, id: Uuid, preview_path: &str, ) -> Result<(), sqlx::Error> { sqlx::query("UPDATE upload SET preview_path = $2 WHERE id = $1") .bind(id) .bind(preview_path) .execute(pool) .await?; Ok(()) } pub async fn set_thumbnail_path( pool: &PgPool, id: Uuid, thumbnail_path: &str, ) -> Result<(), sqlx::Error> { sqlx::query("UPDATE upload SET thumbnail_path = $2 WHERE id = $1") .bind(id) .bind(thumbnail_path) .execute(pool) .await?; Ok(()) } pub async fn soft_delete(pool: &PgPool, id: Uuid) -> Result<(), sqlx::Error> { sqlx::query("UPDATE upload SET deleted_at = NOW() WHERE id = $1") .bind(id) .execute(pool) .await?; Ok(()) } pub async fn update_caption( pool: &PgPool, id: Uuid, caption: Option<&str>, ) -> Result<(), sqlx::Error> { sqlx::query("UPDATE upload SET caption = $2 WHERE id = $1") .bind(id) .bind(caption) .execute(pool) .await?; Ok(()) } }