From 15ad6280788e9f86d1b52c0159f6874656689ad1 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 | 2 + .../transaction/TransactionExtensions.kt | 63 ++++++++++--------- .../transaction/TransactionRepository.kt | 23 ++++++- .../transaction/TransactionRepositoryTest.kt | 48 +++++++++++++- repository/postgresql/src/test/sql/seed.sql | 11 ++-- .../repository/TransactionRepository.kt | 5 ++ 6 files changed, 110 insertions(+), 42 deletions(-) diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 0878bd8e..ff5846b6 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -71,7 +71,9 @@ jobs: cache-read-only: ${{ github.ref != 'refs/heads/master' }} env: DATABASE_URL: postgres://${{env.POSTGRES_USER}}:${{env.POSTGRES_PASSWORD}}@${{env.POSTGRES_HOST}}:${{env.POSTGRES_PORT}}/${{env.POSTGRES_DB}} + - run: cat repository/postgresql/src/test/sql/seed.sql - 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..b7f29a54 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,20 @@ 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;""") + .prepareStatement("""SELECT * FROM "transaction" 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..ca100adf 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 @@ -31,55 +32,96 @@ class TransactionRepositoryTest { @Test fun get() { + println("get") + println(transactionRepository.search(1)) assertEquals( Transaction(1, 1, 1, setOf(1, 2, 3), "42.990000".toBigDecimal(), "USD", "Lunch", Instant.ofEpochMilli(808174800000)), transactionRepository.get(1) ) + println("get") + println(transactionRepository.search(1)) } @Test fun getNonExisting() { + println("getNonExisting") + println(transactionRepository.search(1)) assertNull(transactionRepository.get(0)) + println("getNonExisting") + println(transactionRepository.search(1)) } @Test fun save() { + println("save") + println(transactionRepository.search(1)) transactionRepository.save( Transaction(-1, 2, 3, setOf(3, 2, 1), "10000.000000".toBigDecimal(), "€", "Lux hotel", Instant.ofEpochMilli(808174800000)) ) - + println("save") + println(transactionRepository.search(1)) assertEquals( Transaction(-1, 2, 3, setOf(3, 2, 1), "10000.000000".toBigDecimal(), "€", "Lux hotel", Instant.ofEpochMilli(808174800000)), transactionRepository.get(-1) ) + println("save") + println(transactionRepository.search(1)) } @Test fun update() { + println("update") + println(transactionRepository.search(1)) transactionRepository.save( Transaction(-2, 2, 3, setOf(3, 2, 1), "10000.000000".toBigDecimal(), "€", "Lux hotel", Instant.ofEpochMilli(808174800000)) ) - + println("update") + println(transactionRepository.search(1)) assertEquals( Transaction(-2, 2, 3, setOf(3, 2, 1), "10000.000000".toBigDecimal(), "€", "Lux hotel", Instant.ofEpochMilli(808174800000)), transactionRepository.get(-2) ) - + println("update") + println(transactionRepository.search(1)) transactionRepository.save( Transaction(-2, 2, 3, setOf(3, 2, 1), "20000.000000".toBigDecimal(), "€", "Lux hotel", Instant.ofEpochMilli(808174800000)) ) + println("update") + println(transactionRepository.search(1)) assertEquals( Transaction(-2, 2, 3, setOf(3, 2, 1), "20000.000000".toBigDecimal(), "€", "Lux hotel", Instant.ofEpochMilli(808174800000)), transactionRepository.get(-2) ) + println("update") + println(transactionRepository.search(1)) } @Test fun groupCurrencies() { + println("groupCurrencies") + println(transactionRepository.search(1)) assertEquals( listOf("USD"), transactionRepository.groupCurrencies(1) ) + println("groupCurrencies") + println(transactionRepository.search(1)) + } + + @Test + fun search() { + println("search") + println(transactionRepository.search(1)) + assertTrue( + transactionRepository.search(1).isNotEmpty() + ) + println("search") + println(transactionRepository.search(1)) + assertTrue( + transactionRepository.search(6).size >= 3 + ) + println("search") + println(transactionRepository.search(1)) } } 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 }