commit a8bde648bb82fa3c00f3197eedde84cf54331185
parent 20d73047aa86e690037d486e24dff5e44a7cfef9
Author: Florian Dold <florian.dold@gmail.com>
Date: Fri, 27 Sep 2019 19:47:03 +0200
refactoring
Diffstat:
2 files changed, 36 insertions(+), 38 deletions(-)
diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
@@ -33,8 +33,6 @@ import org.w3c.dom.Document
import tech.libeufin.messages.HEVResponseDataType
import javax.xml.bind.JAXBElement
import io.ktor.features.*
-import io.netty.handler.codec.http.HttpContent
-import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import tech.libeufin.tech.libeufin.*
import java.lang.NumberFormatException
@@ -63,22 +61,8 @@ fun main() {
}
post("/admin/customers") {
-
- var returnId = 0
- try {
-
- val body = call.receive<CustomerRequest>()
- logger.info(body.toString())
-
- transaction {
- val newBankCustomer = BankCustomers.insertAndGetId {
- it[name] = body.name
- it[ebicsSubscriber] = createSubscriber().id
- }
-
- returnId = newBankCustomer.value
- }
-
+ val body = try {
+ call.receive<CustomerRequest>()
} catch (e: Exception) {
e.printStackTrace()
call.respond(
@@ -87,6 +71,26 @@ fun main() {
)
return@post
}
+ logger.info(body.toString())
+
+ val returnId = transaction {
+ val myUserId = EbicsUser.new { }
+ val myPartnerId = EbicsPartner.new { }
+ val mySystemId = EbicsSystem.new { }
+ val subscriber = EbicsSubscriber.new {
+ userId = myUserId
+ partnerId = myPartnerId
+ systemId = mySystemId
+ state = SubscriberStates.NEW
+ }
+ println("subscriber ID: ${subscriber.id.value}")
+ val customer = BankCustomer.new {
+ name = body.name
+ ebicsSubscriber = subscriber
+ }
+ println("name: ${customer.name}")
+ return@transaction customer.id.value
+ }
call.respond(
HttpStatusCode.OK,
@@ -98,32 +102,20 @@ fun main() {
get("/admin/customers/{id}") {
- var id = -1
- var tmp: CustomerInfo? = null
- var result: ResultRow? = null
-
- try {
- id = call.parameters["id"]!!.toInt()
- logger.info("Querying ID: $id")
+ val id: Int = try {
+ call.parameters["id"]!!.toInt()
} catch (e: NumberFormatException) {
call.respond(
HttpStatusCode.BadRequest,
SandboxError(e.message.toString())
)
+ return@get
}
- transaction {
- val singleton = BankCustomers.select { BankCustomers.id eq id }
- result = singleton.firstOrNull()
-
- if (null != result)
- tmp = CustomerInfo(
- result?.get(BankCustomers.name) as String,
- customerEbicsInfo = CustomerEbicsInfo(
- result?.get(BankCustomers.ebicsSubscriber)?.value as Int
- )
- )
+ logger.info("Querying ID: $id")
+ val result = transaction {
+ BankCustomer.findById(id)
}
if (null == result) {
@@ -134,9 +126,16 @@ fun main() {
return@get
}
+ val tmp = CustomerInfo(
+ result.name,
+ customerEbicsInfo = CustomerEbicsInfo(
+ result.ebicsSubscriber.userId.id.value
+ )
+ )
+
call.respond(
HttpStatusCode.OK,
- tmp as CustomerInfo
+ tmp
)
}
diff --git a/src/main/kotlin/tech/libeufin/DB.kt b/src/main/kotlin/tech/libeufin/DB.kt
@@ -1,6 +1,5 @@
package tech.libeufin.tech.libeufin
-import com.sun.org.apache.bcel.internal.generic.NEW
import org.jetbrains.exposed.dao.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction