From cb3a9e58dbfa65da655c68e21d5bf270c4ccb7ef Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Mon, 20 Jan 2025 01:32:52 +0100 Subject: add searchResults --- README.md | 4 --- src/database.rs | 67 +++++++++++++++++++++++++++++++++++--------- src/main.rs | 1 + src/routes.rs | 1 + src/routes/search_results.rs | 55 ++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 src/routes/search_results.rs diff --git a/README.md b/README.md index 5cc8773..879e026 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,3 @@ If not, just start/stop the mariadb service manually. You can only search a parameter at a time. This is because Rust needs to process the sql queries ahead of time in order to make them safe to injection attacks. - -### SearchResults route ignored -It's a little hard to get SearchResults to work, -so it has been skipped for now. diff --git a/src/database.rs b/src/database.rs index a9daa7f..32b1de7 100644 --- a/src/database.rs +++ b/src/database.rs @@ -95,10 +95,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_songs_by_artist( - &self, - artist_id: &str, - ) -> Result, sqlx::Error> { + pub async fn select_songs_by_artist(&self, artist_id: &str) -> Result, sqlx::Error> { return sqlx::query_as!( Song, "SELECT song.name, song.lyrics, song.id, @@ -161,7 +158,7 @@ impl DatabaseWrapper { } pub async fn select_albums_by_name(&self, name_raw: &str) -> Result, sqlx::Error> { - let name : String = format!("{}{}{}", "%", name_raw, "%"); + let name: String = format!("{}{}{}", "%", name_raw, "%"); return sqlx::query_as!( Album, "SELECT album.name, album.id, @@ -176,7 +173,10 @@ impl DatabaseWrapper { .await; } - pub async fn select_albums_by_artist(&self, artist_id: &str) -> Result, sqlx::Error> { + pub async fn select_albums_by_artist( + &self, + artist_id: &str, + ) -> Result, sqlx::Error> { return sqlx::query_as!( Album, "SELECT album.name, album.id, @@ -194,8 +194,7 @@ impl DatabaseWrapper { return sqlx::query_as!( Artist, "SELECT name, id - FROM artist" - , + FROM artist", ) .fetch_all(&self.db_pool) .await; @@ -206,8 +205,7 @@ impl DatabaseWrapper { Artist, "SELECT name, id FROM artist - WHERE id = ?" - , + WHERE id = ?", id, ) .fetch_all(&self.db_pool) @@ -215,17 +213,60 @@ impl DatabaseWrapper { } pub async fn select_artists_by_name(&self, name_raw: &str) -> Result, sqlx::Error> { - let name : String = format!("{}{}{}", "%", name_raw, "%"); + let name: String = format!("{}{}{}", "%", name_raw, "%"); println!("ERROR HUNTING: {}", name); return sqlx::query_as!( Artist, "SELECT name, id FROM artist - WHERE name LIKE ?" - , + WHERE name LIKE ?", name, ) .fetch_all(&self.db_pool) .await; } + + pub async fn search_results( + &self, + ) -> ( + Result, sqlx::Error>, + Result, sqlx::Error>, + Result, sqlx::Error>, + ) { + return ( + self.select_artists().await, + self.select_albums().await, + self.select_songs().await, + ); + } + + pub async fn search_results_by_id( + &self, + id: &str, + ) -> ( + Result, sqlx::Error>, + Result, sqlx::Error>, + Result, sqlx::Error>, + ) { + return ( + self.select_artist_by_id(id).await, + self.select_album_by_id(id).await, + self.select_song_by_id(id).await, + ); + } + + pub async fn search_results_by_name( + &self, + name: &str, + ) -> ( + Result, sqlx::Error>, + Result, sqlx::Error>, + Result, sqlx::Error>, + ) { + return ( + self.select_artists_by_name(name).await, + self.select_albums_by_name(name).await, + self.select_songs_by_name(name).await, + ); + } } diff --git a/src/main.rs b/src/main.rs index 5226692..8dbc7d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ async fn main() -> std::io::Result<()> { .service(routes::song::song) .service(routes::album::album) .service(routes::artist::artist) + .service(routes::search_results::search_results) }) .bind(("127.0.0.1", 8000))? .run() diff --git a/src/routes.rs b/src/routes.rs index 869c123..0cefd1b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,3 +1,4 @@ pub mod song; pub mod album; pub mod artist; +pub mod search_results; diff --git a/src/routes/search_results.rs b/src/routes/search_results.rs new file mode 100644 index 0000000..03bd079 --- /dev/null +++ b/src/routes/search_results.rs @@ -0,0 +1,55 @@ +use crate::database::{Album, Artist, Song}; +use crate::AppState; +use actix_web::{get, web, HttpResponse}; +use serde::Deserialize; + +#[derive(Deserialize)] +struct SearchQueryOptions { + id: Option, + name: Option, +} + +#[get("/searchResults")] +pub async fn search_results( + app_state: web::Data, + get_args: web::Query, +) -> HttpResponse { + let search_attempt: ( + sqlx::Result, sqlx::Error>, + sqlx::Result, sqlx::Error>, + sqlx::Result, sqlx::Error>, + ) = match true { + _ if check_if_exists(&get_args.id) => { + app_state + .database + .search_results_by_id(&get_args.id.clone().unwrap()) + .await + } + _ if check_if_exists(&get_args.name) => { + app_state + .database + .search_results_by_name(&get_args.name.clone().unwrap()) + .await + } + _ => app_state + .database + .search_results() + .await + // Err(sqlx::Error::RowNotFound), + // Err(sqlx::Error::RowNotFound), + // Err(sqlx::Error::RowNotFound), + }; + + return HttpResponse::Ok().json(( + search_attempt.0.unwrap_or_else(|_| return Vec::new()), + search_attempt.1.unwrap_or_else(|_| return Vec::new()), + search_attempt.2.unwrap_or_else(|_| return Vec::new()) + )) +} + +fn check_if_exists(value: &Option) -> bool { + match value { + Some(_) => true, + None => false, + } +} -- cgit v1.2.3