diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 0878bd8e..9984bbff 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -72,6 +72,7 @@ jobs: env: DATABASE_URL: postgres://${{env.POSTGRES_USER}}:${{env.POSTGRES_PASSWORD}}@${{env.POSTGRES_HOST}}:${{env.POSTGRES_PORT}}/${{env.POSTGRES_DB}} - run: psql -h ${{env.POSTGRES_HOST}} -U ${{env.POSTGRES_USER}} -d ${{env.POSTGRES_DB}} )?.filterIsInstance()?.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()?.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 = buildList { + while (this@toTransactions.next()) { + this.add(this@toTransactions.toTransaction()) } -} +}.toList() internal fun ResultSet.toCurrencies(): List { val result = mutableListOf() diff --git a/repository/postgresql/src/main/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepository.kt b/repository/postgresql/src/main/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepository.kt index 87e9629d..0602d171 100644 --- a/repository/postgresql/src/main/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepository.kt +++ b/repository/postgresql/src/main/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepository.kt @@ -15,6 +15,7 @@ class TransactionRepository(dataSource: DataSource) private val logger = LogManager.getLogger(TransactionRepository::class.java)!! } + @Suppress("NestedBlockDepth") override fun get(id: Long): Transaction? { logger.debug("get {}", id) @@ -24,7 +25,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 + } } } } @@ -101,4 +106,19 @@ class TransactionRepository(dataSource: DataSource) } } } + + override fun search(groupId: Long): List { + 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() + } + } + } + } } diff --git a/repository/postgresql/src/test/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepositoryTest.kt b/repository/postgresql/src/test/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepositoryTest.kt index 4502a73a..425601cb 100644 --- a/repository/postgresql/src/test/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepositoryTest.kt +++ b/repository/postgresql/src/test/kotlin/me/madhead/tyzenhaus/repository/postgresql/transaction/TransactionRepositoryTest.kt @@ -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 @@ -82,4 +83,16 @@ class TransactionRepositoryTest { transactionRepository.groupCurrencies(1) ) } + + @Test + fun search() { + assertTrue( + transactionRepository.search(1).isNotEmpty() + ) + println(transactionRepository.search(6)) + transactionRepository.search(6).forEach { println(it) } + assertTrue( + transactionRepository.search(6).size >= 3 + ) + } } diff --git a/repository/src/main/kotlin/me/madhead/tyzenhaus/repository/TransactionRepository.kt b/repository/src/main/kotlin/me/madhead/tyzenhaus/repository/TransactionRepository.kt index 86b40183..7e2f3c1f 100644 --- a/repository/src/main/kotlin/me/madhead/tyzenhaus/repository/TransactionRepository.kt +++ b/repository/src/main/kotlin/me/madhead/tyzenhaus/repository/TransactionRepository.kt @@ -10,4 +10,9 @@ interface TransactionRepository : Repository { * Retrieves a list of unique currencies used in transactions of the specified group. */ fun groupCurrencies(groupId: Long): List + + /** + * Retrieve transactions matching criteria. + */ + fun search(groupId: Long): List }