From 95a3cafbad2ecfe04c9670ed75a519c5502de6bb Mon Sep 17 00:00:00 2001 From: Denis Savosin Date: Tue, 15 Oct 2024 13:51:28 +0700 Subject: [PATCH] refactor services --- .../services/database/city/CityServiceImpl.kt | 57 +++++++++---------- .../database/customer/CustomerServiceImpl.kt | 40 ++++++------- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/main/kotlin/com/github/dannecron/demo/services/database/city/CityServiceImpl.kt b/src/main/kotlin/com/github/dannecron/demo/services/database/city/CityServiceImpl.kt index 4f08cd2..60d5ec5 100644 --- a/src/main/kotlin/com/github/dannecron/demo/services/database/city/CityServiceImpl.kt +++ b/src/main/kotlin/com/github/dannecron/demo/services/database/city/CityServiceImpl.kt @@ -2,8 +2,8 @@ package com.github.dannecron.demo.services.database.city import com.github.dannecron.demo.models.City import com.github.dannecron.demo.providers.CityRepository -import com.github.dannecron.demo.services.database.exceptions.CityNotFoundException import com.github.dannecron.demo.services.database.exceptions.AlreadyDeletedException +import com.github.dannecron.demo.services.database.exceptions.CityNotFoundException import com.github.dannecron.demo.services.kafka.dto.CityCreateDto import java.time.OffsetDateTime import java.time.format.DateTimeFormatter @@ -14,33 +14,30 @@ class CityServiceImpl( ): CityService { override fun findByGuid(guid: UUID): City? = cityRepository.findByGuid(guid) - override fun create(name: String): City { - val city = City( - id = null, - guid = UUID.randomUUID(), - name = name, - createdAt = OffsetDateTime.now(), - updatedAt = null, - deletedAt = null, - ) - - return cityRepository.save(city) + override fun create(name: String): City = City( + id = null, + guid = UUID.randomUUID(), + name = name, + createdAt = OffsetDateTime.now(), + updatedAt = null, + deletedAt = null, + ).let { + cityRepository.save(it) } - override fun create(kafkaCityDto: CityCreateDto): City { - val updatedAt = kafkaCityDto.updatedAt - val deletedAt = kafkaCityDto.deletedAt - - val city = City( - id = null, - guid = UUID.fromString(kafkaCityDto.guid), - name = kafkaCityDto.name, - createdAt = OffsetDateTime.parse(kafkaCityDto.createdAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME), - updatedAt = if (updatedAt != null) OffsetDateTime.parse(updatedAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME) else null, - deletedAt = if (deletedAt != null) OffsetDateTime.parse(deletedAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME) else null, - ) - - return cityRepository.save(city) + override fun create(kafkaCityDto: CityCreateDto): City = City( + id = null, + guid = UUID.fromString(kafkaCityDto.guid), + name = kafkaCityDto.name, + createdAt = OffsetDateTime.parse(kafkaCityDto.createdAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME), + updatedAt = kafkaCityDto.deletedAt?.let { + OffsetDateTime.parse(it, DateTimeFormatter.ISO_OFFSET_DATE_TIME) + }, + deletedAt = kafkaCityDto.deletedAt?.let { + OffsetDateTime.parse(it, DateTimeFormatter.ISO_OFFSET_DATE_TIME) + }, + ).let { + cityRepository.save(it) } override fun delete(guid: UUID): City { @@ -50,10 +47,10 @@ class CityServiceImpl( throw AlreadyDeletedException() } - val deletedCity = city.copy( - deletedAt = OffsetDateTime.now(), + return cityRepository.save( + city.copy( + deletedAt = OffsetDateTime.now(), + ) ) - - return cityRepository.save(deletedCity) } } diff --git a/src/main/kotlin/com/github/dannecron/demo/services/database/customer/CustomerServiceImpl.kt b/src/main/kotlin/com/github/dannecron/demo/services/database/customer/CustomerServiceImpl.kt index 95b156a..4ead70c 100644 --- a/src/main/kotlin/com/github/dannecron/demo/services/database/customer/CustomerServiceImpl.kt +++ b/src/main/kotlin/com/github/dannecron/demo/services/database/customer/CustomerServiceImpl.kt @@ -12,32 +12,24 @@ class CustomerServiceImpl( private val customerRepository: CustomerRepository, private val cityRepository: CityRepository ): CustomerService { - override fun findByGuid(guid: UUID): CustomerExtended? { - val customer = customerRepository.findByGuid(guid) ?: return null - - if (customer.cityId == null) { - return CustomerExtended(customer, null) + override fun findByGuid(guid: UUID): CustomerExtended? = customerRepository.findByGuid(guid) + ?.let { + customer -> CustomerExtended( + customer = customer, + city = customer.cityId?.let { cityId -> cityRepository.findById(cityId).orElse(null) } + ) } - val city = cityRepository.findById(customer.cityId) - - return CustomerExtended(customer, city.orElse(null)) - } - - override fun create(name: String, cityGuid: UUID?): Customer { - val cityId: Long? = cityGuid?.let { + override fun create(name: String, cityGuid: UUID?): Customer = Customer( + id = null, + guid = UUID.randomUUID(), + name = name, + cityId = cityGuid?.let { cityRepository.findByGuid(it)?.id ?: throw CityNotFoundException() - } - - val customer = Customer( - id = null, - guid = UUID.randomUUID(), - name = name, - cityId = cityId, - createdAt = OffsetDateTime.now(), - updatedAt = null, - ) - - return customerRepository.save(customer) + }, + createdAt = OffsetDateTime.now(), + updatedAt = null, + ).let { + customerRepository.save(it) } }