From 4d8706d6bf28af5b2565a5a42d4b2aae025feafc Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 14 Apr 2024 11:00:12 -0700 Subject: [PATCH] simplify search more and add more convenience views. --- migrations/20240409233522_views.up.sql | 3 ++- src/generic_handlers.rs | 1 + src/search.rs | 20 ++++++-------------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/migrations/20240409233522_views.up.sql b/migrations/20240409233522_views.up.sql index 729e1e7..327035b 100644 --- a/migrations/20240409233522_views.up.sql +++ b/migrations/20240409233522_views.up.sql @@ -1,4 +1,5 @@ create view if not exists w as select julid_string(id) id, kind, title, metadata_url, length, release_date, last_updated from watches; create view if not exists s as select julid_string(id) id, name, born, died from stars; create view if not exists u as select julid_string(id) id, username, displayname, email, last_seen, last_updated from users; -create view if not exists i as select julid_string(id) id, julid_string(owner) owner, expires_at, remaining, last_updated from invites; +create view if not exists i as select julid_string(invites.id) id, users.username, expires_at, remaining, invites.last_updated from invites inner join users on users.id = owner; +create view if not exists q as select users.username, watches.title from watch_quests inner join users on users.id = user inner join watches on watch = watches.id; diff --git a/src/generic_handlers.rs b/src/generic_handlers.rs index 0b05e92..da80ae3 100644 --- a/src/generic_handlers.rs +++ b/src/generic_handlers.rs @@ -6,6 +6,7 @@ pub async fn handle_slash_redir() -> impl IntoResponse { Redirect::to("/") } +#[axum::debug_handler] pub async fn handle_slash(auth: AuthSession) -> impl IntoResponse { if let Some(ref user) = auth.user { let name = &user.username; diff --git a/src/search.rs b/src/search.rs index f207a96..4bcfda6 100644 --- a/src/search.rs +++ b/src/search.rs @@ -29,8 +29,6 @@ pub struct SearchQuery { pub title: Option, #[serde(default, deserialize_with = "empty_string_as_none")] pub year: Option, - #[serde(default, deserialize_with = "empty_string_as_none")] - pub person: Option, } pub async fn get_search_watch( @@ -39,20 +37,14 @@ pub async fn get_search_watch( Query(search): Query, ) -> impl IntoResponse { let user = auth.user; - let SearchQuery { - title, - year, - person, - } = &search; + let SearchQuery { title, year } = &search; - let watches: Vec = match (title, year, person) { - (Some(title), None, None) => sqlx::query_as( + let watches: Vec = match (title, year) { + (Some(title), None) => 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(), - (Some(title), Some(year), None) => {todo!()}, - (None, Some(year), None) => {todo!()}, - (None, Some(year), Some(person)) => {todo!()}, - (Some(title), Some(year), Some(person)) => {todo!()} + title.trim()).fetch_all(&pool).await.unwrap_or_default(), + (Some(title), Some(year)) => sqlx::query_as("select * from watches where id in (select id from watch_search where title match ? order by rank) and release_date = ?").bind(title.trim()).bind(year.trim()).fetch_all(&pool).await.unwrap_or_default(), + (None, Some(year)) => sqlx::query_as("select * from watches where release_date = ? order by title").bind(year.trim()).fetch_all(&pool).await.unwrap_or_default(), _ => 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() };