From 8b8ccaf3f36ed77f510248e3a68e685a61d5e664 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Wed, 3 Apr 2024 22:28:51 -0500 Subject: [PATCH] Fix zaps database error --- src/db.rs | 6 +++--- src/lnurlp.rs | 5 +++-- src/models/zaps.rs | 23 ++++------------------- src/routes.rs | 6 +++--- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/db.rs b/src/db.rs index 6c55c02..3eddc1f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -7,7 +7,7 @@ use mockall::{automock, predicate::*}; use crate::models::{ app_user::{AppUser, NewAppUser}, invoice::{Invoice, NewInvoice}, - zaps::{NewZap, Zap}, + zaps::Zap, }; #[cfg_attr(test, automock)] @@ -23,7 +23,7 @@ pub(crate) trait DBConnection { fn get_user_by_name(&self, name: String) -> anyhow::Result>; fn get_user_by_id(&self, id: i32) -> anyhow::Result>; fn get_user_and_increment_counter(&self, name: &str) -> anyhow::Result>; - fn insert_new_zap(&self, new_zap: NewZap) -> anyhow::Result; + fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result; fn get_zap_by_id(&self, id: i32) -> anyhow::Result>; fn set_zap_event_id(&self, zap: Zap, event_id: String) -> anyhow::Result<()>; } @@ -91,7 +91,7 @@ impl DBConnection for PostgresConnection { invoice.set_state(conn, s) } - fn insert_new_zap(&self, new_zap: NewZap) -> anyhow::Result { + fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result { let conn = &mut self.db.get()?; new_zap.insert(conn) } diff --git a/src/lnurlp.rs b/src/lnurlp.rs index 94fd23f..c239fb9 100644 --- a/src/lnurlp.rs +++ b/src/lnurlp.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use crate::{ invoice::{spawn_invoice_subscription, InvoiceState}, mint::select_gateway, - models::{invoice::NewInvoice, zaps::NewZap}, + models::{invoice::NewInvoice, zaps::Zap}, routes::{LnurlCallbackParams, LnurlCallbackResponse, LnurlVerifyResponse}, State, }; @@ -137,7 +137,8 @@ pub async fn lnurl_callback( // save nostr zap request if let Some(request) = params.nostr { - let new_zap = NewZap { + let new_zap = Zap { + id: created_invoice.id, request, event_id: None, }; diff --git a/src/models/zaps.rs b/src/models/zaps.rs index b693327..37a5293 100644 --- a/src/models/zaps.rs +++ b/src/models/zaps.rs @@ -22,12 +22,12 @@ pub struct Zap { } impl Zap { - pub fn insert(&self, conn: &mut PgConnection) -> anyhow::Result<()> { - diesel::insert_into(zaps::table) + pub fn insert(&self, conn: &mut PgConnection) -> anyhow::Result { + let res = diesel::insert_into(zaps::table) .values(self) - .execute(conn)?; + .get_result(conn)?; - Ok(()) + Ok(res) } pub fn get_zaps(conn: &mut PgConnection) -> anyhow::Result> { @@ -51,18 +51,3 @@ impl Zap { } } -#[derive(Insertable)] -#[diesel(table_name = zaps)] -pub struct NewZap { - pub request: String, - pub event_id: Option, -} - -impl NewZap { - pub fn insert(&self, conn: &mut PgConnection) -> anyhow::Result { - diesel::insert_into(zaps::table) - .values(self) - .get_result::(conn) - .map_err(|e| e.into()) - } -} diff --git a/src/routes.rs b/src/routes.rs index 921ed80..f9903b1 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -104,13 +104,13 @@ pub async fn well_known_nip5_route( } } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)] #[serde(rename_all = "camelCase")] pub enum LnurlType { PayRequest, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)] #[serde(rename_all = "UPPERCASE")] pub enum LnurlStatus { Ok, @@ -144,7 +144,7 @@ pub async fn well_known_lnurlp_route( } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Default)] #[serde(rename_all = "camelCase")] pub struct LnurlCallbackParams { pub amount: u64, // User specified amount in MilliSatoshi