Refactor BundeslandRepository, implement V012 migration for Reiter master data changes, harmonize domain models, resolve repository interface inconsistencies, and enhance ZNS import with relational mapping improvements.

This commit is contained in:
2026-04-06 16:51:14 +02:00
parent 0ae9a1f1b8
commit 1b6f8e7c59
8 changed files with 75 additions and 28 deletions
@@ -1,6 +1,7 @@
@file:OptIn(kotlin.uuid.ExperimentalUuidApi::class)
package at.mocode.masterdata.domain.repository
import at.mocode.masterdata.domain.model.Bundesland
import at.mocode.masterdata.domain.model.BundeslandDefinition
import kotlin.uuid.Uuid
@@ -12,7 +13,7 @@ interface BundeslandRepository {
/**
* ZNS-Spezifisch: Sucht ein Bundesland anhand seiner Nummer (01-09).
*/
suspend fun findByNr(nr: Int): at.mocode.masterdata.domain.model.BundeslandDefinition?
suspend fun findByNr(nr: Int): BundeslandDefinition?
/**
* Finds a federal state by its unique ID.
@@ -181,7 +181,7 @@ class BundeslandRepositoryImpl : BundeslandRepository {
it[createdAt] = bundesland.createdAt
it[updatedAt] = bundesland.updatedAt
}
} catch (e: Exception) {
} catch (_: Exception) {
// Race-Condition → erneut Update
if (bundesland.kuerzel != null) {
BundeslandTable.update({ (BundeslandTable.landId eq bundesland.landId) and (BundeslandTable.kuerzel eq bundesland.kuerzel) }) {
@@ -2,10 +2,11 @@
package at.mocode.masterdata.infrastructure.persistence.reiter
import at.mocode.masterdata.domain.model.Bundesland
import at.mocode.masterdata.domain.model.BundeslandDefinition
import at.mocode.masterdata.domain.repository.BundeslandRepository
import at.mocode.core.utils.database.DatabaseFactory
import at.mocode.masterdata.infrastructure.persistence.BundeslandTable
import kotlinx.datetime.Clock
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.jdbc.*
@@ -16,14 +17,22 @@ import kotlin.uuid.Uuid
* Hinweis: Implementiert nur die für den ZNS-Import notwendigen Methoden.
*/
class BundeslandExposedRepository : BundeslandRepository {
private fun rowToDom(row: ResultRow) = Bundesland(
id = row[BundeslandTable.id],
private fun rowToDom(row: ResultRow) = BundeslandDefinition(
bundeslandId = row[BundeslandTable.id],
landId = row[BundeslandTable.landId],
bundeslandNr = row[BundeslandTable.bundeslandNr],
bezeichnung = row[BundeslandTable.bezeichnung],
wappenUrl = row[BundeslandTable.wappenUrl]
oepsCode = row[BundeslandTable.oepsCode],
iso3166_2_Code = row[BundeslandTable.iso3166_2_Code],
name = row[BundeslandTable.name],
kuerzel = row[BundeslandTable.kuerzel],
wappenUrl = row[BundeslandTable.wappenUrl],
istAktiv = row[BundeslandTable.istAktiv],
sortierReihenfolge = row[BundeslandTable.sortierReihenfolge],
createdAt = row[BundeslandTable.createdAt],
updatedAt = row[BundeslandTable.updatedAt]
)
override suspend fun findByNr(nr: Int): Bundesland? = DatabaseFactory.dbQuery {
override suspend fun findByNr(nr: Int): BundeslandDefinition? = DatabaseFactory.dbQuery {
BundeslandTable.selectAll().where { BundeslandTable.bundeslandNr eq nr }
.map(::rowToDom)
.singleOrNull()
@@ -9,7 +9,6 @@ import at.mocode.masterdata.domain.repository.VereinRepository
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.like
import org.jetbrains.exposed.v1.core.or
import org.jetbrains.exposed.v1.jdbc.*
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
@@ -36,16 +36,15 @@ ALTER TABLE reiter ADD COLUMN IF NOT EXISTS nation_id UUID;
-- Fremdschlüssel-Constraints
ALTER TABLE reiter ADD CONSTRAINT fk_reiter_verein FOREIGN KEY (verein_id) REFERENCES verein(verein_id);
ALTER TABLE reiter ADD CONSTRAINT fk_reiter_bundesland FOREIGN KEY (bundesland_id) REFERENCES bundesland(id);
ALTER TABLE reiter ADD CONSTRAINT fk_reiter_nation FOREIGN KEY (nation_id) REFERENCES land(id);
ALTER TABLE reiter ADD CONSTRAINT fk_reiter_lizenz_reiter FOREIGN KEY (reiter_id) REFERENCES reiter(reiter_id) ON DELETE CASCADE;
ALTER TABLE reiter ADD CONSTRAINT fk_reiter_nation FOREIGN KEY (nation_id) REFERENCES land(land_id);
ALTER TABLE reiter_lizenz ADD CONSTRAINT fk_reiter_lizenz_reiter FOREIGN KEY (reiter_id) REFERENCES reiter(reiter_id) ON DELETE CASCADE;
-- 3. Daten gemäß OEPS-Spezifikation korrigieren (für Österreich)
DO $$
DECLARE
austria_id UUID;
BEGIN
SELECT id INTO austria_id FROM land WHERE iso_alpha2_code = 'AT';
SELECT land_id INTO austria_id FROM land WHERE iso_alpha2_code = 'AT';
IF austria_id IS NOT NULL THEN
-- Bestehende Einträge löschen oder aktualisieren