mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-23 23:55:01 +01:00
Add filtering for mint quote states in database (#586)
* filter for mint quote states in db --------- Co-authored-by: thesimplekid <tsk@thesimplekid.com> Co-authored-by: ok300 <106775972+ok300@users.noreply.github.com>
This commit is contained in:
@@ -49,6 +49,11 @@ pub trait Database {
|
|||||||
) -> Result<Option<MintMintQuote>, Self::Err>;
|
) -> Result<Option<MintMintQuote>, Self::Err>;
|
||||||
/// Get Mint Quotes
|
/// Get Mint Quotes
|
||||||
async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
|
async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
|
||||||
|
/// Get Mint Quotes with state
|
||||||
|
async fn get_mint_quotes_with_state(
|
||||||
|
&self,
|
||||||
|
state: MintQuoteState,
|
||||||
|
) -> Result<Vec<MintMintQuote>, Self::Err>;
|
||||||
/// Remove [`MintMintQuote`]
|
/// Remove [`MintMintQuote`]
|
||||||
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
|
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
|
||||||
|
|
||||||
|
|||||||
@@ -381,6 +381,28 @@ impl MintDatabase for MintRedbDatabase {
|
|||||||
Ok(quotes)
|
Ok(quotes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_mint_quotes_with_state(
|
||||||
|
&self,
|
||||||
|
state: MintQuoteState,
|
||||||
|
) -> Result<Vec<MintQuote>, Self::Err> {
|
||||||
|
let read_txn = self.db.begin_read().map_err(Error::from)?;
|
||||||
|
let table = read_txn
|
||||||
|
.open_table(MINT_QUOTES_TABLE)
|
||||||
|
.map_err(Error::from)?;
|
||||||
|
|
||||||
|
let mut quotes = Vec::new();
|
||||||
|
|
||||||
|
for (_id, quote) in (table.iter().map_err(Error::from)?).flatten() {
|
||||||
|
let quote: MintQuote = serde_json::from_str(quote.value()).map_err(Error::from)?;
|
||||||
|
|
||||||
|
if quote.state == state {
|
||||||
|
quotes.push(quote)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(quotes)
|
||||||
|
}
|
||||||
|
|
||||||
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
|
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
|
||||||
let write_txn = self.db.begin_write().map_err(Error::from)?;
|
let write_txn = self.db.begin_write().map_err(Error::from)?;
|
||||||
|
|
||||||
|
|||||||
@@ -431,6 +431,43 @@ FROM mint_quote
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_mint_quotes_with_state(
|
||||||
|
&self,
|
||||||
|
state: MintQuoteState,
|
||||||
|
) -> Result<Vec<MintQuote>, Self::Err> {
|
||||||
|
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
|
||||||
|
let rec = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT *
|
||||||
|
FROM mint_quote
|
||||||
|
WHERE state = ?
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.to_string())
|
||||||
|
.fetch_all(&mut transaction)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match rec {
|
||||||
|
Ok(rows) => {
|
||||||
|
transaction.commit().await.map_err(Error::from)?;
|
||||||
|
let mint_quotes = rows
|
||||||
|
.into_iter()
|
||||||
|
.map(sqlite_row_to_mint_quote)
|
||||||
|
.collect::<Result<Vec<MintQuote>, _>>()?;
|
||||||
|
|
||||||
|
Ok(mint_quotes)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("SQLite get mint quotes with state");
|
||||||
|
if let Err(err) = transaction.rollback().await {
|
||||||
|
tracing::error!("Could not rollback sql transaction: {}", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Err(Error::from(err).into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
|
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
|
||||||
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
|
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
|
||||||
|
|
||||||
|
|||||||
@@ -194,6 +194,21 @@ impl MintDatabase for MintMemoryDatabase {
|
|||||||
Ok(self.mint_quotes.read().await.values().cloned().collect())
|
Ok(self.mint_quotes.read().await.values().cloned().collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_mint_quotes_with_state(
|
||||||
|
&self,
|
||||||
|
state: MintQuoteState,
|
||||||
|
) -> Result<Vec<MintQuote>, Self::Err> {
|
||||||
|
let mint_quotes = self.mint_quotes.read().await;
|
||||||
|
|
||||||
|
let pending_quotes = mint_quotes
|
||||||
|
.values()
|
||||||
|
.filter(|q| q.state == state)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(pending_quotes)
|
||||||
|
}
|
||||||
|
|
||||||
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
|
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
|
||||||
self.mint_quotes.write().await.remove(quote_id);
|
self.mint_quotes.write().await.remove(quote_id);
|
||||||
|
|
||||||
|
|||||||
@@ -178,23 +178,23 @@ impl Mint {
|
|||||||
/// Get pending mint quotes
|
/// Get pending mint quotes
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub async fn get_pending_mint_quotes(&self) -> Result<Vec<MintQuote>, Error> {
|
pub async fn get_pending_mint_quotes(&self) -> Result<Vec<MintQuote>, Error> {
|
||||||
let mint_quotes = self.localstore.get_mint_quotes().await?;
|
let mint_quotes = self
|
||||||
|
.localstore
|
||||||
|
.get_mint_quotes_with_state(MintQuoteState::Pending)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(mint_quotes
|
Ok(mint_quotes)
|
||||||
.into_iter()
|
|
||||||
.filter(|p| p.state == MintQuoteState::Pending)
|
|
||||||
.collect())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get pending mint quotes
|
/// Get pending mint quotes
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub async fn get_unpaid_mint_quotes(&self) -> Result<Vec<MintQuote>, Error> {
|
pub async fn get_unpaid_mint_quotes(&self) -> Result<Vec<MintQuote>, Error> {
|
||||||
let mint_quotes = self.localstore.get_mint_quotes().await?;
|
let mint_quotes = self
|
||||||
|
.localstore
|
||||||
|
.get_mint_quotes_with_state(MintQuoteState::Unpaid)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(mint_quotes
|
Ok(mint_quotes)
|
||||||
.into_iter()
|
|
||||||
.filter(|p| p.state == MintQuoteState::Unpaid)
|
|
||||||
.collect())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove mint quote
|
/// Remove mint quote
|
||||||
|
|||||||
Reference in New Issue
Block a user