summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2025-01-20 13:28:45 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2025-01-20 13:28:45 +0100
commit6ca868c03ab4bc9ead99ef81c28560abf6fa0943 (patch)
treee1e0edd8d97cd0c216ae0dceab3f5e26134301dc
parent371a647536c735118a66522fc46f86a0ec09e2d7 (diff)
cleaner access to database from api routes
-rw-r--r--src/routes/album.rs19
-rw-r--r--src/routes/artist.rs14
-rw-r--r--src/routes/song.rs25
3 files changed, 22 insertions, 36 deletions
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<AppState>,
get_args: web::Query<AlbumQueryOptions>,
) -> HttpResponse {
+ let default: String = String::from("");
let search_attempt: sqlx::Result<Vec<Album>, 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,
};
diff --git a/src/routes/artist.rs b/src/routes/artist.rs
index 8d968ae..5f0b9a6 100644
--- a/src/routes/artist.rs
+++ b/src/routes/artist.rs
@@ -14,18 +14,16 @@ pub async fn artist(
app_state: web::Data<AppState>,
get_args: web::Query<ArtistQueryOptions>,
) -> HttpResponse {
+ let default: String = String::from("");
let search_attempt: sqlx::Result<Vec<Artist>, sqlx::Error> = match true {
_ if check_if_exists(&get_args.id) => {
- app_state
- .database
- .select_artist_by_id(&get_args.id.clone().unwrap())
- .await
+ let id: &str = &get_args.id.as_ref().unwrap_or(&default);
+
+ app_state.database.select_artist_by_id(id).await
}
_ if check_if_exists(&get_args.name) => {
- app_state
- .database
- .select_artists_by_name(&get_args.name.clone().unwrap())
- .await
+ let name: &str = &get_args.name.as_ref().unwrap_or(&default);
+ app_state.database.select_artists_by_name(name).await
}
_ => app_state.database.select_artists().await,
};
diff --git a/src/routes/song.rs b/src/routes/song.rs
index 4cc20e9..8cb34bf 100644
--- a/src/routes/song.rs
+++ b/src/routes/song.rs
@@ -16,30 +16,23 @@ pub async fn song(
app_state: web::Data<AppState>,
get_args: web::Query<SongQueryOptions>,
) -> HttpResponse {
+ let default: String = String::from("");
let search_attempt: sqlx::Result<Vec<Song>> = match true {
_ if check_if_exists(&get_args.id) => {
- app_state
- .database
- .select_song_by_id(&get_args.id.clone().unwrap())
- .await
+ let id: &str = &get_args.id.as_ref().unwrap_or(&default);
+ app_state.database.select_song_by_id(id).await
}
_ if check_if_exists(&get_args.name) => {
- app_state
- .database
- .select_songs_by_name(&get_args.name.clone().unwrap())
- .await
+ let name: &str = &get_args.name.as_ref().unwrap_or(&default);
+ app_state.database.select_songs_by_name(name).await
}
_ if check_if_exists(&get_args.album) => {
- app_state
- .database
- .select_songs_by_album(&get_args.album.clone().unwrap())
- .await
+ let album: &str = &get_args.album.as_ref().unwrap_or(&default);
+ app_state.database.select_songs_by_album(album).await
}
_ if check_if_exists(&get_args.artist) => {
- app_state
- .database
- .select_songs_by_artist(&get_args.artist.clone().unwrap())
- .await
+ let artist: &str = &get_args.artist.as_ref().unwrap_or(&default);
+ app_state.database.select_songs_by_artist(artist).await
}
_ => app_state.database.select_songs().await,
};