Skip to content

Commit

Permalink
Fix zaps database error
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Apr 4, 2024
1 parent 9eb8e46 commit 8b8ccaf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -23,7 +23,7 @@ pub(crate) trait DBConnection {
fn get_user_by_name(&self, name: String) -> anyhow::Result<Option<AppUser>>;
fn get_user_by_id(&self, id: i32) -> anyhow::Result<Option<AppUser>>;
fn get_user_and_increment_counter(&self, name: &str) -> anyhow::Result<Option<AppUser>>;
fn insert_new_zap(&self, new_zap: NewZap) -> anyhow::Result<Zap>;
fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result<Zap>;
fn get_zap_by_id(&self, id: i32) -> anyhow::Result<Option<Zap>>;
fn set_zap_event_id(&self, zap: Zap, event_id: String) -> anyhow::Result<()>;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl DBConnection for PostgresConnection {
invoice.set_state(conn, s)
}

fn insert_new_zap(&self, new_zap: NewZap) -> anyhow::Result<Zap> {
fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result<Zap> {
let conn = &mut self.db.get()?;
new_zap.insert(conn)
}
Expand Down
5 changes: 3 additions & 2 deletions src/lnurlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down
23 changes: 4 additions & 19 deletions src/models/zaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Zap> {
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<Vec<Zap>> {
Expand All @@ -51,18 +51,3 @@ impl Zap {
}
}

#[derive(Insertable)]
#[diesel(table_name = zaps)]
pub struct NewZap {
pub request: String,
pub event_id: Option<String>,
}

impl NewZap {
pub fn insert(&self, conn: &mut PgConnection) -> anyhow::Result<Zap> {
diesel::insert_into(zaps::table)
.values(self)
.get_result::<Zap>(conn)
.map_err(|e| e.into())
}
}
6 changes: 3 additions & 3 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8b8ccaf

Please sign in to comment.