Implement `db2::Database::remove_contact`

Antonio Scandurra created

Change summary

crates/collab/src/db2.rs       | 57 +++++++++++++++++++++--------------
crates/collab/src/db2/tests.rs | 12 +++---
2 files changed, 40 insertions(+), 29 deletions(-)

Detailed changes

crates/collab/src/db2.rs 🔗

@@ -366,29 +366,28 @@ impl Database {
     }
 
     pub async fn remove_contact(&self, requester_id: UserId, responder_id: UserId) -> Result<()> {
-        self.transact(|mut tx| async move {
-            // let (id_a, id_b) = if responder_id < requester_id {
-            //     (responder_id, requester_id)
-            // } else {
-            //     (requester_id, responder_id)
-            // };
-            // let query = "
-            //     DELETE FROM contacts
-            //     WHERE user_id_a = $1 AND user_id_b = $2;
-            // ";
-            // let result = sqlx::query(query)
-            //     .bind(id_a.0)
-            //     .bind(id_b.0)
-            //     .execute(&mut tx)
-            //     .await?;
-
-            // if result.rows_affected() == 1 {
-            //     tx.commit().await?;
-            //     Ok(())
-            // } else {
-            //     Err(anyhow!("no such contact"))?
-            // }
-            todo!()
+        self.transact(|tx| async move {
+            let (id_a, id_b) = if responder_id < requester_id {
+                (responder_id, requester_id)
+            } else {
+                (requester_id, responder_id)
+            };
+
+            let result = contact::Entity::delete_many()
+                .filter(
+                    contact::Column::UserIdA
+                        .eq(id_a)
+                        .and(contact::Column::UserIdB.eq(id_b)),
+                )
+                .exec(&tx)
+                .await?;
+
+            if result.rows_affected == 1 {
+                tx.commit().await?;
+                Ok(())
+            } else {
+                Err(anyhow!("no such contact"))?
+            }
         })
         .await
     }
@@ -488,6 +487,18 @@ impl Database {
         .await
     }
 
+    pub fn fuzzy_like_string(string: &str) -> String {
+        let mut result = String::with_capacity(string.len() * 2 + 1);
+        for c in string.chars() {
+            if c.is_alphanumeric() {
+                result.push('%');
+                result.push(c);
+            }
+        }
+        result.push('%');
+        result
+    }
+
     // projects
 
     pub async fn share_project(

crates/collab/src/db2/tests.rs 🔗

@@ -402,12 +402,12 @@ test_both_dbs!(test_metrics_id_postgres, test_metrics_id_sqlite, db, {
     assert_ne!(metrics_id1, metrics_id2);
 });
 
-// #[test]
-// fn test_fuzzy_like_string() {
-//     assert_eq!(DefaultDb::fuzzy_like_string("abcd"), "%a%b%c%d%");
-//     assert_eq!(DefaultDb::fuzzy_like_string("x y"), "%x%y%");
-//     assert_eq!(DefaultDb::fuzzy_like_string(" z  "), "%z%");
-// }
+#[test]
+fn test_fuzzy_like_string() {
+    assert_eq!(Database::fuzzy_like_string("abcd"), "%a%b%c%d%");
+    assert_eq!(Database::fuzzy_like_string("x y"), "%x%y%");
+    assert_eq!(Database::fuzzy_like_string(" z  "), "%z%");
+}
 
 // #[gpui::test]
 // async fn test_fuzzy_search_users() {