split title and preson search

This commit is contained in:
Joe Ardent 2024-04-14 21:22:16 -07:00
parent 5ab8fa8444
commit 062a3e26bc
4 changed files with 48 additions and 14 deletions

View file

@ -80,7 +80,7 @@ pub async fn app(db_pool: sqlx::SqlitePool) -> IntoMakeService<axum::Router> {
.route("/watch", get(get_watch)) .route("/watch", get(get_watch))
.route("/watch/:watch", get(get_watch)) .route("/watch/:watch", get(get_watch))
.route("/watch/status/:watch", get(get_watch_status)) .route("/watch/status/:watch", get(get_watch_status))
.route("/search", get(get_search_watch)) .route("/title-search", get(get_search_watch))
.route("/add", get(get_add_new_watch).post(post_add_new_watch)) .route("/add", get(get_add_new_watch).post(post_add_new_watch))
.route( .route(
"/add/watch", "/add/watch",

View file

@ -24,20 +24,28 @@ pub enum SearchResult {
} }
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)] #[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
pub struct SearchQuery { pub struct SearchWatchQuery {
#[serde(default, deserialize_with = "empty_string_as_none")] #[serde(default, deserialize_with = "empty_string_as_none")]
pub title: Option<String>, pub title: Option<String>,
#[serde(default, deserialize_with = "empty_string_as_none")] #[serde(default, deserialize_with = "empty_string_as_none")]
pub year: Option<String>, pub year: Option<String>,
} }
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
pub struct SearchStarQuery {
#[serde(default, deserialize_with = "empty_string_as_none")]
pub name: Option<String>,
#[serde(default, deserialize_with = "empty_string_as_none")]
pub year: Option<String>,
}
pub async fn get_search_watch( pub async fn get_search_watch(
auth: AuthSession, auth: AuthSession,
State(pool): State<SqlitePool>, State(pool): State<SqlitePool>,
Query(search): Query<SearchQuery>, Query(search): Query<SearchWatchQuery>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let user = auth.user; let user = auth.user;
let SearchQuery { title, year } = &search; let SearchWatchQuery { title, year } = &search;
let watches: Vec<Watch> = match (title, year) { let watches: Vec<Watch> = match (title, year) {
(Some(title), None) => sqlx::query_as( (Some(title), None) => sqlx::query_as(
@ -53,3 +61,26 @@ pub async fn get_search_watch(
user, user,
} }
} }
pub async fn get_search_star(
auth: AuthSession,
State(pool): State<SqlitePool>,
Query(search): Query<SearchStarQuery>,
) -> impl IntoResponse {
let user = auth.user;
let SearchStarQuery { name, year } = &search;
let watches: Vec<Watch> = match (name, 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.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()
};
SearchPage {
results: watches,
user,
}
}

View file

@ -17,7 +17,7 @@
<p> <p>
<form action="/search" enctype="application/x-www-form-urlencoded" method="get"> <form action="/title-search" enctype="application/x-www-form-urlencoded" method="get">
<label for="title">Looking for something else to watch?</label> <label for="title">Looking for something else to watch?</label>
<input type="text" name="title" id="title"></br> <input type="text" name="title" id="title"></br>
<input type="submit" value="Let's go!"> <input type="submit" value="Let's go!">

View file

@ -8,22 +8,25 @@
<h1>Whatcha Watchin?</h1> <h1>Whatcha Watchin?</h1>
<div class="fullsearch"> <div class="fullsearch">
<form action="/search" enctype="application/x-www-form-urlencoded" method="get"> <form action="/title-search" enctype="application/x-www-form-urlencoded" method="get">
<h2>Search</h2> <h2>Title Search</h2>
<label for="title">Title</label> <label for="title">Title</label>
<input type="text" name="title" id="title-search"></br> <input type="text" name="title" id="title-search"></br>
<label for="year">Release Year</label> <label for="year">Release Year</label>
<input type="text" name="year" id="title-year-search"></br> <input type="text" name="year" id="title-year-search">
<hr>
<label for="person">Person</label>
<input type="text" name="person" id="person-search"></br>
<input type="submit" value="Let's go!"> <input type="submit" value="Let's go!">
</form> </form>
<hr>
<form action="/person-search" enctype="application/x-www-form-urlencoded" method="get">
<label for="person">Person</label>
<input type="text" name="person" id="person-search">
<input type="submit" value="Let's go!">
</form>
</div> </div>
<hr /> <hr />