From be8174ab6b83a0a693be617e393e4a007df1d5d8 Mon Sep 17 00:00:00 2001 From: Chaoran Chen Date: Sun, 29 Dec 2024 16:17:35 +0300 Subject: [PATCH] refactor(backend): reduce redundancy in DataUseTerms.fromParameters() --- .../org/loculus/backend/api/DataUseTerms.kt | 43 ++++++++----------- .../DataUseTermsDatabaseService.kt | 2 +- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/backend/src/main/kotlin/org/loculus/backend/api/DataUseTerms.kt b/backend/src/main/kotlin/org/loculus/backend/api/DataUseTerms.kt index eed9fd22c0..fbd2591153 100644 --- a/backend/src/main/kotlin/org/loculus/backend/api/DataUseTerms.kt +++ b/backend/src/main/kotlin/org/loculus/backend/api/DataUseTerms.kt @@ -83,35 +83,30 @@ sealed interface DataUseTerms { } companion object { - fun fromParameters(type: DataUseTermsType, restrictedUntilString: String?): DataUseTerms = when (type) { - DataUseTermsType.OPEN -> Open - DataUseTermsType.RESTRICTED -> Restricted(parseRestrictedUntil(restrictedUntilString)) - } + fun fromParameters(type: DataUseTermsType, restrictedUntil: String?): DataUseTerms = + if (type == DataUseTermsType.OPEN) { + Open + } else { + fromParameters(type, restrictedUntil?.let { parseDate(it) }) + } - fun fromParameters(type: DataUseTermsType, restrictedUntilString: LocalDate?): DataUseTerms = when (type) { + fun fromParameters(type: DataUseTermsType, restrictedUntil: LocalDate?): DataUseTerms = when (type) { DataUseTermsType.OPEN -> Open - DataUseTermsType.RESTRICTED -> - if (restrictedUntilString == null) { - throw BadRequestException( - "The date 'restrictedUntil' must be set if 'dataUseTermsType' is RESTRICTED.", - ) - } else { - Restricted(restrictedUntilString) - } - } - - private fun parseRestrictedUntil(restrictedUntilString: String?): LocalDate { - if (restrictedUntilString == null) { - throw BadRequestException("The date 'restrictedUntil' must be set if 'dataUseTermsType' is RESTRICTED.") - } - return try { - LocalDate.parse(restrictedUntilString) - } catch (e: Exception) { - throw BadRequestException( - "The date 'restrictedUntil' must be a valid date in the format YYYY-MM-DD: $restrictedUntilString.", + DataUseTermsType.RESTRICTED -> { + restrictedUntil ?: throw BadRequestException( + "The date 'restrictedUntil' must be set if 'dataUseTermsType' is RESTRICTED.", ) + Restricted(restrictedUntil) } } + + private fun parseDate(restrictedUntilString: String): LocalDate = try { + LocalDate.parse(restrictedUntilString) + } catch (e: Exception) { + throw BadRequestException( + "The date 'restrictedUntil' must be a valid date in the format YYYY-MM-DD: $restrictedUntilString.", + ) + } } } diff --git a/backend/src/main/kotlin/org/loculus/backend/service/datauseterms/DataUseTermsDatabaseService.kt b/backend/src/main/kotlin/org/loculus/backend/service/datauseterms/DataUseTermsDatabaseService.kt index 124c135c9a..79c7b53456 100644 --- a/backend/src/main/kotlin/org/loculus/backend/service/datauseterms/DataUseTermsDatabaseService.kt +++ b/backend/src/main/kotlin/org/loculus/backend/service/datauseterms/DataUseTermsDatabaseService.kt @@ -66,7 +66,7 @@ class DataUseTermsDatabaseService( changeDate = it[DataUseTermsTable.changeDateColumn].toString(), dataUseTerms = DataUseTerms.fromParameters( type = DataUseTermsType.fromString(it[DataUseTermsTable.dataUseTermsTypeColumn]), - restrictedUntilString = it[DataUseTermsTable.restrictedUntilColumn], + restrictedUntil = it[DataUseTermsTable.restrictedUntilColumn], ), userName = it[DataUseTermsTable.userNameColumn], )