| 1 |
use super::{DateTime, PgPool, Utc, Uuid}; |
| 2 |
|
| 3 |
#[derive(sqlx::FromRow)] |
| 4 |
pub struct FootnoteWithAuthor { |
| 5 |
pub id: Uuid, |
| 6 |
pub post_id: Uuid, |
| 7 |
pub author_id: Uuid, |
| 8 |
pub author_name: String, |
| 9 |
pub author_username: String, |
| 10 |
pub body_html: String, |
| 11 |
pub created_at: DateTime<Utc>, |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
#[tracing::instrument(skip_all)] |
| 16 |
pub async fn list_footnotes_for_posts( |
| 17 |
pool: &PgPool, |
| 18 |
post_ids: &[Uuid], |
| 19 |
) -> Result<Vec<FootnoteWithAuthor>, sqlx::Error> { |
| 20 |
sqlx::query_as!( |
| 21 |
FootnoteWithAuthor, |
| 22 |
r#"SELECT f.id, f.post_id, f.author_id, |
| 23 |
COALESCE(u.display_name, u.username) AS "author_name!", |
| 24 |
u.username AS author_username, |
| 25 |
f.body_html, |
| 26 |
f.created_at AS "created_at: chrono::DateTime<chrono::Utc>" |
| 27 |
FROM post_footnotes f |
| 28 |
JOIN users u ON u.mnw_account_id = f.author_id |
| 29 |
WHERE f.post_id = ANY($1) |
| 30 |
ORDER BY f.created_at"#, |
| 31 |
post_ids, |
| 32 |
) |
| 33 |
.fetch_all(pool) |
| 34 |
.await |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
#[tracing::instrument(skip_all)] |
| 39 |
pub async fn count_footnotes_for_post(pool: &PgPool, post_id: Uuid) -> Result<i64, sqlx::Error> { |
| 40 |
sqlx::query_scalar!( |
| 41 |
r#"SELECT COUNT(*) AS "count!" FROM post_footnotes WHERE post_id = $1"#, |
| 42 |
post_id, |
| 43 |
) |
| 44 |
.fetch_one(pool) |
| 45 |
.await |
| 46 |
} |
| 47 |
|