Skip to content

Commit

Permalink
Add currencies query to the transactions repository
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 24, 2023
1 parent ab3c587 commit cdf07f9
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package me.madhead.tyzenhaus.core.service

import me.madhead.tyzenhaus.repository.BalanceRepository
import me.madhead.tyzenhaus.repository.TransactionRepository

/**
* Lists currencies used in transactions of the group.
*/
class GroupCurrenciesService(
private val balanceRepository: BalanceRepository,
private val transactionRepository: TransactionRepository
) {
/**
* Lists currencies used in transactions of the [group].
*/
fun groupCurrencies(group: Long): List<String>? {
return balanceRepository
.get(group)
?.balance
?.keys
?.takeUnless { it.isEmpty() }
?.toList()
return transactionRepository.groupCurrencies(group).takeUnless { it.isEmpty() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.koin.dsl.module
val serviceModule = module {
single {
GroupCurrenciesService(
balanceRepository = get(),
transactionRepository = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ internal fun ResultSet.toTransaction(): Transaction? {
null
}
}

internal fun ResultSet.toCurrencies(): List<String> {
val result = mutableListOf<String>()

while (this.next()) {
result.add(this.getString("currency"))
}

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ class TransactionRepository(dataSource: DataSource)
}
}
}

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

dataSource.connection.use { connection ->
connection
.prepareStatement("""SELECT DISTINCT("currency") FROM "transaction" WHERE "group_id" = ?;""")
.use { preparedStatement ->
preparedStatement.setLong(@Suppress("MagicNumber") 1, groupId)
preparedStatement.executeQuery().use { resultSet ->
return@groupCurrencies resultSet.toCurrencies()
}
}
}
}
}
11 changes: 11 additions & 0 deletions repository/postgresql/src/main/liquibase/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ databaseChangeLog:
- sqlFile:
path: ../sql/api-token.deinit.sql
relativeToChangelogFile: true
- changeSet:
id: 8
author: madhead
changes:
- sqlFile:
path: ../sql/transaction-add-group-id-index.sql
relativeToChangelogFile: true
rollback:
- sqlFile:
path: ../sql/transaction-remove-group-id-index.sql
relativeToChangelogFile: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX transaction_group_id_hash ON transaction USING HASH (group_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX transaction_group_id_hash;
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ class TransactionRepositoryTest {
transactionRepository.get(-2)
)
}

@Test
fun groupCurrencies() {
assertEquals(
listOf("USD"),
transactionRepository.groupCurrencies(1)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ import me.madhead.tyzenhaus.entity.transaction.Transaction
/**
* Transactions repository.
*/
interface TransactionRepository : Repository<Long, Transaction>
interface TransactionRepository : Repository<Long, Transaction> {
/**
* Retrieves a list of unique currencies used in transactions of the specified group.
*/
fun groupCurrencies(groupId: Long): List<String>
}

0 comments on commit cdf07f9

Please sign in to comment.