diff --git a/src/main/kotlin/org/octopus/internal/common/models/Entry.kt b/src/main/kotlin/org/octopus/internal/common/models/Entry.kt index 7162f2b..f9ed569 100755 --- a/src/main/kotlin/org/octopus/internal/common/models/Entry.kt +++ b/src/main/kotlin/org/octopus/internal/common/models/Entry.kt @@ -1,16 +1,27 @@ package org.octopus.internal.common.models +import com.fasterxml.jackson.annotation.JsonGetter +import com.fasterxml.jackson.annotation.JsonIgnore import org.octopus.internal.common.enums.EEntryType import java.time.Month import java.util.* data class Entry( var id: Long?, + val name: String, val type: EEntryType, val amount: Double, val fixedDate: Date? = null, val recurrent: Boolean? = false, + @get:JsonIgnore val recurrentMonths: List? = mutableListOf(), val startDate: Date? = null, - val endDate: Date? = null -) \ No newline at end of file + val endDate: Date? = null, + val color: String +){ + + @JsonGetter("recurrentMonths") + fun getRecurrentMonthsAsInts(): List? { + return this.recurrentMonths?.map { it.value } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/db/entities/EntryEntity.kt b/src/main/kotlin/org/octopus/internal/db/entities/EntryEntity.kt index d5b9436..63be77b 100644 --- a/src/main/kotlin/org/octopus/internal/db/entities/EntryEntity.kt +++ b/src/main/kotlin/org/octopus/internal/db/entities/EntryEntity.kt @@ -14,6 +14,7 @@ import java.util.Date data class EntryEntity( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0L, + val name: String, val type: EEntryType, val amount: Double, val fixedDate: Date? = null, @@ -21,5 +22,6 @@ data class EntryEntity( @field:Convert(converter = MonthListConverter::class) val recurrentMonths: List? = mutableListOf(), val startDate: Date? = null, - val endDate: Date? = null + val endDate: Date? = null, + val color: String ) \ 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 226e6e8..1535903 100755 --- a/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt +++ b/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt @@ -19,13 +19,15 @@ class EntryServiceImpl(val repo: EntryRepository) : EntryService { return repo.createEntry(Entry( id = 0L, + name = entry.name, type = EEntryType.valueOf(entry.type), amount = entry.amount, fixedDate = entry.fixedDate, startDate = entry.startDate, endDate = entry.endDate, recurrent = entry.recurrent, - recurrentMonths = entry.recurrentMonths?.map { m -> Month.of(m) }?.toList() ?: emptyList() + recurrentMonths = entry.recurrentMonths?.map { m -> Month.of(m) }?.toList() ?: emptyList(), + color = entry.color )) } @@ -45,10 +47,22 @@ class EntryServiceImpl(val repo: EntryRepository) : EntryService { } override fun editEntry(id: Long, entry: EntryDto): Entry { -// var savedEntry = repo.getById(id) -// savedEntry.startDate = entry.startDate - TODO("FINIRE STA MERDA") - return repo.getById(id) + if (!verifyDto(entry)) { + throw OctopusPlanningException.create(EBusinessException.INVALID_REQUEST, entry, "Entry must be either recurring or fixed.") + } + + return repo.createEntry(Entry( + id = id, + name = entry.name, + type = EEntryType.valueOf(entry.type), + amount = entry.amount, + fixedDate = entry.fixedDate, + startDate = entry.startDate, + endDate = entry.endDate, + recurrent = entry.recurrent, + recurrentMonths = entry.recurrentMonths?.map { m -> Month.of(m) }?.toList() ?: emptyList(), + color = entry.color + )) } private fun verifyDto(entry: EntryDto): Boolean { 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 109719b..1ddb67c 100755 --- a/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt +++ b/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt @@ -38,6 +38,12 @@ class EntryController( return WebResponse.ok(entryService.deleteById(id)) } + @PutMapping("/{id}") + fun editEntryById(@PathVariable("id") id: Long, + @Valid @RequestBody entryDto: EntryDto): WebResponse { + return WebResponse.ok(entryService.editEntry(id, entryDto)) + } + // @GetMapping("/{id}") // fun getClient(@PathVariable("id") id: String, @RequestParam( // "includeDeactivated", 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 117fca2..290c8ab 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 @@ -2,12 +2,14 @@ package org.octopus.internal.web.utils.dtos import jakarta.validation.constraints.DecimalMin import org.hibernate.validator.constraints.Length +import org.octopus.internal.web.utils.dtos.validators.EntryHexColorValidator 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:Length(min = 1, max = 50, message = "The name must be between 1 and 50 characters long.") + val name: String, @field:EntryTypeValidator.Validate val type: String, @field:DecimalMin(value="0.00", inclusive = false, message = "The amount must be greater than 0.00.") @@ -18,5 +20,6 @@ data class EntryDto( val recurrentMonths: List?, val startDate: Date?, val endDate: Date?, + @field:EntryHexColorValidator.Validate val color: String = "0xFFFFFF" ) \ No newline at end of file