From b6b1ebde9cd4773c03215528a2d43fbd4cd94ba4 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 18 Feb 2025 23:41:18 +0300 Subject: [PATCH] refactor(api): simplify API handlers and request bodies * Introduced `ItemRequest` struct to encapsulate URL and item data * Updated `create_item` handler to accept `ItemRequest` instead of separate URL and item parameters * Removed redundant error handling and logging in API handlers * Improved code organization and readability --- src/api.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/api.rs b/src/api.rs index 7d1c808..20df3de 100644 --- a/src/api.rs +++ b/src/api.rs @@ -7,6 +7,14 @@ use std::sync::Arc; #[cfg(feature = "ssr")] use tokio::sync::Mutex; +use serde::Deserialize; +#[cfg(feature = "ssr")] +#[derive(Deserialize)] +pub struct ItemRequest { + pub url: String, + pub item: DbItem, +} + #[cfg(feature = "ssr")] pub async fn get_items( db: web::Data>>, @@ -25,16 +33,11 @@ pub async fn get_items( #[cfg(feature = "ssr")] pub async fn create_item( db: web::Data>>, - url: web::Query, - item: web::Json, + request: web::Json, ) -> HttpResponse { - let db = db.lock().await; - match db.insert_item_by_url(&url, &item.into_inner()).await { - Ok(_) => HttpResponse::Ok().body("Item inserted"), - Err(err) => { - leptos::logging::error!("Failed to insert item: {:?}", err); - HttpResponse::InternalServerError().body("Failed to insert item") - } + match db.lock().await.insert_item_by_url(&request.url, &request.item).await { + Ok(_) => HttpResponse::Ok().body("Item created"), + Err(e) => HttpResponse::InternalServerError().body(e.to_string()), } }