simplify search

This commit is contained in:
Joe Ardent 2024-04-11 18:17:31 -07:00
parent ba1f9119e8
commit 4e2e8e585f

View file

@ -4,7 +4,7 @@ use axum::{
response::IntoResponse,
};
use serde::{Deserialize, Serialize};
use sqlx::{query_as, SqlitePool};
use sqlx::SqlitePool;
use crate::{
misc_util::empty_string_as_none, AuthSession, OptionalOptionalUser, Star, User, Watch,
@ -40,29 +40,17 @@ pub async fn get_search_watch(
State(pool): State<SqlitePool>,
search: Query<SearchQuery>,
) -> impl IntoResponse {
const DEFAULT_WATCHES_QUERY: &str =
"select * from (select * from watches order by random() limit 50) order by release_date asc";
let user = auth.user;
let search_query = search.0;
let query = if search_query == SearchQuery::default() {
query_as(DEFAULT_WATCHES_QUERY)
} else if let Some(title) = search_query.title {
query_as(
"select * from watches where id in (select id from watch_search where title match ? order by rank)",
)
.bind(title)
} else if let Some(ref search) = search_query.search {
query_as("select * from watches where id in (select id from watch_search where title match ?) outer join (select * from stars where id in (select id from star_search where name match ?)) s")
.bind(search).bind(search)
let watches: Vec<Watch> = if let Some(title) = search_query.title {
sqlx::query_as(
"select * from watches where id in (select id from watch_search where title match ? order by rank)").bind(
&title).fetch_all(&pool).await.unwrap_or_default()
} else {
query_as(DEFAULT_WATCHES_QUERY)
sqlx::query_as("select * from (select * from watches order by random() limit 50) order by release_date asc").fetch_all(&pool).await.unwrap_or_default()
};
// until tantivy search
let watches: Vec<Watch> = query.fetch_all(&pool).await.unwrap();
SearchPage {
results: watches,
user,