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")]
|
#[cfg(feature = "ssr")]
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct ItemRequest {
|
||||||
|
pub url: String,
|
||||||
|
pub item: DbItem,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub async fn get_items(
|
pub async fn get_items(
|
||||||
db: web::Data<Arc<Mutex<Database>>>,
|
db: web::Data<Arc<Mutex<Database>>>,
|
||||||
|
@ -25,16 +33,11 @@ pub async fn get_items(
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub async fn create_item(
|
pub async fn create_item(
|
||||||
db: web::Data<Arc<Mutex<Database>>>,
|
db: web::Data<Arc<Mutex<Database>>>,
|
||||||
url: web::Query<String>,
|
request: web::Json<ItemRequest>,
|
||||||
item: web::Json<DbItem>,
|
|
||||||
) -> HttpResponse {
|
) -> HttpResponse {
|
||||||
let db = db.lock().await;
|
match db.lock().await.insert_item_by_url(&request.url, &request.item).await {
|
||||||
match db.insert_item_by_url(&url, &item.into_inner()).await {
|
Ok(_) => HttpResponse::Ok().body("Item created"),
|
||||||
Ok(_) => HttpResponse::Ok().body("Item inserted"),
|
Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
|
||||||
Err(err) => {
|
|
||||||
leptos::logging::error!("Failed to insert item: {:?}", err);
|
|
||||||
HttpResponse::InternalServerError().body("Failed to insert item")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue