summaryrefslogtreecommitdiff
path: root/src/routes/album.rs
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2025-01-21 15:52:44 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2025-01-21 15:52:44 +0100
commitc277e9937115cc1473219a04199566c7bfdcbaf7 (patch)
treec2806eab444269d92c7c22d1165a9686f5edd9de /src/routes/album.rs
parent58515ddfecd3701112616c33ad4879dae4b38ffd (diff)
get, post, put, delete covered
Diffstat (limited to 'src/routes/album.rs')
-rw-r--r--src/routes/album.rs56
1 files changed, 52 insertions, 4 deletions
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<AppState>,
get_args: web::Query<AlbumQueryOptions>,
) -> 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<AppState>,
+ post_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)),
+ }
+}
+
+#[put("/album")]
+pub async fn put_album(
+ app_state: web::Data<AppState>,
+ post_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)),
+ }
+}
+
+#[delete("/album")]
+pub async fn delete_album(
+ 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_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)),
+ }
+}