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 6212af3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 39 deletions.
1 change: 1 addition & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}} <repository/postgresql/src/test/sql/seed.sql
- run: psql -h ${{env.POSTGRES_HOST}} -U ${{env.POSTGRES_USER}} -d ${{env.POSTGRES_DB}} -c 'SELECT * FROM transaction'
- uses: gradle/gradle-build-action@v2
with:
arguments: dbTest jacocoDbTestReport
Expand Down
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 @@ -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)

Expand All @@ -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
}
}
}
}
Expand Down Expand Up @@ -101,4 +106,20 @@ 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;""")
.prepareStatement("""SELECT * FROM "transaction" 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,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
)
}
}
11 changes: 4 additions & 7 deletions repository/postgresql/src/test/sql/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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, '{
Expand Down
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 6212af3

Please sign in to comment.