Skip to content

Commit

Permalink
implement feedback from qa
Browse files Browse the repository at this point in the history
  • Loading branch information
anderssonw committed Sep 9, 2024
1 parent 9b7bc16 commit 178e828
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package no.kartverket.matrikkel.bygning.db

import no.kartverket.matrikkel.bygning.models.Result
import no.kartverket.matrikkel.bygning.models.responses.ErrorDetail
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import javax.sql.DataSource

private val log: Logger = LoggerFactory.getLogger(object {}::class.java)

inline fun <T> DataSource.connection(block: (Connection) -> T) = connection.use(block)

inline fun <T> Connection.createStatement(block: (Statement) -> T) = createStatement().use(block)
Expand Down Expand Up @@ -49,21 +53,19 @@ fun Connection.prepareBatchAndExecuteUpdate(sql: String, batchSetters: List<(Pre
}
}

fun <T> DataSource.withTransaction(block: (Connection) -> Result<T>): Result<T> {
fun <T> DataSource.withTransaction(block: (Connection) -> T): Result<T> {
connection.use { connection ->
try {
connection.autoCommit = false

val result = block(connection)

when (result) {
is Result.Success -> connection.commit()
is Result.ErrorResult -> connection.rollback()
}
connection.commit()

return result
return Result.Success(result)

} catch (_: SQLException) {
} catch (e: SQLException) {
log.warn("Det skjedde noe galt under eksekvering av SQL", e)
connection.rollback()

return Result.ErrorResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import no.kartverket.matrikkel.bygning.models.BygningRegistrering
import no.kartverket.matrikkel.bygning.models.Egenregistrering
import no.kartverket.matrikkel.bygning.models.Registrering
import no.kartverket.matrikkel.bygning.models.Result
import org.intellij.lang.annotations.Language
import org.postgresql.util.PGobject
import java.sql.PreparedStatement
import java.sql.Timestamp
Expand All @@ -26,17 +25,15 @@ class EgenregistreringRepository(private val dataSource: DataSource) {
else -> null
} ?: return null

@Language("PostgreSQL") val getRegistrering = """
val newestRegistrering = dataSource.executeQueryAndMapPreparedStatement(
"""
select r.registrering, e.registrering_tidspunkt
from bygning.registrering r
left join bygning.egenregistrering e on e.id = r.egenregistrering_id
where r.registrering ->> ? = ?
order by e.registrering_tidspunkt DESC
limit 1;
""".trimIndent()

val newestRegistrering = dataSource.executeQueryAndMapPreparedStatement(
getRegistrering,
""".trimIndent(),
{ it.setString(1, idName) },
{ it.setString(2, id.toString()) },
) {
Expand Down Expand Up @@ -70,17 +67,15 @@ class EgenregistreringRepository(private val dataSource: DataSource) {

fun saveEgenregistrering(egenregistrering: Egenregistrering): Result<Int> {
return dataSource.withTransaction<Int> { connection ->
@Language("PostgreSQL") val createEgenregistreringSQL = "INSERT INTO bygning.egenregistrering values (?, ?, ?)"
connection.prepareAndExecuteUpdate(
createEgenregistreringSQL,
"INSERT INTO bygning.egenregistrering values (?, ?, ?)",
{ it.setObject(1, egenregistrering.id) },
{ it.setString(2, egenregistrering.registrerer) },
{ it.setTimestamp(3, Timestamp.from(egenregistrering.registreringTidspunkt)) },
)

@Language("PostgreSQL") val createRegistreringSQL = "INSERT INTO bygning.registrering values (?, ?, ?)"
val bygningAndBruksenheterRegistreringSavedCounts = connection.prepareBatchAndExecuteUpdate(
createRegistreringSQL,
"INSERT INTO bygning.registrering values (?, ?, ?)",
buildList {
egenregistrering.bygningRegistrering?.let { bygningRegistrering ->
add(createRegistreringBatchSetter(egenregistrering.id, bygningRegistrering))
Expand All @@ -92,7 +87,7 @@ class EgenregistreringRepository(private val dataSource: DataSource) {
},
)

return@withTransaction Result.Success(bygningAndBruksenheterRegistreringSavedCounts.sum())
bygningAndBruksenheterRegistreringSavedCounts.sum()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class EgenregistreringService(
val bruksenhet = bygning.bruksenheter.find { it.bruksenhetId == bruksenhetRegistering.bruksenhetId }

if (bruksenhet == null) {
return@mapNotNull bruksenhetRegistering.bruksenhetId
bruksenhetRegistering.bruksenhetId
} else {
return@mapNotNull null
null
}
} ?: emptyList()
}
Expand Down

0 comments on commit 178e828

Please sign in to comment.