From 8e3f94527d530a8a594bcd1d87b9be7bb9236eb1 Mon Sep 17 00:00:00 2001 From: Jari Date: Sat, 18 Oct 2025 17:17:47 +0200 Subject: [PATCH] More operations --- .../octopus/internal/common/models/Entry.kt | 1 + .../internal/db/entities/EntryEntity.kt | 4 +++ .../internal/repositories/EntryRepository.kt | 3 +++ .../repositories/impl/EntryRepositoryImpl.kt | 11 ++++++++ .../octopus/internal/services/EntryService.kt | 3 +++ .../services/impl/EntryServiceImpl.kt | 25 +++++++++++++++++-- .../web/controllers/EntryController.kt | 11 +++++++- .../internal/web/utils/dtos/EntryDto.kt | 9 ++++--- 8 files changed, 61 insertions(+), 6 deletions(-) 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 70621c4..7162f2b 100755 --- a/src/main/kotlin/org/octopus/internal/common/models/Entry.kt +++ b/src/main/kotlin/org/octopus/internal/common/models/Entry.kt @@ -11,5 +11,6 @@ data class Entry( val fixedDate: Date? = null, val recurrent: Boolean? = false, val recurrentMonths: List? = mutableListOf(), + val startDate: Date? = null, val endDate: Date? = null ) \ 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 d6487eb..d5b9436 100644 --- a/src/main/kotlin/org/octopus/internal/db/entities/EntryEntity.kt +++ b/src/main/kotlin/org/octopus/internal/db/entities/EntryEntity.kt @@ -1,10 +1,12 @@ package org.octopus.internal.db.entities +import jakarta.persistence.Convert import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType import jakarta.persistence.Id import org.octopus.internal.common.enums.EEntryType +import org.octopus.internal.db.converters.MonthListConverter import java.time.Month import java.util.Date @@ -16,6 +18,8 @@ data class EntryEntity( val amount: Double, val fixedDate: Date? = null, val recurrent: Boolean? = false, + @field:Convert(converter = MonthListConverter::class) val recurrentMonths: List? = mutableListOf(), + val startDate: Date? = null, val endDate: Date? = null ) \ No newline at end of file diff --git a/src/main/kotlin/org/octopus/internal/repositories/EntryRepository.kt b/src/main/kotlin/org/octopus/internal/repositories/EntryRepository.kt index 3a3cdd5..257c97a 100755 --- a/src/main/kotlin/org/octopus/internal/repositories/EntryRepository.kt +++ b/src/main/kotlin/org/octopus/internal/repositories/EntryRepository.kt @@ -13,4 +13,7 @@ interface EntryRepository { fun getAllRecurrentOnAllMonths(): MutableList fun getAllFixedUpToEndDate(endDate: Date): MutableList fun getAllByType(type: EEntryType): MutableList + + fun deleteById(id: Long): Boolean + fun deleteAll(): Boolean } diff --git a/src/main/kotlin/org/octopus/internal/repositories/impl/EntryRepositoryImpl.kt b/src/main/kotlin/org/octopus/internal/repositories/impl/EntryRepositoryImpl.kt index 3121ade..dc0aed4 100755 --- a/src/main/kotlin/org/octopus/internal/repositories/impl/EntryRepositoryImpl.kt +++ b/src/main/kotlin/org/octopus/internal/repositories/impl/EntryRepositoryImpl.kt @@ -37,6 +37,17 @@ class EntryRepositoryImpl( return mapper.toModels(jpa.findAllByType(type)) } + override fun deleteById(id: Long): Boolean { + jpa.deleteById(id) + return true + + } + + override fun deleteAll(): Boolean { + jpa.deleteAll() + return true + } + override fun getAllRecurrentMonthlyToEndDate(endDate: Date): MutableList { return mapper.toModels( jpa.findAllByFixedDateIsNullAndRecurrentIsTrueAndEndDateIsNotNullAndEndDateBefore(endDate) diff --git a/src/main/kotlin/org/octopus/internal/services/EntryService.kt b/src/main/kotlin/org/octopus/internal/services/EntryService.kt index e2e0043..4042900 100755 --- a/src/main/kotlin/org/octopus/internal/services/EntryService.kt +++ b/src/main/kotlin/org/octopus/internal/services/EntryService.kt @@ -6,4 +6,7 @@ import org.octopus.internal.web.utils.dtos.EntryDto interface EntryService { fun createEntry(entry: EntryDto): Entry fun getAllEntries(): MutableList + fun deleteAll(): Boolean + fun deleteById(id: Long): Boolean + fun editEntry(id: Long, entry: EntryDto): Entry } \ 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 ef168a9..226e6e8 100755 --- a/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt +++ b/src/main/kotlin/org/octopus/internal/services/impl/EntryServiceImpl.kt @@ -22,22 +22,43 @@ class EntryServiceImpl(val repo: EntryRepository) : EntryService { 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) } + recurrentMonths = entry.recurrentMonths?.map { m -> Month.of(m) }?.toList() ?: emptyList() )) } override fun getAllEntries(): MutableList { - TODO("Not yet implemented") + val income = repo.getAllByType(EEntryType.INCOME) + val outcome = repo.getAllByType(EEntryType.OUTCOME) + val invest = repo.getAllByType(EEntryType.INVESTMENT) + return (income + outcome + invest).toMutableList() + } + + override fun deleteAll(): Boolean { + return repo.deleteAll() + } + + override fun deleteById(id: Long): Boolean { + return repo.deleteById(id) + } + + 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) } private fun verifyDto(entry: EntryDto): Boolean { val eventIsRecurring = entry.fixedDate == null && + entry.startDate != null && entry.endDate != null && entry.recurrent == true && entry.recurrentMonths?.isNotEmpty() ?: false val eventIsFixed = entry.fixedDate != null && + entry.startDate == null && entry.endDate == null && entry.recurrent != true && entry.recurrentMonths?.isEmpty() ?: true 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 c853c3d..109719b 100755 --- a/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt +++ b/src/main/kotlin/org/octopus/internal/web/controllers/EntryController.kt @@ -24,10 +24,19 @@ class EntryController( } @GetMapping - fun getAllClients(): WebResponse> { + fun getAllEntries(): WebResponse> { return WebResponse.ok(entryService.getAllEntries()) } + @DeleteMapping + fun deleteAllEntries(): WebResponse { + return WebResponse.ok(entryService.deleteAll()) + } + + @DeleteMapping("/{id}") + fun deleteEntryById(@PathVariable("id") id: Long): WebResponse { + return WebResponse.ok(entryService.deleteById(id)) + } // @GetMapping("/{id}") // fun getClient(@PathVariable("id") id: String, @RequestParam( 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 13cd292..117fca2 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,19 +1,22 @@ 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.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.") @field:EntryTypeValidator.Validate val type: String, - @field:DecimalMin(value="0.00", inclusive = false, message = "Amount value cannot be lower than 0.01") + @field:DecimalMin(value="0.00", inclusive = false, message = "The amount must be greater than 0.00.") val amount: Double, val fixedDate: Date?, val recurrent: Boolean?, @field:EntryMonthValidator.Validate val recurrentMonths: List?, - val endDate: Date? - + val startDate: Date?, + val endDate: Date?, + val color: String = "0xFFFFFF" ) \ No newline at end of file