Skip to content

Commit

Permalink
Search method for the transaction repository
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 26, 2023
1 parent ca8d8e0 commit 0e7371d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@ package me.madhead.tyzenhaus.repository.postgresql.transaction
import java.sql.ResultSet
import me.madhead.tyzenhaus.entity.transaction.Transaction

internal fun ResultSet.toTransaction(): Transaction? {
return if (this.next()) {
Transaction(
id = this.getLong("id"),
groupId = this.getLong("group_id"),
payer = this.getLong("payer"),
recipients = run {
val value = this.getArray("recipients")

if (this.wasNull()) {
emptySet()
} else {
(value.array as? Array<*>)?.filterIsInstance<Long>()?.toSet() ?: emptySet()
}
},
amount = this.getBigDecimal("amount"),
currency = this.getString("currency"),
title = run {
val value = this.getString("title")

if (this.wasNull()) {
null
} else {
value
}
},
timestamp = this.getTimestamp("timestamp").toInstant(),
)
} else {
null
internal fun ResultSet.toTransaction(): Transaction =
Transaction(
id = this.getLong("id"),
groupId = this.getLong("group_id"),
payer = this.getLong("payer"),
recipients = run {
val value = this.getArray("recipients")

if (this.wasNull()) {
emptySet()
} else {
(value.array as? Array<*>)?.filterIsInstance<Long>()?.toSet() ?: emptySet()
}
},
amount = this.getBigDecimal("amount"),
currency = this.getString("currency"),
title = run {
val value = this.getString("title")

if (this.wasNull()) {
null
} else {
value
}
},
timestamp = this.getTimestamp("timestamp").toInstant(),
)

internal fun ResultSet.toTransactions(): List<Transaction> = buildList {
while (this@toTransactions.next()) {
this.add(this@toTransactions.toTransaction())
}
}
}.toList()

internal fun ResultSet.toCurrencies(): List<String> {
val result = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ class TransactionRepository(dataSource: DataSource)
.use { preparedStatement ->
preparedStatement.setLong(@Suppress("MagicNumber") 1, id)
preparedStatement.executeQuery().use { resultSet ->
return@get resultSet.toTransaction()
if (resultSet.next()) {
return@get resultSet.toTransaction()
} else {
return@get null
}
}
}
}
Expand Down Expand Up @@ -101,4 +105,19 @@ class TransactionRepository(dataSource: DataSource)
}
}
}

override fun search(groupId: Long): List<Transaction> {
logger.debug("search {}", groupId)

dataSource.connection.use { connection ->
connection
.prepareStatement("""SELECT * FROM "transaction" WHERE "group_id" = ? ORDER BY "timestamp" DESC;""")
.use { preparedStatement ->
preparedStatement.setLong(@Suppress("MagicNumber") 1, groupId)
preparedStatement.executeQuery().use { resultSet ->
return@search resultSet.toTransactions()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.time.Instant
import me.madhead.tyzenhaus.entity.transaction.Transaction
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -82,4 +83,14 @@ class TransactionRepositoryTest {
transactionRepository.groupCurrencies(1)
)
}

@Test
fun search() {
assertTrue(
transactionRepository.search(1).isNotEmpty()
)
assertTrue(
transactionRepository.search(6).size >= 3
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ interface TransactionRepository : Repository<Long, Transaction> {
* Retrieves a list of unique currencies used in transactions of the specified group.
*/
fun groupCurrencies(groupId: Long): List<String>

/**
* Retrieve transactions matching criteria.
*/
fun search(groupId: Long): List<Transaction>
}

0 comments on commit 0e7371d

Please sign in to comment.