feat(db): run db on backend using actix web
This commit is contained in:
parent
af3f89c561
commit
291cb05847
7 changed files with 208 additions and 114 deletions
106
Cargo.lock
generated
106
Cargo.lock
generated
|
@ -127,7 +127,7 @@ dependencies = [
|
||||||
"actix-utils",
|
"actix-utils",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"mio",
|
"mio 1.0.3",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
@ -409,7 +409,7 @@ dependencies = [
|
||||||
"miniz_oxide",
|
"miniz_oxide",
|
||||||
"object",
|
"object",
|
||||||
"rustc-demangle",
|
"rustc-demangle",
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -746,7 +746,9 @@ dependencies = [
|
||||||
"leptos_actix",
|
"leptos_actix",
|
||||||
"leptos_meta",
|
"leptos_meta",
|
||||||
"leptos_router",
|
"leptos_router",
|
||||||
|
"mio 0.8.11",
|
||||||
"nostr-sdk",
|
"nostr-sdk",
|
||||||
|
"paste",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
"secp256k1 0.30.0",
|
"secp256k1 0.30.0",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -1995,6 +1997,18 @@ dependencies = [
|
||||||
"adler2",
|
"adler2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mio"
|
||||||
|
version = "0.8.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"wasi",
|
||||||
|
"windows-sys 0.48.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.0.3"
|
version = "1.0.3"
|
||||||
|
@ -2167,7 +2181,7 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -3063,7 +3077,7 @@ dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio 1.0.3",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
|
@ -3493,13 +3507,22 @@ dependencies = [
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.48.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets 0.48.5",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.52.0"
|
version = "0.52.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -3508,7 +3531,22 @@ version = "0.59.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm 0.48.5",
|
||||||
|
"windows_aarch64_msvc 0.48.5",
|
||||||
|
"windows_i686_gnu 0.48.5",
|
||||||
|
"windows_i686_msvc 0.48.5",
|
||||||
|
"windows_x86_64_gnu 0.48.5",
|
||||||
|
"windows_x86_64_gnullvm 0.48.5",
|
||||||
|
"windows_x86_64_msvc 0.48.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -3517,28 +3555,46 @@ version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows_aarch64_gnullvm",
|
"windows_aarch64_gnullvm 0.52.6",
|
||||||
"windows_aarch64_msvc",
|
"windows_aarch64_msvc 0.52.6",
|
||||||
"windows_i686_gnu",
|
"windows_i686_gnu 0.52.6",
|
||||||
"windows_i686_gnullvm",
|
"windows_i686_gnullvm",
|
||||||
"windows_i686_msvc",
|
"windows_i686_msvc 0.52.6",
|
||||||
"windows_x86_64_gnu",
|
"windows_x86_64_gnu 0.52.6",
|
||||||
"windows_x86_64_gnullvm",
|
"windows_x86_64_gnullvm 0.52.6",
|
||||||
"windows_x86_64_msvc",
|
"windows_x86_64_msvc 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_gnullvm"
|
name = "windows_aarch64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
|
@ -3551,24 +3607,48 @@ version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
|
|
|
@ -15,6 +15,7 @@ leptos = { version = "0.6" }
|
||||||
leptos_meta = { version = "0.6" }
|
leptos_meta = { version = "0.6" }
|
||||||
leptos_actix = { version = "0.6", optional = true }
|
leptos_actix = { version = "0.6", optional = true }
|
||||||
leptos_router = { version = "0.6" }
|
leptos_router = { version = "0.6" }
|
||||||
|
paste = "1.0"
|
||||||
wasm-bindgen = "=0.2.99"
|
wasm-bindgen = "=0.2.99"
|
||||||
rusqlite = { version = "0.27.0", optional = true}
|
rusqlite = { version = "0.27.0", optional = true}
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
@ -29,6 +30,7 @@ wasm-bindgen-futures = "0.4"
|
||||||
serde_json="1.0.133"
|
serde_json="1.0.133"
|
||||||
thiserror = "2.0.9"
|
thiserror = "2.0.9"
|
||||||
zerofrom = "0.1"
|
zerofrom = "0.1"
|
||||||
|
mio = "0.8"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["ssr"]
|
default = ["ssr"]
|
||||||
|
|
61
src/api.rs
61
src/api.rs
|
@ -1,46 +1,35 @@
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
use leptos::*;
|
use actix_web::{web, HttpResponse};
|
||||||
#[cfg(feature = "ssr")]
|
|
||||||
use leptos::logging::log;
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
use crate::db::{Database, DbItem};
|
use crate::db::{Database, DbItem};
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use std::sync::Arc;
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
#[server(UpdateItem, "/api")]
|
pub async fn get_items(db: web::Data<Arc<Mutex<Database>>>) -> HttpResponse {
|
||||||
pub async fn update_item_db(client_db_item: ClientDbItem) -> Result<(), ServerFnError> {
|
let db = db.lock().await;
|
||||||
|
match db.get_items().await {
|
||||||
let db_item = DbItem {
|
Ok(items) => HttpResponse::Ok().json(items),
|
||||||
id: client_db_item.id,
|
Err(err) => {
|
||||||
name: client_db_item.name,
|
leptos::logging::error!("Failed to fetch items: {:?}", err);
|
||||||
description: client_db_item.description,
|
HttpResponse::InternalServerError().body("Failed to fetch items")
|
||||||
wikidata_id: client_db_item.wikidata_id,
|
|
||||||
custom_properties: client_db_item.custom_properties,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Log the start of the function
|
|
||||||
log!("Starting update_item function for item: {:?}", db_item);
|
|
||||||
|
|
||||||
// Open the database
|
|
||||||
let db = match Database::new("items.db") {
|
|
||||||
Ok(db) => {
|
|
||||||
log!("Database opened successfully");
|
|
||||||
db
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
}
|
||||||
log!("Failed to open database: {}", e);
|
}
|
||||||
return Err(ServerFnError::ServerError(e.to_string()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Insert the item into the database
|
#[cfg(feature = "ssr")]
|
||||||
match db.insert_item(&db_item) {
|
pub async fn create_item(
|
||||||
Ok(_) => {
|
db: web::Data<Arc<Mutex<Database>>>,
|
||||||
log!("Item inserted successfully: {:?}", db_item);
|
item: web::Json<DbItem>,
|
||||||
Ok(())
|
) -> HttpResponse {
|
||||||
}
|
let db = db.lock().await;
|
||||||
Err(e) => {
|
match db.insert_item(&item.into_inner()).await {
|
||||||
log!("Failed to insert item into database: {}", e);
|
Ok(_) => HttpResponse::Ok().body("Item inserted"),
|
||||||
Err(ServerFnError::ServerError(e.to_string()))
|
Err(err) => {
|
||||||
|
leptos::logging::error!("Failed to insert item: {:?}", err);
|
||||||
|
HttpResponse::InternalServerError().body("Failed to insert item")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,17 +1,13 @@
|
||||||
use crate::components::editable_cell::EditableCell;
|
use crate::components::editable_cell::EditableCell;
|
||||||
use crate::components::editable_cell::InputType;
|
use crate::components::editable_cell::InputType;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Deserialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use leptos::logging::log;
|
use leptos::logging::log;
|
||||||
use crate::models::item::Item;
|
use crate::models::item::Item;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use crate::api::update_item_db;
|
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
|
||||||
use crate::db::{Database, DbItem};
|
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
struct WikidataSuggestion {
|
struct WikidataSuggestion {
|
||||||
|
@ -20,15 +16,6 @@ struct WikidataSuggestion {
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
|
||||||
struct ClientDbItem {
|
|
||||||
id: String,
|
|
||||||
name: String,
|
|
||||||
description: String,
|
|
||||||
wikidata_id: Option<String>,
|
|
||||||
custom_properties: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn ItemsList(
|
pub fn ItemsList(
|
||||||
items: ReadSignal<Vec<Item>>,
|
items: ReadSignal<Vec<Item>>,
|
||||||
|
@ -49,23 +36,6 @@ pub fn ItemsList(
|
||||||
//signal to store the fetched property labels
|
//signal to store the fetched property labels
|
||||||
let (property_labels, set_property_labels) = create_signal(HashMap::<String, String>::new());
|
let (property_labels, set_property_labels) = create_signal(HashMap::<String, String>::new());
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
|
||||||
{
|
|
||||||
log!("SSR feature is enabled, attempting to update database...");
|
|
||||||
log!("Is SSR enabled? {}", cfg!(feature = "ssr"));
|
|
||||||
let db_path = "items.db"; // path to the database file
|
|
||||||
let db = Database::new(db_path).unwrap();
|
|
||||||
db.create_schema().unwrap();
|
|
||||||
|
|
||||||
let db_items = db.get_items().unwrap();
|
|
||||||
let loaded_items: Vec<Item> = db_items.into_iter().map(|db_item| {
|
|
||||||
serde_json::from_str(&db_item.custom_properties).unwrap()
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
spawn_local(async move {
|
|
||||||
set_items.set(loaded_items);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Ensure there's an initial empty row
|
// Ensure there's an initial empty row
|
||||||
if items.get().is_empty() {
|
if items.get().is_empty() {
|
||||||
set_items.set(vec![Item {
|
set_items.set(vec![Item {
|
||||||
|
@ -271,26 +241,26 @@ pub fn ItemsList(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the updated item to the server using the API endpoint
|
// // Send the updated item to the server using the API endpoint
|
||||||
let client_db_item = ClientDbItem {
|
// let client_db_item = ClientDbItem {
|
||||||
id: item.id.clone(),
|
// id: item.id.clone(),
|
||||||
name: item.name.clone(),
|
// name: item.name.clone(),
|
||||||
description: item.description.clone(),
|
// description: item.description.clone(),
|
||||||
wikidata_id: item.wikidata_id.clone(),
|
// wikidata_id: item.wikidata_id.clone(),
|
||||||
custom_properties: serde_json::to_string(&item.custom_properties).unwrap(),
|
// custom_properties: serde_json::to_string(&item.custom_properties).unwrap(),
|
||||||
};
|
// };
|
||||||
|
|
||||||
|
|
||||||
spawn_local(async move {
|
// spawn_local(async move {
|
||||||
match update_item_db(client_db_item).await {
|
// match update_item_db(client_db_item).await {
|
||||||
Ok(_) => {
|
// Ok(_) => {
|
||||||
log!("Item updated successfully on the server");
|
// log!("Item updated successfully on the server");
|
||||||
}
|
// }
|
||||||
Err(e) => {
|
// Err(e) => {
|
||||||
log!("Error updating item on the server: {}", e);
|
// log!("Error updating item on the server: {}", e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
// Automatically add a new row when editing the last row
|
// Automatically add a new row when editing the last row
|
||||||
if index == items.len() - 1 && !value.is_empty() {
|
if index == items.len() - 1 && !value.is_empty() {
|
||||||
|
|
66
src/db.rs
66
src/db.rs
|
@ -2,47 +2,64 @@
|
||||||
mod db_impl {
|
mod db_impl {
|
||||||
use rusqlite::{Connection, Error};
|
use rusqlite::{Connection, Error};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use leptos::logging;
|
||||||
|
|
||||||
// Define a struct to represent a database connection
|
// Define a struct to represent a database connection
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
conn: Connection,
|
conn: Arc<Mutex<Connection>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
// Create a new database connection
|
// Create a new database connection
|
||||||
pub fn new(db_path: &str) -> Result<Self, Error> {
|
pub fn new(db_path: &str) -> Result<Self, Error> {
|
||||||
let conn = Connection::open(db_path)?;
|
let conn = Connection::open(db_path)?;
|
||||||
Ok(Database { conn })
|
logging::log!("Database connection established at: {}", db_path); // Log with Leptos
|
||||||
|
Ok(Database {
|
||||||
|
conn: Arc::new(Mutex::new(conn)),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the database schema
|
// Create the database schema
|
||||||
pub fn create_schema(&self) -> Result<(), Error> {
|
pub async fn create_schema(&self) -> Result<(), Error> {
|
||||||
self.conn.execute_batch("
|
let conn = self.conn.lock().await;
|
||||||
CREATE TABLE IF NOT EXISTS items (
|
conn.execute_batch(
|
||||||
|
"CREATE TABLE IF NOT EXISTS items (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
wikidata_id TEXT,
|
wikidata_id TEXT,
|
||||||
custom_properties TEXT
|
custom_properties TEXT
|
||||||
);
|
);",
|
||||||
")?;
|
)?;
|
||||||
|
logging::log!("Database schema created or verified"); // Log with Leptos
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert a new item into the database
|
// Insert a new item into the database
|
||||||
pub fn insert_item(&self, item: &DbItem) -> Result<(), Error> {
|
pub async fn insert_item(&self, item: &DbItem) -> Result<(), Error> {
|
||||||
|
let conn = self.conn.lock().await;
|
||||||
let wikidata_id = item.wikidata_id.as_ref().map(|s| s.as_str()).unwrap_or("");
|
let wikidata_id = item.wikidata_id.as_ref().map(|s| s.as_str()).unwrap_or("");
|
||||||
self.conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO items (id, name, description, wikidata_id, custom_properties) VALUES (?, ?, ?, ?, ?);",
|
"INSERT INTO items (id, name, description, wikidata_id, custom_properties) VALUES (?, ?, ?, ?, ?);",
|
||||||
&[&item.id, &item.name, &item.description, &wikidata_id.to_string(), &item.custom_properties],
|
&[
|
||||||
|
&item.id,
|
||||||
|
&item.name,
|
||||||
|
&item.description,
|
||||||
|
&wikidata_id.to_string(),
|
||||||
|
&item.custom_properties,
|
||||||
|
],
|
||||||
)?;
|
)?;
|
||||||
|
logging::log!("Item inserted: {}", item.id); // Log with Leptos
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve all items from the database
|
// Retrieve all items from the database
|
||||||
pub fn get_items(&self) -> Result<Vec<DbItem>, Error> {
|
pub async fn get_items(&self) -> Result<Vec<DbItem>, Error> {
|
||||||
let mut stmt = self.conn.prepare("SELECT * FROM items;")?;
|
let conn = self.conn.lock().await;
|
||||||
|
let mut stmt = conn.prepare("SELECT * FROM items;")?;
|
||||||
let items = stmt.query_map([], |row| {
|
let items = stmt.query_map([], |row| {
|
||||||
Ok(DbItem {
|
Ok(DbItem {
|
||||||
id: row.get(0)?,
|
id: row.get(0)?,
|
||||||
|
@ -56,6 +73,7 @@ mod db_impl {
|
||||||
for item in items {
|
for item in items {
|
||||||
result.push(item?);
|
result.push(item?);
|
||||||
}
|
}
|
||||||
|
logging::log!("Fetched {} items from the database", result.len()); // Log with Leptos
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +87,29 @@ mod db_impl {
|
||||||
pub wikidata_id: Option<String>,
|
pub wikidata_id: Option<String>,
|
||||||
pub custom_properties: String,
|
pub custom_properties: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implement conversion from DbItem to a JSON-friendly format
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct ItemResponse {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub wikidata_id: Option<String>,
|
||||||
|
pub custom_properties: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<DbItem> for ItemResponse {
|
||||||
|
fn from(item: DbItem) -> Self {
|
||||||
|
ItemResponse {
|
||||||
|
id: item.id,
|
||||||
|
name: item.name,
|
||||||
|
description: item.description,
|
||||||
|
wikidata_id: item.wikidata_id,
|
||||||
|
custom_properties: item.custom_properties,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub use db_impl::{Database, DbItem};
|
pub use db_impl::{Database, DbItem, ItemResponse};
|
19
src/main.rs
19
src/main.rs
|
@ -1,6 +1,3 @@
|
||||||
use actix_web::{web, App, HttpServer};
|
|
||||||
use compareware::api::update_item_db; // Corrected server function name
|
|
||||||
use compareware::db::Database;
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
@ -9,11 +6,20 @@ async fn main() -> std::io::Result<()> {
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_actix::{generate_route_list, LeptosRoutes};
|
use leptos_actix::{generate_route_list, LeptosRoutes};
|
||||||
use compareware::app::*;
|
use compareware::app::*;
|
||||||
|
use compareware::db::{Database, DbItem};
|
||||||
|
use compareware::api::{get_items, create_item}; // Import API handlers
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
let conf = get_configuration(None).await.unwrap();
|
let conf = get_configuration(None).await.unwrap();
|
||||||
let addr = conf.leptos_options.site_addr;
|
let addr = conf.leptos_options.site_addr;
|
||||||
|
|
||||||
|
// Initialize the database
|
||||||
|
let db = Database::new("compareware.db").unwrap();
|
||||||
|
db.create_schema().await.unwrap(); // Ensure the schema is created
|
||||||
|
let db = Arc::new(Mutex::new(db)); // Wrap the database in an Arc<Mutex<T>> for shared state
|
||||||
|
|
||||||
// Generate the list of routes in your Leptos App
|
// Generate the list of routes in your Leptos App
|
||||||
let routes = generate_route_list(App);
|
let routes = generate_route_list(App);
|
||||||
println!("listening on http://{}", &addr);
|
println!("listening on http://{}", &addr);
|
||||||
|
@ -22,6 +28,8 @@ async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let leptos_options = &conf.leptos_options;
|
let leptos_options = &conf.leptos_options;
|
||||||
let site_root = &leptos_options.site_root;
|
let site_root = &leptos_options.site_root;
|
||||||
|
let db = db.clone(); // Clone the Arc for each worker
|
||||||
|
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
// Register server functions
|
// Register server functions
|
||||||
|
@ -37,6 +45,11 @@ async fn main() -> std::io::Result<()> {
|
||||||
// Pass Leptos options to the app
|
// Pass Leptos options to the app
|
||||||
.app_data(web::Data::new(leptos_options.to_owned()))
|
.app_data(web::Data::new(leptos_options.to_owned()))
|
||||||
//.wrap(middleware::Compress::default())
|
//.wrap(middleware::Compress::default())
|
||||||
|
// Pass the database as shared state
|
||||||
|
.app_data(web::Data::new(db))
|
||||||
|
// Register API endpoints
|
||||||
|
.route("/api/items", web::get().to(get_items))
|
||||||
|
.route("/api/items", web::post().to(create_item))
|
||||||
})
|
})
|
||||||
.bind(&addr)?
|
.bind(&addr)?
|
||||||
.run()
|
.run()
|
||||||
|
|
Loading…
Add table
Reference in a new issue