make searches better

This commit is contained in:
Joe Ardent 2024-01-04 22:23:19 -08:00
parent 1e0a71f275
commit d18de3e084
2 changed files with 17 additions and 29 deletions

View file

@ -23,17 +23,13 @@ const GET_QUEST_QUERY: &str = "select * from watch_quests where user = ? and wat
const GET_WATCH_QUERY: &str = "select * from watches where id = $1"; const GET_WATCH_QUERY: &str = "select * from watches where id = $1";
const DEFAULT_WATCHES_QUERY: &str =
"select * from (select * from watches order by random() limit 50) order by release_date asc";
const ADD_WATCH_QUERY: &str = "insert into watches (title, kind, release_date, metadata_url, added_by, length) values ($1, $2, $3, $4, $5, $6) returning id"; const ADD_WATCH_QUERY: &str = "insert into watches (title, kind, release_date, metadata_url, added_by, length) values ($1, $2, $3, $4, $5, $6) returning id";
const ADD_WATCH_QUEST_QUERY: &str = const ADD_WATCH_QUEST_QUERY: &str =
"insert into watch_quests (user, watch, public, watched) values ($1, $2, $3, $4)"; "insert into watch_quests (user, watch, public, watched) values ($1, $2, $3, $4)";
const EMPTY_SEARCH_QUERY_STRUCT: SearchQuery = SearchQuery {
title: None,
kind: None,
year: None,
search: None,
};
const CHECKMARK: &str = "✓"; const CHECKMARK: &str = "✓";
//-************************************************************************ //-************************************************************************
@ -249,33 +245,26 @@ pub async fn get_search_watch(
search: Query<SearchQuery>, search: Query<SearchQuery>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let user = auth.user; let user = auth.user;
let search_query = search.0;
let (search_string, qstring) = if search.0 != EMPTY_SEARCH_QUERY_STRUCT { let query = if search_query == SearchQuery::default() {
let s = search.0; query_as(DEFAULT_WATCHES_QUERY)
let q = if let Some(title) = &s.title {
title
} else if let Some(search) = &s.search {
search
} else {
""
};
(format!("{s:?}"), format!("%{}%", q.trim()))
} else { } else {
("".to_string(), "%".to_string()) if let Some(title) = search_query.title {
let q = format!("%{title}%");
query_as("select * from watches where title like ?").bind(q)
} else if let Some(search) = search_query.search {
let q = format!("%{search}");
query_as("select * from watches where title like ?").bind(q)
} else {
query_as(DEFAULT_WATCHES_QUERY)
}
}; };
// until tantivy search // until tantivy search
let watches: Vec<Watch> = query_as("select * from watches where title like ?") let watches: Vec<Watch> = query.fetch_all(&pool).await.unwrap();
.bind(&qstring)
.fetch_all(&pool)
.await
.unwrap();
SearchWatchesPage { SearchWatchesPage { watches, user }
watches,
user,
search: search_string,
}
} }
pub async fn get_watch_status( pub async fn get_watch_status(

View file

@ -16,7 +16,6 @@ pub struct MyWatchesPage {
pub struct SearchWatchesPage { pub struct SearchWatchesPage {
pub watches: Vec<Watch>, pub watches: Vec<Watch>,
pub user: Option<User>, pub user: Option<User>,
pub search: String,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]