Implement db2::Database::remove_contact

This commit is contained in:
Antonio Scandurra 2022-11-30 17:39:17 +01:00
parent d1a44b889e
commit 4c04d512db
2 changed files with 39 additions and 28 deletions

View file

@ -366,29 +366,28 @@ impl Database {
} }
pub async fn remove_contact(&self, requester_id: UserId, responder_id: UserId) -> Result<()> { pub async fn remove_contact(&self, requester_id: UserId, responder_id: UserId) -> Result<()> {
self.transact(|mut tx| async move { self.transact(|tx| async move {
// let (id_a, id_b) = if responder_id < requester_id { let (id_a, id_b) = if responder_id < requester_id {
// (responder_id, requester_id) (responder_id, requester_id)
// } else { } else {
// (requester_id, responder_id) (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 { let result = contact::Entity::delete_many()
// tx.commit().await?; .filter(
// Ok(()) contact::Column::UserIdA
// } else { .eq(id_a)
// Err(anyhow!("no such contact"))? .and(contact::Column::UserIdB.eq(id_b)),
// } )
todo!() .exec(&tx)
.await?;
if result.rows_affected == 1 {
tx.commit().await?;
Ok(())
} else {
Err(anyhow!("no such contact"))?
}
}) })
.await .await
} }
@ -488,6 +487,18 @@ impl Database {
.await .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 // projects
pub async fn share_project( pub async fn share_project(

View file

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