From 038e0ad7ebcc15c9c11ab3c23d301f2848a08fff Mon Sep 17 00:00:00 2001 From: vazw Date: Sun, 26 May 2024 13:51:33 +0700 Subject: [PATCH] fix database connection not can't write --- src/api.rs | 9 +-- src/app/core_api/api.rs | 172 ++++++++++++++++++++++++---------------- 2 files changed, 107 insertions(+), 74 deletions(-) diff --git a/src/api.rs b/src/api.rs index bf4ad75..09dfaa9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -28,8 +28,7 @@ pub async fn get_username( db: web::Data>, name: &str, ) -> std::io::Result> { - let lowercase_name = name.to_lowercase(); - let query = format!("SELECT * FROM users WHERE name='{lowercase_name}'"); + let query = format!("SELECT * FROM users WHERE name='{name}'"); match sqlx::query_as::<_, UsersData>(&query) .fetch_one(&**db) .await @@ -45,7 +44,7 @@ pub async fn verify(db: web::Data>, payload: web::Query) -> i match user { Some(user) => { let user_respon = format!("{{\"{}\":\"{}\"}}", user.name, user.pubkey); - let relay_respon = format!("{{\"{}\":{}}}", user.pubkey, "[\"wss://relay.siamstr.com\", \"wss://relay.notoshi.win\", \"wss://bostr.lecturify.net\"]"); + let relay_respon = format!("{{\"{}\":{}}}", user.pubkey, "[\"wss://relay.siamstr.com\", \"wss://relay.notoshi.win\", \"wss://bostr.lecturify.net/?accurate=1\"]"); let nip46 = format!("{{\"{}\":{}}}", user.pubkey, "[\"wss://sign.siamstr.com\"]"); let respon: NostrUser = NostrUser { names: serde_json::from_str(&user_respon).unwrap(), @@ -95,10 +94,10 @@ pub async fn lnurl(db: web::Data>, payload: web::Path) -> i } } } else { - HttpResponse::NotFound().json( + return HttpResponse::NotFound().json( serde_json::from_str::("{{\"status\":400,\"message\":\"Error\"}") .unwrap(), - ) + ); } } None => HttpResponse::NotFound() diff --git a/src/app/core_api/api.rs b/src/app/core_api/api.rs index 836af9c..e207a9c 100644 --- a/src/app/core_api/api.rs +++ b/src/app/core_api/api.rs @@ -6,15 +6,13 @@ use nostr_sdk::prelude::*; #[cfg(feature = "ssr")] use chrono::Local; #[cfg(feature = "ssr")] +use sqlx::{Pool, Sqlite}; +#[cfg(feature = "ssr")] use uuid::Uuid; #[cfg(feature = "ssr")] pub const DB_URL: &str = "sqlite://db/database.db"; #[cfg(feature = "ssr")] use sqlx::{Connection, FromRow, SqliteConnection, Row}; -#[cfg(feature = "ssr")] -pub async fn db() -> Result { - Ok(SqliteConnection::connect(DB_URL).await?) -} #[cfg(feature = "ssr")] #[derive(Debug, Clone, FromRow, Serialize, Deserialize, PartialEq, Eq)] @@ -58,17 +56,23 @@ pub struct BoolRespons { #[server] pub async fn count_users(_count: i32) -> Result { - let mut con = db().await.unwrap(); - let query = "SELECT COUNT(*) FROM users"; - let result = sqlx::query(query) - .fetch_one(&mut con) - .await; - match result { - Ok(user) => { - let num :i32=user.get(0); - Ok(CountsRespon { count: num }) + if leptos::leptos_dom::is_server() { + use actix_web::web::Data; + use leptos_actix::*; + let con: Data> = extract().await?; + let query = "SELECT COUNT(*) FROM users"; + let result = sqlx::query(query) + .fetch_one(&**con) + .await; + match result { + Ok(user) => { + let num :i32=user.get(0); + Ok(CountsRespon { count: num }) + } + Err(_) => Ok(CountsRespon { count: 0 }), } - Err(_) => Ok(CountsRespon { count: 0 }), + } else { + Ok(CountsRespon { count: 0 }) } } @@ -82,40 +86,52 @@ pub async fn check_npub(public_key: String) -> Result(&query) - .fetch_one(&mut con) - .await; - match result { - Ok(user) => Ok(UserRespons { - user: Some(UsersDataResult { - id: user.id, - name: user.name, - pubkey: user.pubkey, - lightning_url: user.lightning_url, - created: user.created, + if leptos::leptos_dom::is_server() { + use actix_web::web::Data; + use leptos_actix::*; + let con: Data> = extract().await?; + let query = format!("SELECT * FROM users WHERE pubkey='{hex_npub}'"); + let result = sqlx::query_as::<_, UsersData>(&query) + .fetch_one(&**con) + .await; + match result { + Ok(user) => Ok(UserRespons { + user: Some(UsersDataResult { + id: user.id, + name: user.name, + pubkey: user.pubkey, + lightning_url: user.lightning_url, + created: user.created, + }), }), - }), - Err(_) => Ok(UserRespons { user: None }), + Err(_) => Ok(UserRespons { user: None }), + } + } else { + Ok(UserRespons { user: None }) } } #[server] pub async fn check_username(username: String) -> Result { - let mut con = db().await.unwrap(); - if username.is_empty() { - Ok(BoolRespons { status: 0 }) - } else { - let username = username.to_lowercase(); - let query = format!("SELECT * FROM users WHERE name='{username}'"); - let result = sqlx::query_as::<_, UsersData>(&query) - .fetch_one(&mut con) - .await; - match result { - Ok(_user) => Ok(BoolRespons { status: 1 }), - Err(_) => Ok(BoolRespons { status: 0 }), + if leptos::leptos_dom::is_server() { + use actix_web::web::Data; + use leptos_actix::*; + if username.is_empty() { + Ok(BoolRespons { status: 0 }) + } else { + let con: Data> = extract().await?; + let username = username.to_lowercase(); + let query = format!("SELECT * FROM users WHERE name='{username}'"); + let result = sqlx::query_as::<_, UsersData>(&query) + .fetch_one(&**con) + .await; + match result { + Ok(_user) => Ok(BoolRespons { status: 1 }), + Err(_) => Ok(BoolRespons { status: 0 }), + } } + } else { + Ok(BoolRespons { status: 0 }) } } @@ -128,18 +144,24 @@ pub async fn add_user( let id = Uuid::new_v4().to_string(); let time_now = Local::now().to_rfc3339(); let lowercase_name = username.to_lowercase(); - let mut con = db().await.unwrap(); - match sqlx::query("INSERT INTO users (id,name,pubkey,lightning_url,created) VALUES (?,?,?,?,?)") - .bind(id) - .bind(lowercase_name) - .bind(pubkey) - .bind(lnurl) - .bind(time_now) - .execute(&mut con) - .await - { - Ok(_user) => Ok(BoolRespons { status: 1 }), - Err(_) => Ok(BoolRespons { status: 0 }), + if leptos::leptos_dom::is_server() { + use actix_web::web::Data; + use leptos_actix::*; + let con: Data> = extract().await?; + match sqlx::query("INSERT INTO users (id,name,pubkey,lightning_url,created) VALUES (?,?,?,?,?)") + .bind(id) + .bind(lowercase_name) + .bind(pubkey) + .bind(lnurl) + .bind(time_now) + .execute(&**con) + .await + { + Ok(_user) => Ok(BoolRespons { status: 1 }), + Err(_) => Ok(BoolRespons { status: 0 }), + } + } else { + Ok(BoolRespons { status: 0 }) } } @@ -152,14 +174,20 @@ pub async fn edit_user( ) -> Result { let events : Event = Event::from_json(events).unwrap(); if events.verify().is_ok() && events.pubkey.to_string() == pubkey { - let mut con = db().await.unwrap(); - let username = username.to_lowercase(); - let query = format!( - "UPDATE users SET name='{username}', lightning_url='{lnurl}' WHERE pubkey='{pubkey}'" - ); - match sqlx::query(&query).execute(&mut con).await { - Ok(_user) => Ok(BoolRespons { status: 1 }), - Err(_) => Ok(BoolRespons { status: 0 }), + if leptos::leptos_dom::is_server() { + use actix_web::web::Data; + use leptos_actix::*; + let con: Data> = extract().await?; + let username = username.to_lowercase(); + let query = format!( + "UPDATE users SET name='{username}', lightning_url='{lnurl}' WHERE pubkey='{pubkey}'" + ); + match sqlx::query(&query).execute(&**con).await { + Ok(_user) => Ok(BoolRespons { status: 1 }), + Err(_) => Ok(BoolRespons { status: 0 }), + } + } else { + Ok(BoolRespons { status: 0 }) } } else { Ok(BoolRespons { status: 0 }) @@ -171,14 +199,20 @@ pub async fn edit_user( pub async fn delete_user(pubkey: String, events: String) -> Result { let events : Event = Event::from_json(events).unwrap(); if events.pubkey.to_string() == pubkey && events.verify().is_ok() { - let mut con = db().await.unwrap(); - match sqlx::query("DELETE FROM users WHERE pubkey=(?)") - .bind(pubkey) - .execute(&mut con) - .await - { - Ok(_user) => Ok(BoolRespons { status: 1 }), - Err(_) => Ok(BoolRespons { status: 0 }), + if leptos::leptos_dom::is_server() { + use actix_web::web::Data; + use leptos_actix::*; + let con: Data> = extract().await?; + match sqlx::query("DELETE FROM users WHERE pubkey=(?)") + .bind(pubkey) + .execute(&**con) + .await + { + Ok(_user) => Ok(BoolRespons { status: 1 }), + Err(_) => Ok(BoolRespons { status: 0 }), + } + } else { + Ok(BoolRespons { status: 0 }) } } else { Ok(BoolRespons { status: 0 })