feat (items_list.rs): simplify deserialization and remove unnecessary conversions
This commit is contained in:
parent
63aaa57fa1
commit
e90a6be010
1 changed files with 15 additions and 33 deletions
|
@ -23,28 +23,15 @@ pub async fn load_items_from_db(current_url: &str) -> Result<Vec<Item>, String>
|
||||||
.await
|
.await
|
||||||
.map_err(|err| format!("Failed to fetch items: {:?}", err))?;
|
.map_err(|err| format!("Failed to fetch items: {:?}", err))?;
|
||||||
|
|
||||||
if response.status() == 200 {
|
if response.status() == 200 {
|
||||||
// Deserialize into Vec<DbItem>
|
// Deserialize into Vec<Item>
|
||||||
log!("Loading items from DB...");
|
log!("Loading items from DB...");
|
||||||
let db_items = response
|
let items = response
|
||||||
.json::<Vec<Item>>()
|
.json::<Vec<Item>>()
|
||||||
.await
|
.await
|
||||||
.map_err(|err| format!("Failed to parse items: {:?}", err))?;
|
.map_err(|err| format!("Failed to parse items: {:?}", err))?;
|
||||||
|
|
||||||
// log!("Deserialized DB items: {:?}", db_items);
|
Ok(items)
|
||||||
|
|
||||||
// Convert DbItem to Item
|
|
||||||
let items = db_items.into_iter().map(|db_item| {
|
|
||||||
Item {
|
|
||||||
id: db_item.id,
|
|
||||||
name: db_item.name,
|
|
||||||
description: db_item.description,
|
|
||||||
wikidata_id: db_item.wikidata_id,
|
|
||||||
custom_properties: HashMap::new() // Now populated from joins
|
|
||||||
}
|
|
||||||
}).collect();
|
|
||||||
// log!("Converted items: {:?}", items);
|
|
||||||
Ok(items)
|
|
||||||
} else {
|
} else {
|
||||||
Err(format!("Failed to fetch items: {}", response.status_text()))
|
Err(format!("Failed to fetch items: {}", response.status_text()))
|
||||||
}
|
}
|
||||||
|
@ -174,27 +161,22 @@ pub fn ItemsList(
|
||||||
.collect()
|
.collect()
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Serialize `custom_properties` to a JSON string
|
|
||||||
let custom_properties = serde_json::to_string(&custom_properties).unwrap();
|
|
||||||
|
|
||||||
// Create a new struct to send to the backend
|
// Create a new struct to send to the backend
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
struct ItemToSend {
|
struct ItemRequest {
|
||||||
url: String,
|
url: String,
|
||||||
id: String,
|
item: Item,
|
||||||
name: String,
|
|
||||||
description: String,
|
|
||||||
wikidata_id: Option<String>,
|
|
||||||
custom_properties: String, // JSON-encoded string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let item_to_send = ItemToSend {
|
let item_to_send = ItemRequest {
|
||||||
url: current_url.to_string(),
|
url: current_url.to_string(),
|
||||||
|
item: Item {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
name: item.name,
|
name: item.name,
|
||||||
description: item.description,
|
description: item.description,
|
||||||
wikidata_id: item.wikidata_id,
|
wikidata_id: item.wikidata_id,
|
||||||
custom_properties, // Use the serialized string
|
custom_properties, // Use the filtered HashMap
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = gloo_net::http::Request::post("/api/items")
|
let response = gloo_net::http::Request::post("/api/items")
|
||||||
|
|
Loading…
Add table
Reference in a new issue