From 6ca868c03ab4bc9ead99ef81c28560abf6fa0943 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Mon, 20 Jan 2025 13:28:45 +0100 Subject: cleaner access to database from api routes --- src/routes/album.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/routes/album.rs') diff --git a/src/routes/album.rs b/src/routes/album.rs index c06d4d2..d826f24 100644 --- a/src/routes/album.rs +++ b/src/routes/album.rs @@ -15,24 +15,19 @@ pub async fn album( app_state: web::Data, get_args: web::Query, ) -> HttpResponse { + let default: String = String::from(""); let search_attempt: sqlx::Result, sqlx::Error> = match true { _ if check_if_exists(&get_args.id) => { - app_state - .database - .select_album_by_id(&get_args.id.clone().unwrap()) - .await + let id: &str = &get_args.id.as_ref().unwrap_or(&default); + app_state.database.select_album_by_id(id).await } _ if check_if_exists(&get_args.name) => { - app_state - .database - .select_albums_by_name(&get_args.name.clone().unwrap()) - .await + let name: &str = &get_args.name.as_ref().unwrap_or(&default); + app_state.database.select_albums_by_name(name).await } _ if check_if_exists(&get_args.artist) => { - app_state - .database - .select_albums_by_artist(&get_args.artist.clone().unwrap()) - .await + let artist: &str = &get_args.artist.as_ref().unwrap_or(&default); + app_state.database.select_albums_by_artist(artist).await } _ => app_state.database.select_albums().await, }; -- cgit v1.2.3