Compare commits
2 commits
74bd1a89e5
...
8e3c87f315
Author | SHA1 | Date | |
---|---|---|---|
8e3c87f315 | |||
b6b1ebde9c |
2 changed files with 22 additions and 14 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()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -1,9 +1,9 @@
|
|||
#[cfg(feature = "ssr")]
|
||||
use actix_web::{web, App, HttpServer, HttpResponse, Responder};
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use compareware::db::{Database, DbItem};
|
||||
use compareware::api::{create_item, get_items, delete_item_by_url};
|
||||
use compareware::api::{ItemRequest,create_item, get_items, delete_item_by_url};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
|
@ -12,7 +12,7 @@ async fn main() -> std::io::Result<()> {
|
|||
use leptos::*;
|
||||
use leptos_actix::{generate_route_list, LeptosRoutes};
|
||||
use compareware::app::*;
|
||||
use compareware::db::{Database, DbItem};
|
||||
use compareware::db::Database;
|
||||
use compareware::api::{get_items, create_item, delete_item, delete_property, delete_item_by_url, delete_property_by_url, create_item_by_url, get_items_by_url}; // Import API handlers
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
@ -85,10 +85,15 @@ async fn get_items_handler(
|
|||
// Handler to create an item for a specific URL
|
||||
async fn create_item_handler(
|
||||
db: web::Data<Arc<Mutex<Database>>>,
|
||||
url: web::Path<String>,
|
||||
path: web::Path<String>,
|
||||
item: web::Json<DbItem>,
|
||||
) -> impl Responder {
|
||||
create_item(db, web::Query(url.into_inner()), item).await
|
||||
let url = path.into_inner();
|
||||
let request = ItemRequest {
|
||||
url,
|
||||
item: item.into_inner()
|
||||
};
|
||||
create_item(db, web::Json(request)).await
|
||||
}
|
||||
|
||||
// Handler to delete an item for a specific URL
|
||||
|
|
Loading…
Add table
Reference in a new issue