mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
refactor services
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user