]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/aggregates/comment_aggregates.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / aggregates / comment_aggregates.rs
index 56528a5fe6fa59f7307465797171329c50ae368f..12b57222805cc23e82484e98085016599d697d95 100644 (file)
@@ -2,19 +2,35 @@ use crate::{
   aggregates::structs::CommentAggregates,
   newtypes::CommentId,
   schema::comment_aggregates,
-  utils::{get_conn, DbPool},
+  utils::{functions::hot_rank, get_conn, DbPool},
 };
 use diesel::{result::Error, ExpressionMethods, QueryDsl};
 use diesel_async::RunQueryDsl;
 
 impl CommentAggregates {
-  pub async fn read(pool: &DbPool, comment_id: CommentId) -> Result<Self, Error> {
+  pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<Self, Error> {
     let conn = &mut get_conn(pool).await?;
     comment_aggregates::table
       .filter(comment_aggregates::comment_id.eq(comment_id))
       .first::<Self>(conn)
       .await
   }
+
+  pub async fn update_hot_rank(
+    pool: &mut DbPool<'_>,
+    comment_id: CommentId,
+  ) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
+
+    diesel::update(comment_aggregates::table)
+      .filter(comment_aggregates::comment_id.eq(comment_id))
+      .set(comment_aggregates::hot_rank.eq(hot_rank(
+        comment_aggregates::score,
+        comment_aggregates::published,
+      )))
+      .get_result::<Self>(conn)
+      .await
+  }
 }
 
 #[cfg(test)]
@@ -37,8 +53,11 @@ mod tests {
   #[serial]
   async fn test_crud() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
 
-    let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+    let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+      .await
+      .unwrap();
 
     let new_person = PersonInsertForm::builder()
       .name("thommy_comment_agg".into())