From 44833bc898cac0612907613ed64ee4ff2a94a5c8 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Mon, 20 Jan 2025 00:13:45 +0100 Subject: complete database rework --- src/routes/album.rs | 66 ++++++++++++----------------------------------------- 1 file changed, 15 insertions(+), 51 deletions(-) (limited to 'src/routes/album.rs') diff --git a/src/routes/album.rs b/src/routes/album.rs index 02116d4..c06d4d2 100644 --- a/src/routes/album.rs +++ b/src/routes/album.rs @@ -1,4 +1,4 @@ -use crate::routes::Album; +use crate::database::Album; use crate::AppState; use actix_web::{get, web, HttpResponse}; use serde::Deserialize; @@ -15,62 +15,26 @@ pub async fn album( app_state: web::Data, get_args: web::Query, ) -> HttpResponse { - let search_attempt: sqlx::Result>; - - match true { + let search_attempt: sqlx::Result, sqlx::Error> = match true { _ if check_if_exists(&get_args.id) => { - search_attempt = sqlx::query_as!( - Album, - "SELECT album.name, album.id, - artist.name as artist_name, artist.id as artist_id - FROM album - INNER JOIN artist ON album.artist_id = artist.id - WHERE album.id=?", - &get_args.id, - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_album_by_id(&get_args.id.clone().unwrap()) + .await } _ if check_if_exists(&get_args.name) => { - let new_name : String = format!("{}{}{}", "%", &get_args.name.clone().unwrap(), "%"); - search_attempt = sqlx::query_as!( - Album, - "SELECT album.name, album.id, - artist.name as artist_name, artist.id as artist_id - FROM album - INNER JOIN artist ON album.artist_id = artist.id - WHERE LOWER(album.name) LIKE LOWER(?)", - new_name, - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_albums_by_name(&get_args.name.clone().unwrap()) + .await } _ if check_if_exists(&get_args.artist) => { - search_attempt = sqlx::query_as!( - Album, - "SELECT album.name, album.id, - artist.name as artist_name, artist.id as artist_id - FROM album - INNER JOIN artist ON album.artist_id = artist.id - WHERE album.artist_id=? - ", - &get_args.artist, - ) - .fetch_all(&app_state.pool) - .await; - } - _ => { - search_attempt = sqlx::query_as!( - Album, - "SELECT album.name, album.id, - artist.name as artist_name, artist.id as artist_id - FROM album - INNER JOIN artist ON album.artist_id = artist.id - ", - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_albums_by_artist(&get_args.artist.clone().unwrap()) + .await } + _ => app_state.database.select_albums().await, }; match search_attempt { -- cgit v1.2.3