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 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_QUEST_QUERY: &str =
"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 = "✓";
//-************************************************************************
@ -249,33 +245,26 @@ pub async fn get_search_watch(
search: Query<SearchQuery>,
) -> impl IntoResponse {
let user = auth.user;
let search_query = search.0;
let (search_string, qstring) = if search.0 != EMPTY_SEARCH_QUERY_STRUCT {
let s = search.0;
let q = if let Some(title) = &s.title {
title
} else if let Some(search) = &s.search {
search
let query = if search_query == SearchQuery::default() {
query_as(DEFAULT_WATCHES_QUERY)
} else {
""
};
(format!("{s:?}"), format!("%{}%", q.trim()))
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 {
("".to_string(), "%".to_string())
query_as(DEFAULT_WATCHES_QUERY)
}
};
// until tantivy search
let watches: Vec<Watch> = query_as("select * from watches where title like ?")
.bind(&qstring)
.fetch_all(&pool)
.await
.unwrap();
let watches: Vec<Watch> = query.fetch_all(&pool).await.unwrap();
SearchWatchesPage {
watches,
user,
search: search_string,
}
SearchWatchesPage { watches, user }
}
pub async fn get_watch_status(

View file

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