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
This commit is contained in:
parent
74bd1a89e5
commit
b6b1ebde9c
1 changed files with 12 additions and 9 deletions
21
src/api.rs
21
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<Arc<Mutex<Database>>>,
|
||||
|
@ -25,16 +33,11 @@ pub async fn get_items(
|
|||
#[cfg(feature = "ssr")]
|
||||
pub async fn create_item(
|
||||
db: web::Data<Arc<Mutex<Database>>>,
|
||||
url: web::Query<String>,
|
||||
item: web::Json<DbItem>,
|
||||
request: web::Json<ItemRequest>,
|
||||
) -> 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()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue