Add creation

This commit is contained in:
Jari 2025-10-18 16:00:58 +02:00
parent 52033fe77f
commit c93186cc52
6 changed files with 45 additions and 6 deletions

3
.gitignore vendored
View File

@ -46,4 +46,5 @@ bin/
.idea/*
.env
data/*
data/*
testSuite/*

View File

@ -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<Entry>
}

View File

@ -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<Entry> {
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
}
}

View File

@ -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<Entry> {
return WebResponse.ok(entryService.createEntry(entryDto))
}
@GetMapping
fun getAllClients(): WebResponse<List<Entry>> {
return WebResponse.ok(entryService.getAllEntries())

View File

@ -95,8 +95,7 @@ class BaseAdvice {
): WebResponse<Unit> {
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.",
)
}

View File

@ -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?,