mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +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.models.City
|
||||||
import com.github.dannecron.demo.providers.CityRepository
|
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.AlreadyDeletedException
|
||||||
|
import com.github.dannecron.demo.services.database.exceptions.CityNotFoundException
|
||||||
import com.github.dannecron.demo.services.kafka.dto.CityCreateDto
|
import com.github.dannecron.demo.services.kafka.dto.CityCreateDto
|
||||||
import java.time.OffsetDateTime
|
import java.time.OffsetDateTime
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
@@ -14,33 +14,30 @@ class CityServiceImpl(
|
|||||||
): CityService {
|
): CityService {
|
||||||
override fun findByGuid(guid: UUID): City? = cityRepository.findByGuid(guid)
|
override fun findByGuid(guid: UUID): City? = cityRepository.findByGuid(guid)
|
||||||
|
|
||||||
override fun create(name: String): City {
|
override fun create(name: String): City = City(
|
||||||
val city = City(
|
id = null,
|
||||||
id = null,
|
guid = UUID.randomUUID(),
|
||||||
guid = UUID.randomUUID(),
|
name = name,
|
||||||
name = name,
|
createdAt = OffsetDateTime.now(),
|
||||||
createdAt = OffsetDateTime.now(),
|
updatedAt = null,
|
||||||
updatedAt = null,
|
deletedAt = null,
|
||||||
deletedAt = null,
|
).let {
|
||||||
)
|
cityRepository.save(it)
|
||||||
|
|
||||||
return cityRepository.save(city)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun create(kafkaCityDto: CityCreateDto): City {
|
override fun create(kafkaCityDto: CityCreateDto): City = City(
|
||||||
val updatedAt = kafkaCityDto.updatedAt
|
id = null,
|
||||||
val deletedAt = kafkaCityDto.deletedAt
|
guid = UUID.fromString(kafkaCityDto.guid),
|
||||||
|
name = kafkaCityDto.name,
|
||||||
val city = City(
|
createdAt = OffsetDateTime.parse(kafkaCityDto.createdAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME),
|
||||||
id = null,
|
updatedAt = kafkaCityDto.deletedAt?.let {
|
||||||
guid = UUID.fromString(kafkaCityDto.guid),
|
OffsetDateTime.parse(it, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
|
||||||
name = kafkaCityDto.name,
|
},
|
||||||
createdAt = OffsetDateTime.parse(kafkaCityDto.createdAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME),
|
deletedAt = kafkaCityDto.deletedAt?.let {
|
||||||
updatedAt = if (updatedAt != null) OffsetDateTime.parse(updatedAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME) else null,
|
OffsetDateTime.parse(it, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
|
||||||
deletedAt = if (deletedAt != null) OffsetDateTime.parse(deletedAt, DateTimeFormatter.ISO_OFFSET_DATE_TIME) else null,
|
},
|
||||||
)
|
).let {
|
||||||
|
cityRepository.save(it)
|
||||||
return cityRepository.save(city)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun delete(guid: UUID): City {
|
override fun delete(guid: UUID): City {
|
||||||
@@ -50,10 +47,10 @@ class CityServiceImpl(
|
|||||||
throw AlreadyDeletedException()
|
throw AlreadyDeletedException()
|
||||||
}
|
}
|
||||||
|
|
||||||
val deletedCity = city.copy(
|
return cityRepository.save(
|
||||||
deletedAt = OffsetDateTime.now(),
|
city.copy(
|
||||||
|
deletedAt = OffsetDateTime.now(),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return cityRepository.save(deletedCity)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,32 +12,24 @@ class CustomerServiceImpl(
|
|||||||
private val customerRepository: CustomerRepository,
|
private val customerRepository: CustomerRepository,
|
||||||
private val cityRepository: CityRepository
|
private val cityRepository: CityRepository
|
||||||
): CustomerService {
|
): CustomerService {
|
||||||
override fun findByGuid(guid: UUID): CustomerExtended? {
|
override fun findByGuid(guid: UUID): CustomerExtended? = customerRepository.findByGuid(guid)
|
||||||
val customer = customerRepository.findByGuid(guid) ?: return null
|
?.let {
|
||||||
|
customer -> CustomerExtended(
|
||||||
if (customer.cityId == null) {
|
customer = customer,
|
||||||
return CustomerExtended(customer, null)
|
city = customer.cityId?.let { cityId -> cityRepository.findById(cityId).orElse(null) }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
val city = cityRepository.findById(customer.cityId)
|
override fun create(name: String, cityGuid: UUID?): Customer = Customer(
|
||||||
|
id = null,
|
||||||
return CustomerExtended(customer, city.orElse(null))
|
guid = UUID.randomUUID(),
|
||||||
}
|
name = name,
|
||||||
|
cityId = cityGuid?.let {
|
||||||
override fun create(name: String, cityGuid: UUID?): Customer {
|
|
||||||
val cityId: Long? = cityGuid?.let {
|
|
||||||
cityRepository.findByGuid(it)?.id ?: throw CityNotFoundException()
|
cityRepository.findByGuid(it)?.id ?: throw CityNotFoundException()
|
||||||
}
|
},
|
||||||
|
createdAt = OffsetDateTime.now(),
|
||||||
val customer = Customer(
|
updatedAt = null,
|
||||||
id = null,
|
).let {
|
||||||
guid = UUID.randomUUID(),
|
customerRepository.save(it)
|
||||||
name = name,
|
|
||||||
cityId = cityId,
|
|
||||||
createdAt = OffsetDateTime.now(),
|
|
||||||
updatedAt = null,
|
|
||||||
)
|
|
||||||
|
|
||||||
return customerRepository.save(customer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user