From c277e9937115cc1473219a04199566c7bfdcbaf7 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Tue, 21 Jan 2025 15:52:44 +0100 Subject: get, post, put, delete covered --- src/routes/album.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/routes/artist.rs | 54 ++++++++++++++++++++++++++++++++++++++++++-------- src/routes/song.rs | 56 ++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 144 insertions(+), 22 deletions(-) (limited to 'src/routes') diff --git a/src/routes/album.rs b/src/routes/album.rs index dbf04f0..ab82e2e 100644 --- a/src/routes/album.rs +++ b/src/routes/album.rs @@ -1,6 +1,6 @@ -use crate::database::Album; +use crate::database::{Album, AlbumPost, AlbumPut, Delete}; use crate::AppState; -use actix_web::{get, web, HttpResponse}; +use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::Deserialize; #[derive(Deserialize)] @@ -11,11 +11,11 @@ struct AlbumQueryOptions { } #[get("/album")] -pub async fn album( +pub async fn get_album( app_state: web::Data, get_args: web::Query, ) -> HttpResponse { - let default: String = String::from(""); + let default = String::from(""); if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); @@ -44,3 +44,51 @@ pub async fn album( Err(e) => HttpResponse::Ok().body(format!("{}", e)), } } + +#[post("/album")] +pub async fn post_album( + app_state: web::Data, + post_data: web::Json, +) -> 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)), + } +} + +#[put("/album")] +pub async fn put_album( + app_state: web::Data, + post_data: web::Json, +) -> 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)), + } +} + +#[delete("/album")] +pub async fn delete_album( + app_state: web::Data, + post_data: web::Json, +) -> HttpResponse { + let id: i32 = post_data + .into_inner() + .id + .unwrap_or(String::default()) + .parse::() + .unwrap_or(-1); + + if id == -1 { + return HttpResponse::Ok().body("Invalid id value, code not executed\n"); + } + + match app_state.database.delete_album(id).await { + Ok(_) => HttpResponse::Ok().body("Deletion succeeded\n"), + Err(e) => HttpResponse::Ok().body(format!("There was an issue in the request:\n{}", e)), + } +} diff --git a/src/routes/artist.rs b/src/routes/artist.rs index dc481f3..81ae773 100644 --- a/src/routes/artist.rs +++ b/src/routes/artist.rs @@ -1,6 +1,6 @@ -use crate::database::Artist; +use crate::database::{Artist, ArtistPost, ArtistPut, Delete}; use crate::AppState; -use actix_web::{get, post, web, HttpResponse}; +use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::Deserialize; #[derive(Deserialize)] @@ -14,7 +14,7 @@ pub async fn get_artist( app_state: web::Data, get_args: web::Query, ) -> HttpResponse { - let default: String = String::from(""); + let default = String::from(""); if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); @@ -43,10 +43,48 @@ pub async fn get_artist( #[post("/artist")] pub async fn post_artist( -// app_state: web::Data, + app_state: web::Data, + post_data: web::Json, ) -> HttpResponse { -// if get_args.body.is_some() { -// HttpResponse::Ok().json("{}"); -// } - HttpResponse::Ok().body("bad") + 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)), + } +} + +#[put("/artist")] +pub async fn put_artist( + app_state: web::Data, + post_data: web::Json, +) -> 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)), + } +} + +#[delete("/artist")] +pub async fn delete_artist( + app_state: web::Data, + post_data: web::Json, +) -> HttpResponse { + let id: i32 = post_data + .into_inner() + .id + .unwrap_or(String::default()) + .parse::() + .unwrap_or(-1); + + if id == -1 { + return HttpResponse::Ok().body("Invalid id value, code not executed\n"); + } + + 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)), + } } diff --git a/src/routes/song.rs b/src/routes/song.rs index 97513f6..850c759 100644 --- a/src/routes/song.rs +++ b/src/routes/song.rs @@ -1,6 +1,6 @@ -use crate::database::Song; +use crate::database::{Delete, Song, SongPost, SongPut}; use crate::AppState; -use actix_web::{get, web, HttpResponse}; +use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::Deserialize; #[derive(Deserialize)] @@ -16,7 +16,7 @@ pub async fn get_song( app_state: web::Data, get_args: web::Query, ) -> HttpResponse { - let default: String = String::from(""); + let default = String::from(""); if get_args.id.is_some() { let id: &str = get_args.id.as_ref().unwrap_or(&default); @@ -51,10 +51,46 @@ pub async fn get_song( } } -// #[post("/song")] -// pub async fn post_song( -// app_state: web::Data, -// get_args: web::Query, -// ) -> HttpResponse { -// HttpResponse::Ok().body("Post carried successfully") -// } +#[post("/song")] +pub async fn post_song( + app_state: web::Data, + post_data: web::Json, +) -> 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)), + } +} + +#[put("/song")] +pub async fn put_song( + app_state: web::Data, + post_data: web::Json, +) -> 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)), + } +} + +#[delete("/song")] +pub async fn delete_song( + app_state: web::Data, + post_data: web::Json, +) -> HttpResponse { + let id: i32 = post_data + .into_inner() + .id + .unwrap_or(String::default()) + .parse::() + .unwrap_or(-1); + + if id == -1 { + return HttpResponse::Ok().body("Invalid id value, code not executed\n"); + } + + 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)), + } +} -- cgit v1.2.3