Search changes
This commit is contained in:
parent
b6c71ee35b
commit
bac5584b46
@ -1,9 +1,10 @@
|
||||
SELECT
|
||||
user_id, username, email
|
||||
FROM
|
||||
users
|
||||
ORDER BY
|
||||
user_id, username, email,
|
||||
GREATEST (
|
||||
similarity (email, $1),
|
||||
similarity (username, $1)
|
||||
) DESC
|
||||
) as "similarity!"
|
||||
FROM
|
||||
users
|
||||
ORDER BY
|
||||
"similarity!" DESC
|
@ -95,9 +95,17 @@ pub async fn get_hash(search_string: &str, pool: &Pool) -> sqlx::Result<Option<(
|
||||
Ok(record.map(|record| (record.user_id, record.hashed_password)))
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct UserSearch {
|
||||
pub user_id: i32,
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
pub similarity: f32,
|
||||
}
|
||||
|
||||
pub fn search_for_user<'a>(
|
||||
search_string: &str,
|
||||
pool: &'a Pool,
|
||||
) -> BoxStream<'a, sqlx::Result<UserInfo>> {
|
||||
sqlx::query_file_as!(UserInfo, "sql/search_for_user.sql", search_string).fetch(pool)
|
||||
) -> BoxStream<'a, sqlx::Result<UserSearch>> {
|
||||
sqlx::query_file_as!(UserSearch, "sql/search_for_user.sql", search_string).fetch(pool)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use futures::TryStreamExt;
|
||||
use futures::{future, TryStreamExt};
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
@ -10,9 +10,10 @@ pub struct Params {
|
||||
pub async fn search(
|
||||
State(pool): State<Pool>,
|
||||
Query(params): Query<Params>,
|
||||
) -> sqlx::Result<Json<Vec<db::users::UserInfo>>, StatusCode> {
|
||||
) -> sqlx::Result<Json<Vec<db::users::UserSearch>>, StatusCode> {
|
||||
let users = db::users::search_for_user(¶ms.search_string, &pool)
|
||||
.take(20)
|
||||
.try_filter(|user| future::ready(user.similarity > 0.1))
|
||||
.try_collect()
|
||||
.await
|
||||
.handle_internal()?;
|
||||
|
Reference in New Issue
Block a user