From 10c4618546b2b38f83bd03e5e755d66abcefd88f Mon Sep 17 00:00:00 2001 From: madhead Date: Thu, 26 Oct 2023 19:12:28 +0200 Subject: [PATCH] Search method for the transaction repository --- .github/workflows/default.yml | 1 + .../transaction/TransactionExtensions.kt | 63 ++++++++++--------- .../transaction/TransactionRepository.kt | 22 ++++++- .../transaction/TransactionRepositoryTest.kt | 13 ++++ repository/postgresql/src/test/sql/seed.sql | 11 ++-- .../repository/TransactionRepository.kt | 5 ++ 6 files changed, 76 insertions(+), 39 deletions(-) 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..0dad4dc4 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() { + println(transactionRepository.search(1)) + assertTrue( + transactionRepository.search(1).isNotEmpty() + ) + println(transactionRepository.search(6)) + assertTrue( + transactionRepository.search(6).size >= 3 + ) + } } diff --git a/repository/postgresql/src/test/sql/seed.sql b/repository/postgresql/src/test/sql/seed.sql index 7ae7cf6e..e05a87cc 100644 --- a/repository/postgresql/src/test/sql/seed.sql +++ b/repository/postgresql/src/test/sql/seed.sql @@ -29,13 +29,10 @@ VALUES (6, 2, '{ }'::JSONB); INSERT INTO "transaction" ("id", "group_id", "payer", "recipients", "amount", "currency", "title", "timestamp") -VALUES (1, 1, 1, '{1,2,3}', 42.99, 'USD', 'Lunch', '1995-08-12 00:00:00 +03:00'); -INSERT INTO "transaction" ("id", "group_id", "payer", "recipients", "amount", "currency", "title", "timestamp") -VALUES (2, 6, 1, '{1,2,3}', 42.99, 'USD', 'Breakfast', '1995-08-12 00:00:00 +03:00'); -INSERT INTO "transaction" ("id", "group_id", "payer", "recipients", "amount", "currency", "title", "timestamp") -VALUES (3, 6, 2, '{1,2,3}', 42.99, 'USD', 'Lunch', '1995-08-12 00:00:00 +03:00'); -INSERT INTO "transaction" ("id", "group_id", "payer", "recipients", "amount", "currency", "title", "timestamp") -VALUES (4, 6, 3, '{1,2,3}', 42.99, 'USD', 'Dinner', '1995-08-12 00:00:00 +03:00'); +VALUES (1, 1, 1, '{1,2,3}', 42.99, 'USD', 'Lunch', '1995-08-12 00:00:00 +03:00'), + (2, 6, 1, '{1,2,3}', 42.99, 'USD', 'Breakfast', '1995-08-12 00:00:00 +03:00'), + (3, 6, 2, '{1,2,3}', 42.99, 'USD', 'Lunch', '1995-08-12 00:00:00 +03:00'), + (4, 6, 3, '{1,2,3}', 42.99, 'USD', 'Dinner', '1995-08-12 00:00:00 +03:00'); INSERT INTO balance ("group_id", "version", "balance") VALUES (1, 2, '{ 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 }