From 52033fe77f7fab61187eac660a40468c86b48881 Mon Sep 17 00:00:00 2001 From: Jari Date: Sat, 18 Oct 2025 13:22:30 +0200 Subject: [PATCH] add EntryDTO --- .../octopus/internal/services/EntryService.kt | 2 + .../services/impl/EntryServiceImpl.kt | 7 +++- .../internal/web/utils/dtos/AddRoleDto.kt | 8 ---- .../internal/web/utils/dtos/ClientDto.kt | 24 ------------ .../internal/web/utils/dtos/EntryDto.kt | 17 ++++++++ .../internal/web/utils/dtos/UserAuthDto.kt | 11 ------ .../internal/web/utils/dtos/WorkerDto.kt | 18 --------- .../web/utils/dtos/validators/B64Validator.kt | 33 ---------------- .../web/utils/dtos/validators/DniValidator.kt | 34 ---------------- .../utils/dtos/validators/EmailValidator.kt | 33 ---------------- .../dtos/validators/EntryMonthValidator.kt | 39 +++++++++++++++++++ .../dtos/validators/EntryTypeValidator.kt | 34 ++++++++++++++++ .../web/utils/dtos/validators/ExpValidator.kt | 34 ---------------- .../utils/dtos/validators/NameValidator.kt | 34 ---------------- .../utils/dtos/validators/PhoneValidator.kt | 34 ---------------- .../dtos/validators/UsernameValidator.kt | 33 ---------------- 16 files changed, 98 insertions(+), 297 deletions(-) delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/AddRoleDto.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/ClientDto.kt create mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/EntryDto.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/UserAuthDto.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/WorkerDto.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/B64Validator.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/DniValidator.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EmailValidator.kt create mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryMonthValidator.kt create mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryTypeValidator.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/ExpValidator.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/NameValidator.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/PhoneValidator.kt delete mode 100755 src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/UsernameValidator.kt diff --git a/src/main/kotlin/org/octopus/internal/services/EntryService.kt b/src/main/kotlin/org/octopus/internal/services/EntryService.kt index c72d50c..f6416fc 100755 --- a/src/main/kotlin/org/octopus/internal/services/EntryService.kt +++ b/src/main/kotlin/org/octopus/internal/services/EntryService.kt @@ -1,7 +1,9 @@ package org.octopus.internal.services import org.octopus.internal.common.models.Entry +import org.octopus.internal.web.utils.dtos.EntryDto interface EntryService { + fun createEntry(entry: EntryDto) fun getAllEntries(): MutableList } \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt b/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt index f4d3b21..6511374 100755 --- a/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt +++ b/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt @@ -4,12 +4,17 @@ import org.octopus.internal.common.enums.EEntryType import org.octopus.internal.common.models.Entry import org.octopus.internal.repositories.EntryRepository import org.octopus.internal.services.EntryService +import org.octopus.internal.web.utils.dtos.EntryDto import org.springframework.stereotype.Service @Service class EntryServiceImpl(val repo: EntryRepository) : EntryService { + override fun createEntry(entry: EntryDto) { + TODO("Not yet implemented") + } + override fun getAllEntries(): MutableList { - return repo.getA + TODO("Not yet implemented") } } \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/AddRoleDto.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/AddRoleDto.kt deleted file mode 100755 index 480ba44..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/AddRoleDto.kt +++ /dev/null @@ -1,8 +0,0 @@ -package org.octopus.internal.web.utils.dtos - -import org.octopus.internal.web.utils.dtos.validators.UsernameValidator - -data class AddRoleDto( - @field:UsernameValidator.Validate - val username: String -) \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/ClientDto.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/ClientDto.kt deleted file mode 100755 index b668209..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/ClientDto.kt +++ /dev/null @@ -1,24 +0,0 @@ -package org.octopus.internal.web.utils.dtos - -import org.octopus.internal.web.utils.dtos.validators.DniValidator -import org.octopus.internal.web.utils.dtos.validators.ExpValidator -import org.octopus.internal.web.utils.dtos.validators.NameValidator -import org.octopus.internal.web.utils.dtos.validators.PhoneValidator -import java.util.* - -data class ClientDto( - @field:NameValidator.Validate - val name: String, - @field:NameValidator.Validate - val surname: String, - @field:DniValidator.Validate - val dni: String, - @field:PhoneValidator.Validate - val phone: String, - @field:ExpValidator.Validate - val numExp: String, - @field:ExpValidator.Validate - val numUser: String, - val dateOfBirth: Date, - val deactivated: Boolean? = false -) \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/EntryDto.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/EntryDto.kt new file mode 100755 index 0000000..198b3ed --- /dev/null +++ b/src/main/kotlin/org/octopus/internal/web/utils/dtos/EntryDto.kt @@ -0,0 +1,17 @@ +package org.octopus.internal.web.utils.dtos + +import org.octopus.internal.web.utils.dtos.validators.EntryMonthValidator +import org.octopus.internal.web.utils.dtos.validators.EntryTypeValidator +import java.util.Date + +data class EntryDto( + @field:EntryTypeValidator.Validate + val type: String, + val amount: Double, + val fixedDate: Date?, + val recurrent: Boolean?, + @field:EntryMonthValidator.Validate + val recurrentMonths: List?, + val endDate: Date? + +) \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/UserAuthDto.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/UserAuthDto.kt deleted file mode 100755 index 8124276..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/UserAuthDto.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.octopus.internal.web.utils.dtos - -import org.octopus.internal.web.utils.dtos.validators.B64Validator -import org.octopus.internal.web.utils.dtos.validators.UsernameValidator - -data class UserAuthDto( - @field:UsernameValidator.Validate - val username: String, - @field:B64Validator.Validate - val password: String -) \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/WorkerDto.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/WorkerDto.kt deleted file mode 100755 index 6681833..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/WorkerDto.kt +++ /dev/null @@ -1,18 +0,0 @@ -package org.octopus.internal.web.utils.dtos - -import org.octopus.internal.web.utils.dtos.validators.DniValidator -import org.octopus.internal.web.utils.dtos.validators.EmailValidator -import org.octopus.internal.web.utils.dtos.validators.NameValidator -import java.util.* - -data class WorkerDto( - @field:NameValidator.Validate - val name: String, - @field:NameValidator.Validate - val surname: String, - @field:DniValidator.Validate - val dni: String, - val dateOfBirth: Date, - @field:EmailValidator.Validate - val email: String -) \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/B64Validator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/B64Validator.kt deleted file mode 100755 index c7ee125..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/B64Validator.kt +++ /dev/null @@ -1,33 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class B64Validator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("^[-A-Za-z0-9+/]*={0,3}\$") - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : this is not b64") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [B64Validator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/DniValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/DniValidator.kt deleted file mode 100755 index 1dc763c..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/DniValidator.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class DniValidator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("^\\d{8}[A-Z]$") - - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : 8 numbers and a letter are expected.") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [DniValidator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EmailValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EmailValidator.kt deleted file mode 100755 index 82843bf..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EmailValidator.kt +++ /dev/null @@ -1,33 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class EmailValidator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("^(?=.{1,64}@.{1,255}$)([a-zA-Z0-9._%+-]{1,64})@([a-zA-Z0-9.-]+\\.[a-zA-Z]{2,})$") - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : Email pattern is wrong.") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [EmailValidator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryMonthValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryMonthValidator.kt new file mode 100755 index 0000000..9ad8b13 --- /dev/null +++ b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryMonthValidator.kt @@ -0,0 +1,39 @@ +package org.octopus.internal.web.utils.dtos.validators + +import jakarta.validation.Constraint +import jakarta.validation.ConstraintValidator +import jakarta.validation.ConstraintValidatorContext +import jakarta.validation.Payload +import org.octopus.internal.common.enums.EEntryType +import java.time.Month +import kotlin.reflect.KClass + + +class EntryMonthValidator : ConstraintValidator?> { + override fun isValid(value: List?, context: ConstraintValidatorContext): Boolean { + val monthInt = Month.entries.map { m -> m.value } + if (value == null) { + return true + } + + val allMatched = value.all { v -> monthInt.contains(v) } + + if (!allMatched) { + context.disableDefaultConstraintViolation() + context.buildConstraintViolationWithTemplate("Invalid. '$value' contains at least a value outside [1,12] range.") + .addConstraintViolation() + } + + return allMatched + } + + @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) + @Retention(AnnotationRetention.RUNTIME) + @MustBeDocumented + @Constraint(validatedBy = [EntryMonthValidator::class]) + annotation class Validate( + val message: String = "", + val groups: Array> = [], + val payload: Array> = [] + ) +} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryTypeValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryTypeValidator.kt new file mode 100755 index 0000000..dccc986 --- /dev/null +++ b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/EntryTypeValidator.kt @@ -0,0 +1,34 @@ +package org.octopus.internal.web.utils.dtos.validators + +import jakarta.validation.Constraint +import jakarta.validation.ConstraintValidator +import jakarta.validation.ConstraintValidatorContext +import jakarta.validation.Payload +import org.octopus.internal.common.enums.EEntryType +import kotlin.reflect.KClass + + +class EntryTypeValidator : ConstraintValidator { + override fun isValid(value: String, context: ConstraintValidatorContext): Boolean { + val typeNames = EEntryType.entries.map {e -> e.name} + val matchedType = typeNames.any { e -> e == value } + + if (!matchedType) { + context.disableDefaultConstraintViolation() + context.buildConstraintViolationWithTemplate("Invalid. '$value' is not in accepted values: $typeNames") + .addConstraintViolation() + } + + return matchedType + } + + @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) + @Retention(AnnotationRetention.RUNTIME) + @MustBeDocumented + @Constraint(validatedBy = [EntryTypeValidator::class]) + annotation class Validate( + val message: String = "", + val groups: Array> = [], + val payload: Array> = [] + ) +} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/ExpValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/ExpValidator.kt deleted file mode 100755 index 5d3db22..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/ExpValidator.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class ExpValidator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("\\d+/[0-9]{4}") - - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : digits/full_year is expected") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [ExpValidator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/NameValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/NameValidator.kt deleted file mode 100755 index 31a74b0..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/NameValidator.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class NameValidator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("^[A-Za-zÁÉÍÓÚÜÑáéíóúüñ]+(?:[-' ][A-Za-zÁÉÍÓÚÜÑáéíóúüñ]+)*$") - - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : Only letters, spaces, and dashes are allowed.") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [NameValidator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/PhoneValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/PhoneValidator.kt deleted file mode 100755 index 2bd48f3..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/PhoneValidator.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class PhoneValidator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("^[6789]\\d{8}$") - - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : Phone must start with [6,7,8,9] and 8 more numbers are expected.") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [PhoneValidator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/UsernameValidator.kt b/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/UsernameValidator.kt deleted file mode 100755 index e60d203..0000000 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/validators/UsernameValidator.kt +++ /dev/null @@ -1,33 +0,0 @@ -package org.octopus.internal.web.utils.dtos.validators - -import jakarta.validation.Constraint -import jakarta.validation.ConstraintValidator -import jakarta.validation.ConstraintValidatorContext -import jakarta.validation.Payload -import kotlin.reflect.KClass - - -class UsernameValidator : ConstraintValidator { - override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { - val pattern = Regex("[a-z]{4,16}") - if (value == null || value.matches(pattern)) { - return true - } - - context.disableDefaultConstraintViolation() - context.buildConstraintViolationWithTemplate("Invalid value '$value' : Only 4 to 16 lowercase letters are allowed") - .addConstraintViolation() - - return false - } - - @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER) - @Retention(AnnotationRetention.RUNTIME) - @MustBeDocumented - @Constraint(validatedBy = [UsernameValidator::class]) - annotation class Validate( - val message: String = "", - val groups: Array> = [], - val payload: Array> = [] - ) -} \ No newline at end of file