Skip to content

Commit

Permalink
1762: adjust code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
f1sh1918 committed Feb 28, 2025
1 parent 598e6af commit 9cb5b39
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import app.ehrenamtskarte.backend.freinet.database.FreinetAgencies
import app.ehrenamtskarte.backend.freinet.database.FreinetAgenciesEntity

object FreinetAgencyRepository {
fun getFreinetAgencyByRegionId(regionId: Int): FreinetAgenciesEntity? {
return FreinetAgenciesEntity.find { FreinetAgencies.regionId eq regionId }.singleOrNull()
}
fun getFreinetAgencyByRegionId(regionId: Int): FreinetAgenciesEntity? =
FreinetAgenciesEntity.find { FreinetAgencies.regionId eq regionId }.singleOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class FreinetAgenciesLoader {
}

fun loadAgenciesFromXml(projectConfigs: List<ProjectConfig>): List<FreinetApiAgency> {
val bayernConfig =
projectConfigs.find { it.id == EAK_BAYERN_PROJECT } ?: throw NotFoundException("Project config not found")
val bayernConfig = projectConfigs.find { it.id == EAK_BAYERN_PROJECT } ?: throw NotFoundException("Project config not found")
if (bayernConfig.freinet == null) {
logger.error("Couldn't find required freinet api parameters in backend config.")
return emptyList()
Expand All @@ -60,12 +59,13 @@ class FreinetAgenciesLoader {
}.bodyAsText()
}

val xmlMapper = XmlMapper()
xmlMapper.registerModule(KotlinModule.Builder().build())
xmlMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

return transformAndFilterAgencyData(xmlMapper.readValue(response, XMLAgencies::class.java))
return transformAndFilterAgencyData(
XmlMapper().apply {
registerModule(KotlinModule.Builder().build())
enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
}.readValue(response, XMLAgencies::class.java)
)
} catch (e: Exception) {
logger.error("Couldn't fetch agency information: ", e)
return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ class FreinetAgencyQueryService {
fun getFreinetAgencyByRegionId(dfe: DataFetchingEnvironment, regionId: Int, project: String): FreinetAgency? = transaction {
val context = dfe.getContext<GraphQLContext>()
val jwtPayload = context.enforceSignedIn()
val admin =
AdministratorEntity.findById(jwtPayload.adminId)
?: throw UnauthorizedException()
val admin = AdministratorEntity.findById(jwtPayload.adminId) ?: throw UnauthorizedException()
if (!Authorizer.mayViewFreinetAgencyInformationInRegion(admin, regionId)) {
throw ForbiddenException()
}
val projectEntity = ProjectEntity.find { Projects.project eq project }.firstOrNull()
ProjectEntity.find { Projects.project eq project }.firstOrNull()
?.let {
if (it.project != EAK_BAYERN_PROJECT) {
throw NotEakProjectException()
}
}
?: throw ProjectNotFoundException(project)

if (projectEntity.project != EAK_BAYERN_PROJECT) {
throw NotEakProjectException()
FreinetAgencyRepository.getFreinetAgencyByRegionId(regionId)?.let {
FreinetAgency(
agencyId = it.agencyId,
apiAccessKey = it.apiAccessKey,
agencyName = it.agencyName,
dataTransferActivated = it.dataTransferActivated
)
}
val agency = FreinetAgencyRepository.getFreinetAgencyByRegionId(regionId)
agency?.let { FreinetAgency(agencyId = it.agencyId, apiAccessKey = agency.apiAccessKey, agencyName = agency.agencyName, dataTransferActivated = agency.dataTransferActivated) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class V0026_AddFreinetAgenciesTable : Migration() {
exec(
"""
CREATE TABLE freinetagencies (
id SERIAL PRIMARY KEY ,
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"regionId" INTEGER NOT NULL,
"agencyId" INTEGER NOT NULL,
"agencyName" character varying(255) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,21 @@ fun insertOrUpdateRegions(agencies: List<FreinetApiAgency>) {
?: throw Error("Required project '$EAK_BAYERN_PROJECT' not found!")
EAK_BAYERN_REGIONS.forEach { eakRegion ->
val dbRegion = dbRegions.find { it.regionIdentifier == eakRegion[2] && it.projectId == eakProject.id }
val regionEntity: RegionEntity = if (dbRegion == null) {
RegionEntity.new {
val regionEntity: RegionEntity = dbRegion
?.apply {
name = eakRegion[1]
prefix = eakRegion[0]
website = eakRegion[3]
}
?: RegionEntity.new {
projectId = eakProject.id
name = eakRegion[1]
prefix = eakRegion[0]
regionIdentifier = eakRegion[2]
website = eakRegion[3]
}
} else {
dbRegion.name = eakRegion[1]
dbRegion.prefix = eakRegion[0]
dbRegion.website = eakRegion[3]
dbRegion
}
val agency = agencies.find { agency -> agency.officialRegionalKeys.any { it.startsWith(eakRegion[2]) } }
if (agency != null) {
agencies.find { agency -> agency.officialRegionalKeys.any { it.startsWith(eakRegion[2]) } }?.let {
agency ->
insertOrUpdateFreinetRegionInformation(agency, dbFreinetRegionInformation, regionEntity)
}
}
Expand Down

0 comments on commit 9cb5b39

Please sign in to comment.