fix(debug): add logging to solve db saving issues
This commit is contained in:
parent
e90a6be010
commit
585a4a6eb7
3 changed files with 73 additions and 10 deletions
35
src/api.rs
35
src/api.rs
|
@ -8,11 +8,15 @@ use std::sync::Arc;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
use crate::models::item::Item;
|
use crate::models::item::Item;
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use std::collections::HashMap;
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use leptos::logging::log;
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
#[derive(Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct ItemRequest {
|
pub struct ItemRequest {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub item: Item,
|
pub item: Item,
|
||||||
|
@ -38,9 +42,27 @@ pub async fn create_item(
|
||||||
db: web::Data<Arc<Mutex<Database>>>,
|
db: web::Data<Arc<Mutex<Database>>>,
|
||||||
request: web::Json<ItemRequest>,
|
request: web::Json<ItemRequest>,
|
||||||
) -> HttpResponse {
|
) -> HttpResponse {
|
||||||
match db.lock().await.insert_item_by_url(&request.url, &request.item).await {
|
let db = db.lock().await;
|
||||||
Ok(_) => HttpResponse::Ok().body("Item created"),
|
let url = request.url.clone();
|
||||||
Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
|
let item = request.item.clone();
|
||||||
|
let item_id = request.item.id.clone();
|
||||||
|
// request logging
|
||||||
|
log!("[API] Received item request - URL: {}, Item ID: {}",
|
||||||
|
request.url, request.item.id);
|
||||||
|
|
||||||
|
// raw JSON logging
|
||||||
|
let raw_json = serde_json::to_string(&request.into_inner()).unwrap();
|
||||||
|
log!("[API] Raw request JSON: {}", raw_json);
|
||||||
|
|
||||||
|
match db.insert_item_by_url(&url, &item).await {
|
||||||
|
Ok(_) => {
|
||||||
|
log!("[API] Successfully saved item ID: {}", item_id);
|
||||||
|
HttpResponse::Ok().json(item)
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
log!("[API] Database error: {:?}", e);
|
||||||
|
HttpResponse::BadRequest().body(format!("Database error: {}", e))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +101,9 @@ pub async fn delete_property(
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub async fn get_items_by_url(
|
pub async fn get_items_by_url(
|
||||||
db: web::Data<Arc<Mutex<Database>>>,
|
db: web::Data<Arc<Mutex<Database>>>,
|
||||||
url: web::Path<String>,
|
query: web::Query<HashMap<String, String>>,
|
||||||
) -> HttpResponse {
|
) -> HttpResponse {
|
||||||
|
let url = query.get("url").unwrap_or(&String::new()).to_string();
|
||||||
let db = db.lock().await;
|
let db = db.lock().await;
|
||||||
match db.get_items_by_url(&url).await {
|
match db.get_items_by_url(&url).await {
|
||||||
Ok(items) => HttpResponse::Ok().json(items),
|
Ok(items) => HttpResponse::Ok().json(items),
|
||||||
|
|
|
@ -33,7 +33,8 @@ pub async fn load_items_from_db(current_url: &str) -> Result<Vec<Item>, String>
|
||||||
|
|
||||||
Ok(items)
|
Ok(items)
|
||||||
} else {
|
} else {
|
||||||
Err(format!("Failed to fetch items: {}", response.status_text()))
|
let body = response.text().await.unwrap_or_default();
|
||||||
|
Err(format!("Server error ({}): {}", response.status(), body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,10 +153,12 @@ pub fn ItemsList(
|
||||||
|
|
||||||
// Function to send an item to the backend API
|
// Function to send an item to the backend API
|
||||||
async fn save_item_to_db(item: Item, selected_properties: ReadSignal<HashMap<String, bool>>, current_url: String) {
|
async fn save_item_to_db(item: Item, selected_properties: ReadSignal<HashMap<String, bool>>, current_url: String) {
|
||||||
|
|
||||||
|
let custom_props = item.custom_properties.clone();
|
||||||
// Use a reactive closure to access `selected_properties`
|
// Use a reactive closure to access `selected_properties`
|
||||||
let custom_properties: HashMap<String, String> = (move || {
|
let custom_properties: HashMap<String, String> = (move || {
|
||||||
let selected_props = selected_properties.get(); // Access the signal inside a reactive closure
|
let selected_props = selected_properties.get(); // Access the signal inside a reactive closure
|
||||||
item.custom_properties
|
custom_props
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|(key, _)| selected_props.contains_key(key)) // Use the extracted value
|
.filter(|(key, _)| selected_props.contains_key(key)) // Use the extracted value
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -168,6 +171,9 @@ pub fn ItemsList(
|
||||||
item: Item,
|
item: Item,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log!("[FRONTEND] Saving item - ID: {}, Name: '{}', Properties: {:?}",
|
||||||
|
item.id, item.name, item.custom_properties);
|
||||||
|
|
||||||
let item_to_send = ItemRequest {
|
let item_to_send = ItemRequest {
|
||||||
url: current_url.to_string(),
|
url: current_url.to_string(),
|
||||||
item: Item {
|
item: Item {
|
||||||
|
@ -185,6 +191,8 @@ pub fn ItemsList(
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
log!("[FRONTEND] Save response status: {:?}", response.as_ref().map(|r| r.status()));
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
if resp.status() == 200 {
|
if resp.status() == 200 {
|
||||||
|
|
32
src/db.rs
32
src/db.rs
|
@ -7,6 +7,7 @@ mod db_impl {
|
||||||
use leptos::logging;
|
use leptos::logging;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use crate::models::item::Item;
|
use crate::models::item::Item;
|
||||||
|
use leptos::logging::log;
|
||||||
|
|
||||||
// Define a struct to represent a database connection
|
// Define a struct to represent a database connection
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -207,13 +208,20 @@ mod db_impl {
|
||||||
url: &str,
|
url: &str,
|
||||||
item: &Item
|
item: &Item
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
|
// Log before DB operations
|
||||||
|
log!("[DATABASE] Inserting item - ID: {}, Name: '{}'", item.id, item.name);
|
||||||
let conn = self.conn.lock().await;
|
let conn = self.conn.lock().await;
|
||||||
|
|
||||||
// Get or create URL record
|
// Get or create URL record
|
||||||
let url_id = match self.get_url_id(url).await {
|
let url_id = match self.get_url_id(url).await {
|
||||||
Ok(Some(id)) => id,
|
Ok(Some(id)) => id,
|
||||||
_ => self.insert_url(url).await?,
|
_ => self.insert_url(url).await?,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Log final SQL parameters
|
||||||
|
log!("[DATABASE] SQL params - ID: {}, URL ID: {}, Name: '{}'",
|
||||||
|
item.id, url_id, item.name);
|
||||||
|
|
||||||
// Insert item with URL relationship
|
// Insert item with URL relationship
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO items (id, url_id, name, description, wikidata_id)
|
"INSERT INTO items (id, url_id, name, description, wikidata_id)
|
||||||
|
@ -231,6 +239,7 @@ mod db_impl {
|
||||||
&[&item.id, &prop_id.to_string(), &value],
|
&[&item.id, &prop_id.to_string(), &value],
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
log!("[DATABASE] Successfully inserted item ID: {}", item.id);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,6 +271,29 @@ mod db_impl {
|
||||||
logging::log!("Property deleted from the database for URL: {}", url);
|
logging::log!("Property deleted from the database for URL: {}", url);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
// function to log database state
|
||||||
|
pub async fn debug_dump(&self) -> Result<(), Error> {
|
||||||
|
let conn = self.conn.lock().await;
|
||||||
|
log!("[DATABASE DEBUG] URLs:");
|
||||||
|
let mut stmt = conn.prepare("SELECT id, url FROM urls")?;
|
||||||
|
let urls = stmt.query_map([], |row| {
|
||||||
|
Ok(format!("ID: {}, URL: {}", row.get::<_, i64>(0)?, row.get::<_, String>(1)?))
|
||||||
|
})?;
|
||||||
|
for url in urls {
|
||||||
|
log!("[DATABASE DEBUG] {}", url?);
|
||||||
|
}
|
||||||
|
|
||||||
|
log!("[DATABASE DEBUG] Items:");
|
||||||
|
let mut stmt = conn.prepare("SELECT id, name FROM items")?;
|
||||||
|
let items = stmt.query_map([], |row| {
|
||||||
|
Ok(format!("ID: {}, Name: '{}'", row.get::<_, String>(0)?, row.get::<_, String>(1)?))
|
||||||
|
})?;
|
||||||
|
for item in items {
|
||||||
|
log!("[DATABASE DEBUG] {}", item?);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a struct to represent an item in the database
|
// Define a struct to represent an item in the database
|
||||||
|
|
Loading…
Add table
Reference in a new issue