gnunet-svn
[Top][All Lists]
Advanced

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

[libeufin] branch master updated: move history and balance extraction in


From: gnunet
Subject: [libeufin] branch master updated: move history and balance extraction into helpers
Date: Wed, 11 Dec 2019 21:19:22 +0100

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

marcello pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new c818278  move history and balance extraction into helpers
c818278 is described below

commit c818278e7bdd3265d80568a79b8ac4fceb21bb35
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed Dec 11 21:18:52 2019 +0100

    move history and balance extraction into helpers
---
 .../tech/libeufin/sandbox/EbicsProtocolBackend.kt  |  1 +
 .../src/main/kotlin/tech/libeufin/sandbox/JSON.kt  |  4 +-
 .../src/main/kotlin/tech/libeufin/sandbox/Main.kt  | 74 ++++++++++++----------
 3 files changed, 45 insertions(+), 34 deletions(-)

diff --git 
a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
index 34950eb..46eae7b 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
@@ -471,6 +471,7 @@ suspend fun ApplicationCall.ebicsweb() {
             when (header.static.orderDetails.orderType) {
                 "INI" -> handleEbicsIni(header, orderData)
                 "HIA" -> handleEbicsHia(header, orderData)
+                // "C52" -> handleEbicsC52(header, orderData)
                 else -> throw EbicsInvalidXmlError()
             }
         }
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
index d006b92..bde1449 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
@@ -55,8 +55,8 @@ data class CustomerEbicsInfo(
 )
 
 data class CustomerHistoryRequest(
-    val start: String,
-    val end: String
+    val start: String?,
+    val end: String?
 )
 
 data class CustomerHistoryResponseElement(
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
index 2934cbd..c101792 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -170,6 +170,34 @@ fun sampleData() {
 
 }
 
+fun extractHistoryForEach(id: Int, start: String?, end: String?, builder: 
(BankTransactionEntity) -> Any) {
+    val s = if (start != null) DateTime.parse(start) else DateTime(0)
+    val e = if (end != null) DateTime.parse(end) else DateTime.now()
+
+    transaction {
+        BankTransactionEntity.find {
+            BankTransactionsTable.localCustomer eq id and
+                    BankTransactionsTable.date.between(s, e)
+        }.forEach {
+            builder(it)
+        }
+    }
+}
+
+fun calculateBalance(id: Int, start: String?, end: String?): BigDecimal {
+    val s = if (start != null) DateTime.parse(start) else DateTime(0)
+    val e = if (end != null) DateTime.parse(end) else DateTime.now()
+
+    var ret = BigDecimal(0)
+
+    transaction {
+        BankTransactionEntity.find {
+            BankTransactionsTable.localCustomer eq id and 
BankTransactionsTable.date.between(s, e)
+        }.forEach { ret += it.amount }
+    }
+    return ret
+}
+
 val LOGGER: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox")
 
 fun main() {
@@ -207,10 +235,8 @@ fun main() {
             }
         }
         routing {
-            
-            post("/{id}/history") {
 
-                LOGGER.debug("/history fired up")
+            post("/{id}/history") {
 
                 val req = call.receive<CustomerHistoryRequest>()
                 val startDate = DateTime.parse(req.start)
@@ -218,47 +244,31 @@ fun main() {
 
                 LOGGER.debug("Fetching history from ${startDate.toString()}, 
to ${endDate.toString()}")
 
+                val customer = findCustomer(call.parameters["id"])
                 val ret = CustomerHistoryResponse()
 
-                transaction {
-                    val customer = findCustomer(call.parameters["id"])
-
-                    BankTransactionEntity.find {
-                        BankTransactionsTable.localCustomer eq customer.id and
-                                BankTransactionsTable.date.between(startDate, 
endDate)
-
-                    }.forEach {
-                        ret.history.add(
-                            CustomerHistoryResponseElement(
-                                subject = it.subject,
-                                amount = 
"${it.amount.signToString()}${it.amount.toString()} EUR",
-                                counterpart = it.counterpart,
-                                date = it.date.toString("Y-M-d")
-                            )
+                extractHistoryForEach(customer.id.value, req.start, req.end) {
+                    ret.history.add(
+                        CustomerHistoryResponseElement(
+                            subject = it.subject,
+                            amount = "${it.amount.signToString()}${it.amount} 
EUR",
+                            counterpart = it.counterpart,
+                            date = it.date.toString("Y-M-d")
                         )
-                    }
+                    )
                 }
-
                 call.respond(ret)
                 return@post
             }
 
             get("/{id}/balance") {
-                val (name, balance) = transaction {
-                    val tmp: BankCustomerEntity = 
findCustomer(call.parameters["id"])
-
-                    var ret = Amount(0)
-                    BankTransactionEntity.find {
-                        BankTransactionsTable.localCustomer eq tmp.id
-                    }.forEach {
-                        ret += it.amount
-                    }
-                    Pair(tmp.name, ret)
-                }
+
+                val customer = findCustomer(call.parameters["id"])
+                val balance = calculateBalance(customer.id.value, null, null)
 
                 call.respond(
                     CustomerBalance(
-                    name = name,
+                    name = customer.name,
                     balance = "${balance} EUR"
                     )
                 )

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

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