| 1 |
use super::{PgPool, Uuid}; |
| 2 |
|
| 3 |
|
| 4 |
#[tracing::instrument(skip_all)] |
| 5 |
pub async fn insert_image( |
| 6 |
pool: &PgPool, |
| 7 |
uploader_id: Uuid, |
| 8 |
community_id: Uuid, |
| 9 |
s3_key: &str, |
| 10 |
filename: &str, |
| 11 |
content_type: &str, |
| 12 |
size_bytes: i64, |
| 13 |
) -> Result<Uuid, sqlx::Error> { |
| 14 |
sqlx::query_scalar!( |
| 15 |
"INSERT INTO images (uploader_id, community_id, s3_key, filename, content_type, size_bytes) |
| 16 |
VALUES ($1, $2, $3, $4, $5, $6) |
| 17 |
RETURNING id", |
| 18 |
uploader_id, |
| 19 |
community_id, |
| 20 |
s3_key, |
| 21 |
filename, |
| 22 |
content_type, |
| 23 |
size_bytes, |
| 24 |
) |
| 25 |
.fetch_one(pool) |
| 26 |
.await |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
#[tracing::instrument(skip_all)] |
| 31 |
pub async fn remove_image<'e, E: sqlx::PgExecutor<'e>>( |
| 32 |
executor: E, |
| 33 |
image_id: Uuid, |
| 34 |
removed_by: Uuid, |
| 35 |
) -> Result<(), sqlx::Error> { |
| 36 |
sqlx::query!( |
| 37 |
"UPDATE images SET removed_at = now(), removed_by = $2 WHERE id = $1 AND removed_at IS NULL", |
| 38 |
image_id, |
| 39 |
removed_by, |
| 40 |
) |
| 41 |
.execute(executor) |
| 42 |
.await?; |
| 43 |
Ok(()) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#[tracing::instrument(skip_all)] |
| 50 |
pub async fn delete_image_row(pool: &PgPool, image_id: Uuid) -> Result<(), sqlx::Error> { |
| 51 |
sqlx::query!("DELETE FROM images WHERE id = $1", image_id) |
| 52 |
.execute(pool) |
| 53 |
.await?; |
| 54 |
Ok(()) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[tracing::instrument(skip_all)] |
| 61 |
pub async fn mark_images_s3_purged(pool: &PgPool, image_ids: &[Uuid]) -> Result<(), sqlx::Error> { |
| 62 |
if image_ids.is_empty() { |
| 63 |
return Ok(()); |
| 64 |
} |
| 65 |
sqlx::query!( |
| 66 |
"UPDATE images SET s3_purged_at = now() WHERE id = ANY($1)", |
| 67 |
image_ids |
| 68 |
) |
| 69 |
.execute(pool) |
| 70 |
.await?; |
| 71 |
Ok(()) |
| 72 |
} |
| 73 |
|