gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] 01/07: Helpers.


From: gnunet
Subject: [libeufin] 01/07: Helpers.
Date: Wed, 12 Apr 2023 11:28:19 +0200

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

commit 090bc0e537d120e0996aeefc9fa82c1929bd676c
Author: MS <ms@taler.net>
AuthorDate: Tue Apr 11 13:16:43 2023 +0200

    Helpers.
    
    Adding helper to extract the transactions history in Sandbox'
    Access API.
---
 .../src/main/kotlin/tech/libeufin/sandbox/Main.kt  | 41 +++++---------------
 .../kotlin/tech/libeufin/sandbox/bankAccount.kt    | 44 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 32 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
index 31856c09..856cbbeb 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -1530,39 +1530,16 @@ val sandboxApp: Application.() -> Unit = {
                     if (fromMs < 0) throw badRequest("'from_ms' param is less 
than 0")
                     val untilMs = 
expectLong(call.request.queryParameters["until_ms"] ?: 
Long.MAX_VALUE.toString())
                     if (untilMs < 0) throw badRequest("'until_ms' param is 
less than 0")
-                    val ret = mutableListOf<XLibeufinBankTransaction>()
-                    /**
-                     * Case where page number wasn't given,
-                     * therefore the results starts from the last transaction.
-                     */
-                    transaction {
-                        /**
-                         * Get a history page - from the calling bank account 
- having
-                         * 'firstElementId' as the latest transaction in it.  
*/
-                        fun getPage(firstElementId: Long): 
Iterable<BankAccountTransactionEntity> {
-                            logger.debug("Trying to build pageBuf from ID: 
$firstElementId," +
-                                    " including $size txs in the past."
+                    val ret: List<XLibeufinBankTransaction> = transaction {
+                        extractTxHistory(
+                            HistoryParams(
+                                pageNumber = page,
+                                pageSize = size,
+                                bankAccount = bankAccount,
+                                fromMs = fromMs,
+                                untilMs = untilMs
                             )
-                            return BankAccountTransactionEntity.find {
-                                (BankAccountTransactionsTable.id lessEq 
firstElementId) and
-                                        (BankAccountTransactionsTable.account 
eq bankAccount.id) and
-                                        
(BankAccountTransactionsTable.date.between(fromMs, untilMs))
-                            }.sortedByDescending { it.id.value }.take(size)
-                        }
-                        val lt: BankAccountTransactionEntity? = 
bankAccount.lastTransaction
-                        if (lt == null) return@transaction
-                        var nextPageIdUpperLimit: Long = lt.id.value
-                        // This loop fetches (and discards) pages until the 
desired one is found.
-                        for (i in 1..(page)) {
-                            val pageBuf = getPage(nextPageIdUpperLimit)
-                            logger.debug("pageBuf #$i follows.  Request wants 
#$page:")
-                            pageBuf.forEach { logger.debug("ID: ${it.id}, 
subject: ${it.subject}, amount: ${it.currency}:${it.amount}") }
-                            if (pageBuf.none()) return@transaction
-                            nextPageIdUpperLimit = pageBuf.last().id.value - 1
-                            if (i == page) pageBuf.forEach {
-                                
ret.add(getHistoryElementFromTransactionRow(it))
-                            }
-                        }
+                        )
                     }
                     call.respond(object {val transactions = ret})
                     return@get
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
index e6559897..b9344405 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
@@ -226,4 +226,48 @@ fun wireTransfer(
         }
     }
     return transactionRef
+}
+
+/**
+ * Helper that constructs a transactions history page
+ * according to the URI parameters passed to Access API's
+ * GET /transactions.
+ */
+data class HistoryParams(
+    val pageNumber: Int,
+    val pageSize: Int,
+    val fromMs: Long,
+    val untilMs: Long,
+    val bankAccount: BankAccountEntity
+)
+fun extractTxHistory(params: HistoryParams): List<XLibeufinBankTransaction> {
+    val ret = mutableListOf<XLibeufinBankTransaction>()
+    /**
+     * Helper that gets transactions earlier than the 'firstElementId'
+     * transaction AND that match the URI parameters.
+     */
+    fun getPage(firstElementId: Long): Iterable<BankAccountTransactionEntity> {
+        return BankAccountTransactionEntity.find {
+            (BankAccountTransactionsTable.id lessEq firstElementId) and
+                    (BankAccountTransactionsTable.account eq 
params.bankAccount.id) and
+                    (BankAccountTransactionsTable.date.between(params.fromMs, 
params.untilMs))
+        }.sortedByDescending { it.id.value }.take(params.pageSize)
+    }
+    // Gets a pointer to the last transaction of this bank account.
+    val lastTransaction: BankAccountTransactionEntity? = 
params.bankAccount.lastTransaction
+    if (lastTransaction == null) return ret
+    var nextPageIdUpperLimit: Long = lastTransaction.id.value
+
+    // This loop fetches (and discards) pages until the desired one is found.
+    for (i in 1..(params.pageNumber)) {
+        val pageBuf = getPage(nextPageIdUpperLimit)
+        logger.debug("pageBuf #$i follows.  Request wants 
#${params.pageNumber}:")
+        pageBuf.forEach { logger.debug("ID: ${it.id}, subject: ${it.subject}, 
amount: ${it.currency}:${it.amount}") }
+        if (pageBuf.none()) return ret
+        nextPageIdUpperLimit = pageBuf.last().id.value - 1
+        if (i == params.pageNumber) pageBuf.forEach {
+            ret.add(getHistoryElementFromTransactionRow(it))
+        }
+    }
+    return ret
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]