65 lines
2.3 KiB
Kotlin
65 lines
2.3 KiB
Kotlin
package space.luminic.budgerapp.repos.sqlrepo
|
|
|
|
import org.slf4j.LoggerFactory
|
|
import org.springframework.jdbc.core.JdbcTemplate
|
|
import org.springframework.jdbc.core.RowMapper
|
|
import org.springframework.stereotype.Repository
|
|
import space.luminic.budgerapp.models.Category
|
|
import space.luminic.budgerapp.models.Transaction
|
|
import space.luminic.budgerapp.models.TransactionType
|
|
import space.luminic.budgerapp.models.User
|
|
import space.luminic.budgerapp.repos.CategoryRepoOld
|
|
import space.luminic.budgerapp.repos.UserRepoOld
|
|
import space.luminic.budgerapp.services.CategoryService
|
|
import space.luminic.budgerapp.services.UserService
|
|
import java.sql.SQLException
|
|
import java.time.LocalDate
|
|
import java.time.ZoneId
|
|
|
|
|
|
@Repository
|
|
class TransactionsRepoSQl(
|
|
private val jdbcTemplate: JdbcTemplate,
|
|
private val userRepoOld: UserRepoOld,
|
|
private val categoryRepoOld: CategoryRepoOld
|
|
) {
|
|
|
|
private val logger = LoggerFactory.getLogger(TransactionsRepoSQl::class.java)
|
|
|
|
|
|
fun getTransactions(): List<Transaction> {
|
|
|
|
|
|
return jdbcTemplate.query(
|
|
"SELECT tt.code as tt_code, tt.name as tt_name, u.username, c.name as c_name, t.comment, t.date, t.amount, t.is_done, t.created_at, t.id" +
|
|
" FROM budger.transactions t" +
|
|
" JOIN budger.transaction_types tt on t.transaction_type_code = tt.code " +
|
|
" JOIN budger.categories c on t.category_id = c.id" +
|
|
" LEFT JOIN budger.users u on t.user_id = u.id", transactionRowMapper()
|
|
)
|
|
}
|
|
|
|
|
|
fun transactionRowMapper() = RowMapper { rs, _ ->
|
|
|
|
|
|
val transaction = Transaction(
|
|
type = TransactionType(rs.getString("tt_code"), rs.getString("tt_name")),
|
|
user = rs.getString("username")
|
|
?.let { userRepoOld.findByUsername(it) }
|
|
?: userRepoOld.findByUsername("voroninv"),
|
|
category = categoryRepoOld.findByName(rs.getString("c_name"))!!,
|
|
comment = rs.getString("comment"),
|
|
date = rs.getDate("date").toLocalDate(),
|
|
amount = rs.getDouble("amount"),
|
|
isDone = rs.getBoolean("is_done"),
|
|
createdAt = rs.getTimestamp("created_at").toInstant()
|
|
.atZone(ZoneId.systemDefault())
|
|
.toLocalDateTime()
|
|
)
|
|
logger.info(transaction.toString())
|
|
return@RowMapper transaction
|
|
}
|
|
|
|
|
|
} |