diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-21 15:52:44 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-21 15:52:44 +0100 |
commit | c277e9937115cc1473219a04199566c7bfdcbaf7 (patch) | |
tree | c2806eab444269d92c7c22d1165a9686f5edd9de /src/routes/artist.rs | |
parent | 58515ddfecd3701112616c33ad4879dae4b38ffd (diff) |
get, post, put, delete covered
Diffstat (limited to 'src/routes/artist.rs')
-rw-r--r-- | src/routes/artist.rs | 54 |
1 files changed, 46 insertions, 8 deletions
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<AppState>, get_args: web::Query<ArtistQueryOptions>, ) -> 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<AppState>, + app_state: web::Data<AppState>, + post_data: web::Json<ArtistPost>, ) -> 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<AppState>, + post_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)), + } +} + +#[delete("/artist")] +pub async fn delete_artist( + app_state: web::Data<AppState>, + post_data: web::Json<Delete>, +) -> HttpResponse { + let id: i32 = post_data + .into_inner() + .id + .unwrap_or(String::default()) + .parse::<i32>() + .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)), + } } |