diff options
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/album.rs | 60 | ||||
-rw-r--r-- | src/api/artist.rs | 59 | ||||
-rw-r--r-- | src/api/mod.rs | 43 | ||||
-rw-r--r-- | src/api/search_results.rs | 2 | ||||
-rw-r--r-- | src/api/song.rs | 52 |
5 files changed, 141 insertions, 75 deletions
diff --git a/src/api/album.rs b/src/api/album.rs index d9d0d52..3f91cd0 100644 --- a/src/api/album.rs +++ b/src/api/album.rs @@ -1,8 +1,10 @@ +use crate::api::{get_response_from_query, Response}; use crate::database::{Album, AlbumPost, AlbumPut, Delete}; use crate::AppState; use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::Deserialize; +/* Possible arguments ( /album?arg=value ) */ #[derive(Deserialize)] struct AlbumQueryOptions { id: Option<String>, @@ -15,19 +17,23 @@ pub async fn get_album( app_state: web::Data<AppState>, get_args: web::Query<AlbumQueryOptions>, ) -> HttpResponse { - let default = String::from(""); + /* Avoid lifespan issues */ + let default = String::default(); + /* Handle individual result for search-by-id */ if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); - let search_attempt: sqlx::Result<Option<Album>> = + let query_result: sqlx::Result<Option<Album>> = app_state.database.select_album_by_id(id).await; - return match search_attempt { + return match query_result { Ok(song_list) => HttpResponse::Ok().json(song_list), Err(e) => HttpResponse::Ok().body(format!("{}", e)), }; } - let search_attempt: sqlx::Result<Vec<Album>, sqlx::Error> = match true { + + /* Handle N results */ + let query_result: sqlx::Result<Vec<Album>, sqlx::Error> = match true { _ if get_args.name.is_some() => { let name: &str = &get_args.name.as_ref().unwrap_or(&default); app_state.database.select_albums_by_name(name).await @@ -39,7 +45,7 @@ pub async fn get_album( _ => app_state.database.select_albums().await, }; - match search_attempt { + match query_result { Ok(album_list) => HttpResponse::Ok().json(album_list), Err(e) => HttpResponse::Ok().body(format!("{}", e)), } @@ -48,35 +54,35 @@ pub async fn get_album( #[post("/album")] pub async fn post_album( app_state: web::Data<AppState>, - post_data: web::Json<AlbumPost>, + request_data: web::Json<AlbumPost>, ) -> HttpResponse { - match app_state - .database - .create_album(post_data.into_inner()) - .await - { - Ok(_) => HttpResponse::Ok().body("Post succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("{}", e)), - } + get_response_from_query( + app_state + .database + .create_album(request_data.into_inner()) + .await, + "POST".to_string(), + ) } #[put("/album")] pub async fn put_album( app_state: web::Data<AppState>, - post_data: web::Json<AlbumPut>, + request_data: web::Json<AlbumPut>, ) -> HttpResponse { - match app_state.database.edit_album(post_data.into_inner()).await { - Ok(_) => HttpResponse::Ok().body("Put succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("{}", e)), - } + get_response_from_query( + app_state.database.edit_album(request_data.into_inner()).await, + "PUT".to_string(), + ) } #[delete("/album")] pub async fn delete_album( app_state: web::Data<AppState>, - post_data: web::Json<Delete>, + request_data: web::Json<Delete>, ) -> HttpResponse { - let id: i32 = post_data + /* Check if ID is valid (return -1 if invalid) */ + let id: i32 = request_data .into_inner() .id .unwrap_or(String::default()) @@ -84,11 +90,13 @@ pub async fn delete_album( .unwrap_or(-1); if id == -1 { - return HttpResponse::Ok().body("Invalid id value, code not executed\n"); + return HttpResponse::BadRequest().json(Response { + message: "Invalid id value, code not executed\n".to_owned(), + }); } - match app_state.database.delete_album(id).await { - Ok(_) => HttpResponse::Ok().body("Deletion succeeded\n".to_owned()), - Err(e) => HttpResponse::Ok().body(format!("There was an issue in the request:\n{}", e)), - } + get_response_from_query( + app_state.database.delete_album(id).await, + "POST".to_string(), + ) } diff --git a/src/api/artist.rs b/src/api/artist.rs index 81ae773..155f982 100644 --- a/src/api/artist.rs +++ b/src/api/artist.rs @@ -1,8 +1,10 @@ +use crate::api::{get_response_from_query, Response}; use crate::database::{Artist, ArtistPost, ArtistPut, Delete}; use crate::AppState; use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::Deserialize; +/* Possible arguments ( /artist?arg=value ) */ #[derive(Deserialize)] struct ArtistQueryOptions { id: Option<String>, @@ -14,20 +16,23 @@ pub async fn get_artist( app_state: web::Data<AppState>, get_args: web::Query<ArtistQueryOptions>, ) -> HttpResponse { - let default = String::from(""); + /* Avoid lifespan issues */ + let default = String::default(); + /* Handle individual result for search-by-id */ if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); - let search_attempt: sqlx::Result<Option<Artist>> = + let query_result: sqlx::Result<Option<Artist>> = app_state.database.select_artist_by_id(id).await; - return match search_attempt { + return match query_result { Ok(song_list) => HttpResponse::Ok().json(song_list), Err(e) => HttpResponse::Ok().body(format!("{}", e)), }; } - let search_attempt: sqlx::Result<Vec<Artist>, sqlx::Error> = match true { + /* Handle N results */ + let query_result: sqlx::Result<Vec<Artist>, sqlx::Error> = match true { _ if get_args.name.is_some() => { let name: &str = &get_args.name.as_ref().unwrap_or(&default); app_state.database.select_artists_by_name(name).await @@ -35,7 +40,7 @@ pub async fn get_artist( _ => app_state.database.select_artists().await, }; - match search_attempt { + match query_result { Ok(artist_list) => HttpResponse::Ok().json(artist_list), Err(e) => HttpResponse::Ok().body(format!("{}", e)), } @@ -44,35 +49,35 @@ pub async fn get_artist( #[post("/artist")] pub async fn post_artist( app_state: web::Data<AppState>, - post_data: web::Json<ArtistPost>, + request_data: web::Json<ArtistPost>, ) -> HttpResponse { - match app_state - .database - .create_artist(post_data.into_inner()) - .await - { - Ok(_) => HttpResponse::Ok().body("Post succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("{}", e)), - } + get_response_from_query( + app_state + .database + .create_artist(request_data.into_inner()) + .await, + "POST".to_string(), + ) } #[put("/artist")] pub async fn put_artist( app_state: web::Data<AppState>, - post_data: web::Json<ArtistPut>, + request_data: web::Json<ArtistPut>, ) -> HttpResponse { - match app_state.database.edit_artist(post_data.into_inner()).await { - Ok(_) => HttpResponse::Ok().body("Put succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("{}", e)), - } + get_response_from_query( + app_state.database.edit_artist(request_data.into_inner()).await, + "PUT".to_string(), + ) } #[delete("/artist")] pub async fn delete_artist( app_state: web::Data<AppState>, - post_data: web::Json<Delete>, + request_data: web::Json<Delete>, ) -> HttpResponse { - let id: i32 = post_data + /* Check if ID is valid (return -1 if invalid) */ + let id: i32 = request_data .into_inner() .id .unwrap_or(String::default()) @@ -80,11 +85,13 @@ pub async fn delete_artist( .unwrap_or(-1); if id == -1 { - return HttpResponse::Ok().body("Invalid id value, code not executed\n"); + return HttpResponse::BadRequest().json(Response { + message: "Invalid id value, code not executed\n".to_owned(), + }); } - match app_state.database.delete_artist(id).await { - Ok(_) => HttpResponse::Ok().body("Deletion succeeded\n\n"), - Err(e) => HttpResponse::Ok().body(format!("There was an issue in the request:\n{}", e)), - } + get_response_from_query( + app_state.database.delete_artist(id).await, + "DELETE".to_string(), + ) } diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..d07079a --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,43 @@ +use actix_web::{web, HttpResponse, Scope}; +use serde::{Deserialize, Serialize}; + +pub mod album; +pub mod artist; +pub mod search_results; +pub mod song; + +pub fn api_scope() -> Scope { + web::scope("/api") + .service(song::get_song) + .service(song::post_song) + .service(song::put_song) + .service(song::delete_song) + .service(album::get_album) + .service(album::post_album) + .service(album::put_album) + .service(album::delete_album) + .service(artist::get_artist) + .service(artist::post_artist) + .service(artist::put_artist) + .service(artist::delete_artist) + .service(search_results::search_results) +} + +#[derive(Serialize, Deserialize)] +pub struct Response { + message: String, +} + +pub fn get_response_from_query( + query: Result<sqlx::mysql::MySqlQueryResult, sqlx::Error>, + method: String, +) -> HttpResponse { + match query { + Ok(_) => HttpResponse::Ok().json(Response { + message: format!("{} request executed with no errors", method).to_owned(), + }), + Err(e) => HttpResponse::BadRequest().json(Response { + message: format!("There was an issue in the request: {}", e).to_owned(), + }), + } +} diff --git a/src/api/search_results.rs b/src/api/search_results.rs index 6456ff9..585ba2c 100644 --- a/src/api/search_results.rs +++ b/src/api/search_results.rs @@ -14,7 +14,7 @@ pub async fn search_results( app_state: web::Data<AppState>, get_args: web::Query<SearchQueryOptions>, ) -> HttpResponse { - let default: String = String::from(""); + let default: String = String::default(); if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); diff --git a/src/api/song.rs b/src/api/song.rs index 850c759..698f27a 100644 --- a/src/api/song.rs +++ b/src/api/song.rs @@ -1,8 +1,10 @@ +use crate::api::{get_response_from_query, Response}; use crate::database::{Delete, Song, SongPost, SongPut}; use crate::AppState; use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::Deserialize; +/* Possible arguments ( /song?arg=value ) */ #[derive(Deserialize)] struct SongQueryOptions { id: Option<String>, @@ -16,20 +18,23 @@ pub async fn get_song( app_state: web::Data<AppState>, get_args: web::Query<SongQueryOptions>, ) -> HttpResponse { - let default = String::from(""); + /* Avoid lifespan issues */ + let default = String::default(); + /* Handle individual result for search-by-id */ if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); - let search_attempt: sqlx::Result<Option<Song>> = + let query_result: sqlx::Result<Option<Song>> = app_state.database.select_song_by_id(id).await; - return match search_attempt { + return match query_result { Ok(song_list) => HttpResponse::Ok().json(song_list), Err(e) => HttpResponse::Ok().body(format!("{}", e)), }; } - let search_attempt: sqlx::Result<Vec<Song>> = match true { + /* Handle N results */ + let query_result: sqlx::Result<Vec<Song>> = match true { _ if get_args.name.is_some() => { let name: &str = get_args.name.as_ref().unwrap_or(&default); app_state.database.select_songs_by_name(name).await @@ -45,7 +50,7 @@ pub async fn get_song( _ => app_state.database.select_songs().await, }; - match search_attempt { + match query_result { Ok(song_list) => HttpResponse::Ok().json(song_list), Err(e) => HttpResponse::Ok().body(format!("{}", e)), } @@ -54,31 +59,32 @@ pub async fn get_song( #[post("/song")] pub async fn post_song( app_state: web::Data<AppState>, - post_data: web::Json<SongPost>, + request_data: web::Json<SongPost>, ) -> HttpResponse { - match app_state.database.create_song(post_data.into_inner()).await { - Ok(_) => HttpResponse::Ok().body("Post succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("{}", e)), - } + get_response_from_query( + app_state.database.create_song(request_data.into_inner()).await, + "POST".to_string(), + ) } #[put("/song")] pub async fn put_song( app_state: web::Data<AppState>, - post_data: web::Json<SongPut>, + request_data: web::Json<SongPut>, ) -> HttpResponse { - match app_state.database.edit_song(post_data.into_inner()).await { - Ok(_) => HttpResponse::Ok().body("Put succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("{}", e)), - } + get_response_from_query( + app_state.database.edit_song(request_data.into_inner()).await, + "PUT".to_owned(), + ) } #[delete("/song")] pub async fn delete_song( app_state: web::Data<AppState>, - post_data: web::Json<Delete>, + request_data: web::Json<Delete>, ) -> HttpResponse { - let id: i32 = post_data + /* Check if ID is valid (return -1 if invalid) */ + let id: i32 = request_data .into_inner() .id .unwrap_or(String::default()) @@ -86,11 +92,13 @@ pub async fn delete_song( .unwrap_or(-1); if id == -1 { - return HttpResponse::Ok().body("Invalid id value, code not executed\n"); + return HttpResponse::BadRequest().json(Response { + message: "Invalid id value, code not executed".to_owned(), + }); } - match app_state.database.delete_song(id).await { - Ok(_) => HttpResponse::Ok().body("Deletion succeeded\n"), - Err(e) => HttpResponse::Ok().body(format!("There was an issue in the request:\n{}", e)), - } + get_response_from_query( + app_state.database.delete_song(id).await, + "DELETE".to_owned(), + ) } |