diff --git a/.gitignore b/.gitignore index dcb6c75..6e9e444 100644 --- a/.gitignore +++ b/.gitignore @@ -46,4 +46,5 @@ bin/ .idea/* .env -data/* \ No newline at end of file +data/* +testSuite/* \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/services/EntryService.kt b/src/main/kotlin/org/octopus/internal/services/EntryService.kt index f6416fc..e2e0043 100755 --- a/src/main/kotlin/org/octopus/internal/services/EntryService.kt +++ b/src/main/kotlin/org/octopus/internal/services/EntryService.kt @@ -4,6 +4,6 @@ import org.octopus.internal.common.models.Entry import org.octopus.internal.web.utils.dtos.EntryDto interface EntryService { - fun createEntry(entry: EntryDto) + fun createEntry(entry: EntryDto): Entry 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 6511374..ef168a9 100755 --- a/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt +++ b/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt @@ -1,20 +1,47 @@ package org.octopus.internal.services.impl +import org.octopus.internal.common.enums.EBusinessException import org.octopus.internal.common.enums.EEntryType +import org.octopus.internal.common.exceptions.OctopusPlanningException 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 +import java.time.Month @Service class EntryServiceImpl(val repo: EntryRepository) : EntryService { - override fun createEntry(entry: EntryDto) { - TODO("Not yet implemented") + override fun createEntry(entry: EntryDto): Entry { + if (!verifyDto(entry)) { + throw OctopusPlanningException.create(EBusinessException.INVALID_REQUEST, entry, "Entry must be either recurring or fixed.") + } + + return repo.createEntry(Entry( + id = 0L, + type = EEntryType.valueOf(entry.type), + amount = entry.amount, + fixedDate = entry.fixedDate, + endDate = entry.endDate, + recurrent = entry.recurrent, + recurrentMonths = entry.recurrentMonths?.map { m -> Month.of(m) } + )) } override fun getAllEntries(): MutableList { TODO("Not yet implemented") } + private fun verifyDto(entry: EntryDto): Boolean { + val eventIsRecurring = entry.fixedDate == null && + entry.endDate != null && + entry.recurrent == true && + entry.recurrentMonths?.isNotEmpty() ?: false + val eventIsFixed = entry.fixedDate != null && + entry.endDate == null && + entry.recurrent != true && + entry.recurrentMonths?.isEmpty() ?: true + return eventIsRecurring || eventIsFixed + } + } \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt b/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt index 5d36a6e..c853c3d 100755 --- a/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt +++ b/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt @@ -1,9 +1,12 @@ package org.octopus.internal.web.controllers +import jakarta.validation.Valid import lombok.AllArgsConstructor import org.octopus.internal.common.models.Entry import org.octopus.internal.services.EntryService +import org.octopus.internal.web.utils.dtos.EntryDto import org.octopus.internal.web.utils.responses.WebResponse +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web import org.springframework.web.bind.annotation.* @RestController @@ -13,6 +16,13 @@ class EntryController( val entryService: EntryService ) { + @PostMapping + fun createEntry( + @Valid @RequestBody entryDto: EntryDto + ): WebResponse { + return WebResponse.ok(entryService.createEntry(entryDto)) + } + @GetMapping fun getAllClients(): WebResponse> { return WebResponse.ok(entryService.getAllEntries()) diff --git a/src/main/kotlin/org/octopus/internal/web/utils/BaseAdvice.kt b/src/main/kotlin/org/octopus/internal/web/utils/BaseAdvice.kt index 19d6228..74a73ab 100755 --- a/src/main/kotlin/org/octopus/internal/web/utils/BaseAdvice.kt +++ b/src/main/kotlin/org/octopus/internal/web/utils/BaseAdvice.kt @@ -95,8 +95,7 @@ class BaseAdvice { ): WebResponse { return WebResponse.ko( HttpStatus.NOT_ACCEPTABLE, - "${HttpStatus.NOT_ACCEPTABLE.reasonPhrase}: JSON parse error" - + "${HttpStatus.NOT_ACCEPTABLE.reasonPhrase}: JSON parse error. Please verify the body has the correct format.", ) } 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 index 198b3ed..13cd292 100755 --- a/src/main/kotlin/org/octopus/internal/web/utils/dtos/EntryDto.kt +++ b/src/main/kotlin/org/octopus/internal/web/utils/dtos/EntryDto.kt @@ -1,5 +1,6 @@ package org.octopus.internal.web.utils.dtos +import jakarta.validation.constraints.DecimalMin import org.octopus.internal.web.utils.dtos.validators.EntryMonthValidator import org.octopus.internal.web.utils.dtos.validators.EntryTypeValidator import java.util.Date @@ -7,6 +8,7 @@ import java.util.Date data class EntryDto( @field:EntryTypeValidator.Validate val type: String, + @field:DecimalMin(value="0.00", inclusive = false, message = "Amount value cannot be lower than 0.01") val amount: Double, val fixedDate: Date?, val recurrent: Boolean?,