Fix issues with transaction and category routes

This commit is contained in:
William Brawner 2021-08-13 16:09:05 -06:00
parent 2900915842
commit ff70db9280
4 changed files with 23 additions and 10 deletions

View file

@ -43,10 +43,14 @@ fun Application.categoryRoutes(
errorResponse()
return@get
}
call.respond(categoryRepository.findAll(
categoryRepository.findAll(
ids = call.parameters.getAll("id"),
budgetIds = budgetIds
).map { it.asResponse() })
)
.map { it.asResponse() }
.firstOrNull()?.let {
call.respond(it)
} ?: errorResponse()
}
post {
@ -113,7 +117,8 @@ fun Application.categoryRoutes(
delete("/{id}") {
val session = call.principal<Session>()!!
val category = categoryRepository.findAll(ids = call.parameters.getAll("id"))
val categoryId = call.parameters.entries().first().value
val category = categoryRepository.findAll(ids = categoryId)
.firstOrNull()
?: run {
errorResponse(HttpStatusCode.NotFound)
@ -128,6 +133,7 @@ fun Application.categoryRoutes(
return@delete
}
categoryRepository.delete(category)
call.respond(HttpStatusCode.NoContent)
}
}
}

View file

@ -158,7 +158,12 @@ fun Application.transactionRoutes(
) {
return@delete
}
transactionRepository.delete(transaction)
val response = if (transactionRepository.delete(transaction)) {
HttpStatusCode.NoContent
} else {
HttpStatusCode.InternalServerError
}
call.respond(response)
}
}
}

View file

@ -102,7 +102,7 @@ abstract class JdbcRepository<Entity, Fields : Enum<Fields>>(protected val dataS
is String -> setString(index + 1, param)
is Enum<*> -> setString(index + 1, param.name)
null -> setNull(index + 1, NULL)
else -> throw Error("Unhandled parameter type: ${param?.javaClass?.name}")
else -> throw Error("Unhandled parameter type: ${param.javaClass.name}")
}
}
}

View file

@ -21,10 +21,8 @@ class JdbcTransactionRepository(dataSource: DataSource) :
to: Instant?
): List<Transaction> = dataSource.connection.use { conn ->
val sql = StringBuilder("SELECT * FROM $tableName")
val params = mutableListOf<Any?>(budgetIds)
val params = mutableListOf<Any?>()
fun queryWord(): String = if (params.isEmpty()) " WHERE" else " AND"
ids?.let {
sql.append("${queryWord()} $ID IN (${it.questionMarks()})")
params.addAll(it)
@ -49,6 +47,7 @@ class JdbcTransactionRepository(dataSource: DataSource) :
sql.append("${queryWord()} ${Fields.DATE.name.lowercase()} <= ?")
params.add(it)
}
sql.append(" ORDER BY ${Fields.DATE.name.lowercase()} DESC")
conn.executeQuery(sql.toString(), params)
}
@ -75,7 +74,10 @@ class JdbcTransactionRepository(dataSource: DataSource) :
conn.prepareStatement("SELECT (${sql.toString().coalesce()}) - (${sql.toString().coalesce()})")
.setParameters(params + false + params + true)
.executeQuery()
.getLong(1)
.run {
next()
getLong(1)
}
}
private fun String.coalesce(): String = "COALESCE(($this), 0)"
@ -84,7 +86,7 @@ class JdbcTransactionRepository(dataSource: DataSource) :
id = getString(ID),
title = getString(Fields.TITLE.name.lowercase()),
description = getString(Fields.DESCRIPTION.name.lowercase()),
date = Instant.parse(getString(Fields.DATE.name.lowercase())),
date = getInstant(Fields.DATE.name.lowercase()),
amount = getLong(Fields.AMOUNT.name.lowercase()),
expense = getBoolean(Fields.EXPENSE.name.lowercase()),
createdBy = getString(Fields.CREATED_BY.name.lowercase()),