feature/creation #2
|
|
@ -46,4 +46,5 @@ bin/
|
||||||
|
|
||||||
.idea/*
|
.idea/*
|
||||||
.env
|
.env
|
||||||
data/*
|
data/*
|
||||||
|
testSuite/*
|
||||||
|
|
@ -4,6 +4,6 @@ import org.octopus.internal.common.models.Entry
|
||||||
import org.octopus.internal.web.utils.dtos.EntryDto
|
import org.octopus.internal.web.utils.dtos.EntryDto
|
||||||
|
|
||||||
interface EntryService {
|
interface EntryService {
|
||||||
fun createEntry(entry: EntryDto)
|
fun createEntry(entry: EntryDto): Entry
|
||||||
fun getAllEntries(): MutableList<Entry>
|
fun getAllEntries(): MutableList<Entry>
|
||||||
}
|
}
|
||||||
|
|
@ -1,20 +1,47 @@
|
||||||
package org.octopus.internal.services.impl
|
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.enums.EEntryType
|
||||||
|
import org.octopus.internal.common.exceptions.OctopusPlanningException
|
||||||
import org.octopus.internal.common.models.Entry
|
import org.octopus.internal.common.models.Entry
|
||||||
import org.octopus.internal.repositories.EntryRepository
|
import org.octopus.internal.repositories.EntryRepository
|
||||||
import org.octopus.internal.services.EntryService
|
import org.octopus.internal.services.EntryService
|
||||||
import org.octopus.internal.web.utils.dtos.EntryDto
|
import org.octopus.internal.web.utils.dtos.EntryDto
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import java.time.Month
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class EntryServiceImpl(val repo: EntryRepository) : EntryService {
|
class EntryServiceImpl(val repo: EntryRepository) : EntryService {
|
||||||
override fun createEntry(entry: EntryDto) {
|
override fun createEntry(entry: EntryDto): Entry {
|
||||||
TODO("Not yet implemented")
|
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> {
|
override fun getAllEntries(): MutableList<Entry> {
|
||||||
TODO("Not yet implemented")
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
package org.octopus.internal.web.controllers
|
package org.octopus.internal.web.controllers
|
||||||
|
|
||||||
|
import jakarta.validation.Valid
|
||||||
import lombok.AllArgsConstructor
|
import lombok.AllArgsConstructor
|
||||||
import org.octopus.internal.common.models.Entry
|
import org.octopus.internal.common.models.Entry
|
||||||
import org.octopus.internal.services.EntryService
|
import org.octopus.internal.services.EntryService
|
||||||
|
import org.octopus.internal.web.utils.dtos.EntryDto
|
||||||
import org.octopus.internal.web.utils.responses.WebResponse
|
import org.octopus.internal.web.utils.responses.WebResponse
|
||||||
|
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.web.bind.annotation.*
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
@ -13,6 +16,13 @@ class EntryController(
|
||||||
val entryService: EntryService
|
val entryService: EntryService
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
fun createEntry(
|
||||||
|
@Valid @RequestBody entryDto: EntryDto
|
||||||
|
): WebResponse<Entry> {
|
||||||
|
return WebResponse.ok(entryService.createEntry(entryDto))
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
fun getAllClients(): WebResponse<List<Entry>> {
|
fun getAllClients(): WebResponse<List<Entry>> {
|
||||||
return WebResponse.ok(entryService.getAllEntries())
|
return WebResponse.ok(entryService.getAllEntries())
|
||||||
|
|
|
||||||
|
|
@ -95,8 +95,7 @@ class BaseAdvice {
|
||||||
): WebResponse<Unit> {
|
): WebResponse<Unit> {
|
||||||
return WebResponse.ko(
|
return WebResponse.ko(
|
||||||
HttpStatus.NOT_ACCEPTABLE,
|
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.",
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package org.octopus.internal.web.utils.dtos
|
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.EntryMonthValidator
|
||||||
import org.octopus.internal.web.utils.dtos.validators.EntryTypeValidator
|
import org.octopus.internal.web.utils.dtos.validators.EntryTypeValidator
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
@ -7,6 +8,7 @@ import java.util.Date
|
||||||
data class EntryDto(
|
data class EntryDto(
|
||||||
@field:EntryTypeValidator.Validate
|
@field:EntryTypeValidator.Validate
|
||||||
val type: String,
|
val type: String,
|
||||||
|
@field:DecimalMin(value="0.00", inclusive = false, message = "Amount value cannot be lower than 0.01")
|
||||||
val amount: Double,
|
val amount: Double,
|
||||||
val fixedDate: Date?,
|
val fixedDate: Date?,
|
||||||
val recurrent: Boolean?,
|
val recurrent: Boolean?,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue