diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-20 00:13:45 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-20 00:13:45 +0100 |
commit | 44833bc898cac0612907613ed64ee4ff2a94a5c8 (patch) | |
tree | 5bb8f355ebeabeead47495f05d00a186d2e06960 | |
parent | ed0f5925de1931a13e6713d73e174a5362fc5d68 (diff) |
complete database rework
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/database.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 14 | ||||
-rw-r--r-- | src/routes/album.rs | 66 | ||||
-rw-r--r-- | src/routes/artist.rs | 44 | ||||
-rw-r--r-- | src/routes/song.rs | 92 |
6 files changed, 68 insertions, 171 deletions
@@ -10,6 +10,7 @@ serde = { version = "1.0.210", features = ["derive"] } tokio = "1.40.0" dotenv = "0.15.0" serde_json = "1.0.128" +env_logger = "0.11.6" [package.metadata.scripts] db_create = "bash ./scripts/create_db.sh" diff --git a/src/database.rs b/src/database.rs index 299bd8d..a9daa7f 100644 --- a/src/database.rs +++ b/src/database.rs @@ -61,7 +61,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_song_by_id(&self, id: String) -> Result<Vec<Song>, sqlx::Error> { + pub async fn select_song_by_id(&self, id: &str) -> Result<Vec<Song>, sqlx::Error> { return sqlx::query_as!( Song, "SELECT song.name, song.lyrics, song.id, @@ -78,7 +78,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_songs_by_album(&self, album_id: String) -> Result<Vec<Song>, sqlx::Error> { + pub async fn select_songs_by_album(&self, album_id: &str) -> Result<Vec<Song>, sqlx::Error> { return sqlx::query_as!( Song, "SELECT song.name, song.lyrics, song.id, @@ -97,7 +97,7 @@ impl DatabaseWrapper { pub async fn select_songs_by_artist( &self, - artist_id: String, + artist_id: &str, ) -> Result<Vec<Song>, sqlx::Error> { return sqlx::query_as!( Song, @@ -115,8 +115,9 @@ impl DatabaseWrapper { .await; } - pub async fn select_songs_by_name(&self, name_raw: String) -> Result<Vec<Song>, sqlx::Error> { + pub async fn select_songs_by_name(&self, name_raw: &str) -> Result<Vec<Song>, sqlx::Error> { let name: String = format!("{}{}{}", "%", name_raw, "%"); + println!("ERROR HUNTING: {}", name); return sqlx::query_as!( Song, "SELECT song.name, song.lyrics, song.id, @@ -145,7 +146,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_album_by_id(&self, id: String) -> Result<Vec<Album>, sqlx::Error> { + pub async fn select_album_by_id(&self, id: &str) -> Result<Vec<Album>, sqlx::Error> { return sqlx::query_as!( Album, "SELECT album.name, album.id, @@ -159,7 +160,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_albums_by_name(&self, name_raw: String) -> Result<Vec<Album>, sqlx::Error> { + pub async fn select_albums_by_name(&self, name_raw: &str) -> Result<Vec<Album>, sqlx::Error> { let name : String = format!("{}{}{}", "%", name_raw, "%"); return sqlx::query_as!( Album, @@ -175,7 +176,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_albums_by_artist_id(&self, artist_id: String) -> 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, @@ -200,7 +201,7 @@ impl DatabaseWrapper { .await; } - pub async fn select_artist_by_id(&self, id: String) -> Result<Vec<Artist>, sqlx::Error> { + pub async fn select_artist_by_id(&self, id: &str) -> Result<Vec<Artist>, sqlx::Error> { return sqlx::query_as!( Artist, "SELECT name, id @@ -213,13 +214,14 @@ impl DatabaseWrapper { .await; } - pub async fn select_artists_by_name(&self, name_raw: String) -> Result<Vec<Artist>, sqlx::Error> { + pub async fn select_artists_by_name(&self, name_raw: &str) -> Result<Vec<Artist>, sqlx::Error> { let name : String = format!("{}{}{}", "%", name_raw, "%"); + println!("ERROR HUNTING: {}", name); return sqlx::query_as!( Artist, "SELECT name, id FROM artist - WHERE name = ?" + WHERE name LIKE ?" , name, ) diff --git a/src/main.rs b/src/main.rs index f1a58ec..5226692 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,20 +6,26 @@ use actix_web::{web, App, HttpServer}; use dotenv::dotenv; use std::sync::Arc; +#[derive(Clone)] struct AppState { - database: database::DatabaseWrapper + database: Arc<database::DatabaseWrapper> } #[actix_web::main] async fn main() -> std::io::Result<()> { + // Errors can get very tough, + // the rust log saved my ass + std::env::set_var("RUST_LOG", "debug"); + env_logger::init(); dotenv().ok(); - let app_state_raw = database::DatabaseWrapper::new().await.expect("Something went wrong -- DatabaseWrapper::new()"); - let app_state = Arc::new(app_state_raw); + let db_raw = database::DatabaseWrapper::new().await.expect("Something went wrong -- DatabaseWrapper::new()"); + let db = Arc::new(db_raw); + let app_state = AppState{database: db}; HttpServer::new(move || { App::new() - .app_data(web::Data::new(Arc::clone(&app_state))) + .app_data(web::Data::new(app_state.clone())) .route("/", web::get().to(root)) .service(routes::song::song) .service(routes::album::album) 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<AppState>, get_args: web::Query<AlbumQueryOptions>, ) -> HttpResponse { - let search_attempt: sqlx::Result<Vec<Album>>; - - match true { + let search_attempt: sqlx::Result<Vec<Album>, 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 { diff --git a/src/routes/artist.rs b/src/routes/artist.rs index 6a16ace..8d968ae 100644 --- a/src/routes/artist.rs +++ b/src/routes/artist.rs @@ -1,4 +1,4 @@ -use crate::routes::Artist; +use crate::database::Artist; use crate::AppState; use actix_web::{get, web, HttpResponse}; use serde::Deserialize; @@ -14,42 +14,20 @@ pub async fn artist( app_state: web::Data<AppState>, get_args: web::Query<ArtistQueryOptions>, ) -> HttpResponse { - let search_attempt: sqlx::Result<Vec<Artist>>; - - match true { + let search_attempt: sqlx::Result<Vec<Artist>, sqlx::Error> = match true { _ if check_if_exists(&get_args.id) => { - search_attempt = sqlx::query_as!( - Artist, - "SELECT name, id - FROM artist - WHERE id=?", - &get_args.id, - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_artist_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!( - Artist, - "SELECT name, id - FROM artist - WHERE LOWER(name) LIKE LOWER(?)", - new_name, - ) - .fetch_all(&app_state.pool) - .await; - } - _ => { - search_attempt = sqlx::query_as!( - Artist, - "SELECT name, id - FROM artist - ", - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_artists_by_name(&get_args.name.clone().unwrap()) + .await } + _ => app_state.database.select_artists().await, }; match search_attempt { diff --git a/src/routes/song.rs b/src/routes/song.rs index 0cdd4b5..4cc20e9 100644 --- a/src/routes/song.rs +++ b/src/routes/song.rs @@ -1,4 +1,4 @@ -use crate::routes::Song; +use crate::database::Song; use crate::AppState; use actix_web::{get, web, HttpResponse}; use serde::Deserialize; @@ -16,86 +16,32 @@ pub async fn song( app_state: web::Data<AppState>, get_args: web::Query<SongQueryOptions>, ) -> HttpResponse { - let search_attempt: sqlx::Result<Vec<Song>>; - - match true { + let search_attempt: sqlx::Result<Vec<Song>> = match true { _ if check_if_exists(&get_args.id) => { - search_attempt = sqlx::query_as!( - Song, - "SELECT song.name, song.lyrics, song.id, - album.name as album_name, album.id as album_id, - artist.name as artist_name, artist.id as artist_id - FROM song - INNER JOIN album ON song.album_id = album.id - INNER JOIN artist ON album.artist_id = artist.id - WHERE song.id=?", - &get_args.id, - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_song_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!( - Song, - "SELECT song.name, song.lyrics, song.id, - album.name as album_name, album.id as album_id, - artist.name as artist_name, artist.id as artist_id - FROM song - INNER JOIN album ON song.album_id = album.id - INNER JOIN artist ON album.artist_id = artist.id - WHERE LOWER(song.name) LIKE LOWER(?)", - new_name, - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_songs_by_name(&get_args.name.clone().unwrap()) + .await } _ if check_if_exists(&get_args.album) => { - search_attempt = sqlx::query_as!( - Song, - "SELECT song.name, song.lyrics, song.id, - album.name as album_name, album.id as album_id, - artist.name as artist_name, artist.id as artist_id - FROM song - INNER JOIN album ON song.album_id = album.id - INNER JOIN artist ON album.artist_id = artist.id - WHERE song.album_id=? - ", - &get_args.album, - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_songs_by_album(&get_args.album.clone().unwrap()) + .await } _ if check_if_exists(&get_args.artist) => { - search_attempt = sqlx::query_as!( - Song, - "SELECT song.name, song.lyrics, song.id, - album.name as album_name, album.id as album_id, - artist.name as artist_name, artist.id as artist_id - FROM song - INNER JOIN album ON song.album_id = album.id - 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!( - Song, - "SELECT song.name, song.lyrics, song.id, - album.name as album_name, album.id as album_id, - artist.name as artist_name, artist.id as artist_id - FROM song - INNER JOIN album ON song.album_id = album.id - INNER JOIN artist ON album.artist_id = artist.id - ", - ) - .fetch_all(&app_state.pool) - .await; + app_state + .database + .select_songs_by_artist(&get_args.artist.clone().unwrap()) + .await } + _ => app_state.database.select_songs().await, }; match search_attempt { |