| 1 |
use super::{PgPool, Uuid}; |
| 2 |
|
| 3 |
#[derive(sqlx::FromRow)] |
| 4 |
pub struct EndorsementRow { |
| 5 |
pub post_id: Uuid, |
| 6 |
pub endorser_id: Uuid, |
| 7 |
} |
| 8 |
|
| 9 |
|
| 10 |
#[derive(sqlx::FromRow)] |
| 11 |
pub struct EndorsementCount { |
| 12 |
pub post_id: Uuid, |
| 13 |
pub count: i64, |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[tracing::instrument(skip_all)] |
| 24 |
pub async fn list_endorsements_for_posts( |
| 25 |
pool: &PgPool, |
| 26 |
post_ids: &[Uuid], |
| 27 |
) -> Result<Vec<EndorsementRow>, sqlx::Error> { |
| 28 |
sqlx::query_as!( |
| 29 |
EndorsementRow, |
| 30 |
"SELECT post_id, endorser_id FROM post_endorsements WHERE post_id = ANY($1)", |
| 31 |
post_ids, |
| 32 |
) |
| 33 |
.fetch_all(pool) |
| 34 |
.await |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#[tracing::instrument(skip_all)] |
| 44 |
pub async fn count_endorsements_for_posts( |
| 45 |
pool: &PgPool, |
| 46 |
post_ids: &[Uuid], |
| 47 |
) -> Result<Vec<EndorsementCount>, sqlx::Error> { |
| 48 |
sqlx::query_as!( |
| 49 |
EndorsementCount, |
| 50 |
r#"SELECT post_id, COUNT(*) AS "count!" |
| 51 |
FROM post_endorsements |
| 52 |
WHERE post_id = ANY($1) |
| 53 |
GROUP BY post_id"#, |
| 54 |
post_ids, |
| 55 |
) |
| 56 |
.fetch_all(pool) |
| 57 |
.await |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[tracing::instrument(skip_all)] |
| 64 |
pub async fn list_user_endorsed_posts( |
| 65 |
pool: &PgPool, |
| 66 |
post_ids: &[Uuid], |
| 67 |
user_id: Uuid, |
| 68 |
) -> Result<Vec<Uuid>, sqlx::Error> { |
| 69 |
sqlx::query_scalar!( |
| 70 |
"SELECT post_id FROM post_endorsements WHERE post_id = ANY($1) AND endorser_id = $2", |
| 71 |
post_ids, |
| 72 |
user_id, |
| 73 |
) |
| 74 |
.fetch_all(pool) |
| 75 |
.await |
| 76 |
} |
| 77 |
|