diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-20 01:32:52 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-20 01:32:52 +0100 |
commit | cb3a9e58dbfa65da655c68e21d5bf270c4ccb7ef (patch) | |
tree | 68c8d797f26dbec33e335ea1b0ae96e1b36e26a1 | |
parent | f739988888aacd04d9e821fd2d2eea467d01c6ae (diff) |
add searchResults
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/database.rs | 67 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/routes.rs | 1 | ||||
-rw-r--r-- | src/routes/search_results.rs | 55 |
5 files changed, 111 insertions, 17 deletions
@@ -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<Vec<Song>, sqlx::Error> { + pub async fn select_songs_by_artist(&self, artist_id: &str) -> Result<Vec<Song>, 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<Vec<Album>, 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<Vec<Album>, sqlx::Error> { + pub async fn select_albums_by_artist( + &self, + artist_id: &str, + ) -> Result<Vec<Album>, 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<Vec<Artist>, 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<Vec<Artist>, sqlx::Error>, + Result<Vec<Album>, sqlx::Error>, + Result<Vec<Song>, 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<Vec<Artist>, sqlx::Error>, + Result<Vec<Album>, sqlx::Error>, + Result<Vec<Song>, 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<Vec<Artist>, sqlx::Error>, + Result<Vec<Album>, sqlx::Error>, + Result<Vec<Song>, 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<String>, + name: Option<String>, +} + +#[get("/searchResults")] +pub async fn search_results( + app_state: web::Data<AppState>, + get_args: web::Query<SearchQueryOptions>, +) -> HttpResponse { + let search_attempt: ( + sqlx::Result<Vec<Artist>, sqlx::Error>, + sqlx::Result<Vec<Album>, sqlx::Error>, + sqlx::Result<Vec<Song>, 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<String>) -> bool { + match value { + Some(_) => true, + None => false, + } +} |