Refactored code be more readable
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package net.ipksindia.dao
|
||||
|
||||
import model.TransactionRequest
|
||||
import net.ipksindia.ItemNotFoundException
|
||||
import java.sql.Date
|
||||
import java.sql.ResultSet
|
||||
import java.sql.SQLException
|
||||
|
||||
class TransactionDao() {
|
||||
class TransactionDao {
|
||||
|
||||
private val singleTransactionRequestQuery = """
|
||||
SELECT
|
||||
@@ -63,74 +65,73 @@ class TransactionDao() {
|
||||
""".trimIndent()
|
||||
|
||||
fun updateSuccessTransaction(request: TransactionRequest, transferQueueNumber: String, neftQueueNumber: String) {
|
||||
DatabaseFactory.instance.getConnection()
|
||||
.use { connection ->
|
||||
connection
|
||||
.prepareStatement(transactionUpdateQuery)
|
||||
.also {
|
||||
it.setString(1, request.transactionNumber)
|
||||
it.setString(2, request.pacsCurrentAccountNumber)
|
||||
it.setString(3, request.neftBeneficiaryAccountNumber)
|
||||
it.setString(4, request.ifscCode)
|
||||
it.setString(5, request.amount)
|
||||
it.setDate(6, Date.valueOf(request.date))
|
||||
it.setString(7, request.tellerId)
|
||||
it.setString(8, "PROCESSED")
|
||||
it.setString(9, request.beneficiaryName)
|
||||
it.setString(10, request.beneficiaryAddress)
|
||||
it.setString(11, request.pacsId)
|
||||
it.setString(12, request.commissionTransactionNumber)
|
||||
it.setString(13, request.commissionAmount)
|
||||
it.setString(14, request.dccbCode)
|
||||
it.setString(15, request.branchCode)
|
||||
it.setString(16, request.remitterName)
|
||||
it.setString(17, transferQueueNumber)
|
||||
it.setString(18, request.pacsAccountNumber)
|
||||
it.setString(19, neftQueueNumber)
|
||||
}
|
||||
.use { it.executeUpdate() }
|
||||
try {
|
||||
DatabaseFactory.instance.getConnection().use { connection ->
|
||||
connection.prepareStatement(transactionUpdateQuery).also {
|
||||
it.setString(1, request.transactionNumber)
|
||||
it.setString(2, request.pacsCurrentAccountNumber)
|
||||
it.setString(3, request.neftBeneficiaryAccountNumber)
|
||||
it.setString(4, request.ifscCode)
|
||||
it.setString(5, request.amount)
|
||||
it.setDate(6, Date.valueOf(request.date))
|
||||
it.setString(7, request.tellerId)
|
||||
it.setString(8, "PROCESSED")
|
||||
it.setString(9, request.beneficiaryName)
|
||||
it.setString(10, request.beneficiaryAddress)
|
||||
it.setString(11, request.pacsId)
|
||||
it.setString(12, request.commissionTransactionNumber)
|
||||
it.setString(13, request.commissionAmount)
|
||||
it.setString(14, request.dccbCode)
|
||||
it.setString(15, request.branchCode)
|
||||
it.setString(16, request.remitterName)
|
||||
it.setString(17, transferQueueNumber)
|
||||
it.setString(18, request.pacsAccountNumber)
|
||||
it.setString(19, neftQueueNumber)
|
||||
}.use { it.executeUpdate() }
|
||||
}
|
||||
}
|
||||
|
||||
fun getTransactionRequest(transactionNumber: String): TransactionRequest? {
|
||||
return DatabaseFactory.instance
|
||||
.getConnection()
|
||||
.use { connection ->
|
||||
connection
|
||||
.prepareStatement(singleTransactionRequestQuery)
|
||||
.apply { setString(1, transactionNumber) }
|
||||
.executeQuery()
|
||||
.use { mapToObject(it).firstOrNull() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun mapToObject(rs: ResultSet): List<TransactionRequest> {
|
||||
val list = mutableListOf<TransactionRequest>()
|
||||
while (rs.next()) {
|
||||
val transactionRequest = TransactionRequest(
|
||||
transactionNumber = rs.getString("txn_no"),
|
||||
pacsCurrentAccountNumber = rs.getString("src_ac_no"),
|
||||
neftBeneficiaryAccountNumber = rs.getString("dest_ac_no"),
|
||||
ifscCode = rs.getString("ifsc_code"),
|
||||
amount = rs.getString("txn_amt"),
|
||||
date = rs.getDate("txn_date").toLocalDate(),
|
||||
tellerId = rs.getString("teller_id"),
|
||||
status = rs.getString("status"),
|
||||
beneficiaryName = rs.getString("beneficiary_name"),
|
||||
beneficiaryAddress = rs.getString("beneficiary_add"),
|
||||
pacsId = rs.getString("pacs_id"),
|
||||
commissionTransactionNumber = rs.getString("comm_txn_no"),
|
||||
commissionAmount = rs.getString("comm_txn_amt"),
|
||||
dccbCode = rs.getString("dccb_code"),
|
||||
branchCode = rs.getString("br_Code"),
|
||||
remitterName = rs.getString("remitter_name"),
|
||||
pacsAccountNumber = rs.getString("pacs_acc_no"),
|
||||
linkedCBSAccountNumber = rs.getString("cbs_sb_acc_no"),
|
||||
)
|
||||
list.add(transactionRequest)
|
||||
}catch (e: ExceptionInInitializerError) {
|
||||
throw SQLException("Failed to connect to the database")
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
}
|
||||
fun getTransactionRequest(transactionNumber: String): TransactionRequest {
|
||||
return try {
|
||||
DatabaseFactory.instance.getConnection().use { connection ->
|
||||
connection.prepareStatement(singleTransactionRequestQuery).apply { setString(1, transactionNumber) }
|
||||
.executeQuery()
|
||||
.use { mapToObject(it) ?: throw ItemNotFoundException("Transaction Number", transactionNumber) }
|
||||
}
|
||||
} catch (e: ExceptionInInitializerError) {
|
||||
throw SQLException("Failed to connect to the database")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun mapToObject(rs: ResultSet): TransactionRequest? {
|
||||
|
||||
if (rs.next()) {
|
||||
return TransactionRequest(
|
||||
transactionNumber = rs.getString("txn_no"),
|
||||
pacsCurrentAccountNumber = rs.getString("src_ac_no"),
|
||||
neftBeneficiaryAccountNumber = rs.getString("dest_ac_no"),
|
||||
ifscCode = rs.getString("ifsc_code"),
|
||||
amount = rs.getString("txn_amt"),
|
||||
date = rs.getDate("txn_date").toLocalDate(),
|
||||
tellerId = rs.getString("teller_id"),
|
||||
status = rs.getString("status"),
|
||||
beneficiaryName = rs.getString("beneficiary_name"),
|
||||
beneficiaryAddress = rs.getString("beneficiary_add"),
|
||||
pacsId = rs.getString("pacs_id"),
|
||||
commissionTransactionNumber = rs.getString("comm_txn_no"),
|
||||
commissionAmount = rs.getString("comm_txn_amt"),
|
||||
dccbCode = rs.getString("dccb_code"),
|
||||
branchCode = rs.getString("br_Code"),
|
||||
remitterName = rs.getString("remitter_name"),
|
||||
pacsAccountNumber = rs.getString("pacs_acc_no"),
|
||||
linkedCBSAccountNumber = rs.getString("cbs_sb_acc_no"),
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user