mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
refactor services exceptions
This commit is contained in:
@@ -9,7 +9,7 @@ import com.example.demo.http.responses.page.PageResponse
|
|||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import com.example.demo.services.database.product.ProductService
|
import com.example.demo.services.database.product.ProductService
|
||||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
import com.example.demo.services.database.exceptions.ProductNotFoundException
|
||||||
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
||||||
import io.swagger.v3.oas.annotations.media.Content
|
import io.swagger.v3.oas.annotations.media.Content
|
||||||
import io.swagger.v3.oas.annotations.media.Schema
|
import io.swagger.v3.oas.annotations.media.Schema
|
||||||
@@ -107,4 +107,4 @@ class ProductController(
|
|||||||
|
|
||||||
return ResponseEntity(makeOkResponse(), HttpStatus.OK)
|
return ResponseEntity(makeOkResponse(), HttpStatus.OK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.example.demo.services.database.city
|
package com.example.demo.services.database.city
|
||||||
|
|
||||||
import com.example.demo.models.City
|
import com.example.demo.models.City
|
||||||
import com.example.demo.services.database.city.exceptions.CityNotFoundException
|
import com.example.demo.services.database.exceptions.CityNotFoundException
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import com.example.demo.services.kafka.dto.CityCreateDto
|
import com.example.demo.services.kafka.dto.CityCreateDto
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
@@ -11,9 +11,9 @@ import java.util.*
|
|||||||
interface CityService {
|
interface CityService {
|
||||||
fun findByGuid(guid: UUID): City?
|
fun findByGuid(guid: UUID): City?
|
||||||
|
|
||||||
fun create(name: String): City?
|
fun create(name: String): City
|
||||||
fun create(kafkaCityDto: CityCreateDto): City?
|
fun create(kafkaCityDto: CityCreateDto): City
|
||||||
|
|
||||||
@Throws(CityNotFoundException::class, AlreadyDeletedException::class)
|
@Throws(CityNotFoundException::class, AlreadyDeletedException::class)
|
||||||
fun delete(guid: UUID): City?
|
fun delete(guid: UUID): City
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.example.demo.services.database.city
|
|||||||
|
|
||||||
import com.example.demo.models.City
|
import com.example.demo.models.City
|
||||||
import com.example.demo.providers.CityRepository
|
import com.example.demo.providers.CityRepository
|
||||||
import com.example.demo.services.database.city.exceptions.CityNotFoundException
|
import com.example.demo.services.database.exceptions.CityNotFoundException
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import com.example.demo.services.kafka.dto.CityCreateDto
|
import com.example.demo.services.kafka.dto.CityCreateDto
|
||||||
import java.time.OffsetDateTime
|
import java.time.OffsetDateTime
|
||||||
@@ -14,7 +14,7 @@ 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 {
|
||||||
val city = City(
|
val city = City(
|
||||||
id = null,
|
id = null,
|
||||||
guid = UUID.randomUUID(),
|
guid = UUID.randomUUID(),
|
||||||
@@ -27,7 +27,7 @@ class CityServiceImpl(
|
|||||||
return cityRepository.save(city)
|
return cityRepository.save(city)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun create(kafkaCityDto: CityCreateDto): City? {
|
override fun create(kafkaCityDto: CityCreateDto): City {
|
||||||
val updatedAt = kafkaCityDto.updatedAt
|
val updatedAt = kafkaCityDto.updatedAt
|
||||||
val deletedAt = kafkaCityDto.deletedAt
|
val deletedAt = kafkaCityDto.deletedAt
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ class CityServiceImpl(
|
|||||||
return cityRepository.save(city)
|
return cityRepository.save(city)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun delete(guid: UUID): City? {
|
override fun delete(guid: UUID): City {
|
||||||
val city = findByGuid(guid) ?: throw CityNotFoundException()
|
val city = findByGuid(guid) ?: throw CityNotFoundException()
|
||||||
|
|
||||||
if (city.isDeleted()) {
|
if (city.isDeleted()) {
|
||||||
@@ -56,4 +56,4 @@ class CityServiceImpl(
|
|||||||
|
|
||||||
return cityRepository.save(deletedCity)
|
return cityRepository.save(deletedCity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
package com.example.demo.services.database.city.exceptions
|
|
||||||
|
|
||||||
class CityNotFoundException: RuntimeException()
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.example.demo.services.database.exceptions
|
||||||
|
|
||||||
|
class CityNotFoundException: ModelNotFoundException("city")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.example.demo.services.database.exceptions
|
||||||
|
|
||||||
|
open class ModelNotFoundException(entityName: String): RuntimeException("$entityName not found")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.example.demo.services.database.exceptions
|
||||||
|
|
||||||
|
class ProductNotFoundException: ModelNotFoundException("product")
|
||||||
@@ -2,7 +2,7 @@ package com.example.demo.services.database.product
|
|||||||
|
|
||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
import com.example.demo.services.database.exceptions.ProductNotFoundException
|
||||||
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
||||||
import org.springframework.data.domain.Page
|
import org.springframework.data.domain.Page
|
||||||
import org.springframework.data.domain.Pageable
|
import org.springframework.data.domain.Pageable
|
||||||
@@ -18,8 +18,8 @@ interface ProductService {
|
|||||||
fun create(name: String, price: Long, description: String?): Product
|
fun create(name: String, price: Long, description: String?): Product
|
||||||
|
|
||||||
@Throws(ProductNotFoundException::class, AlreadyDeletedException::class)
|
@Throws(ProductNotFoundException::class, AlreadyDeletedException::class)
|
||||||
fun delete(guid: UUID): Product?
|
fun delete(guid: UUID): Product
|
||||||
|
|
||||||
@Throws(ProductNotFoundException::class, InvalidArgumentException::class)
|
@Throws(ProductNotFoundException::class, InvalidArgumentException::class)
|
||||||
fun syncToKafka(guid: UUID, topic: String?)
|
fun syncToKafka(guid: UUID, topic: String?)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.example.demo.services.database.product
|
|||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.providers.ProductRepository
|
import com.example.demo.providers.ProductRepository
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
import com.example.demo.services.database.exceptions.ProductNotFoundException
|
||||||
import com.example.demo.services.kafka.Producer
|
import com.example.demo.services.kafka.Producer
|
||||||
import com.example.demo.utils.LoggerDelegate
|
import com.example.demo.utils.LoggerDelegate
|
||||||
import net.logstash.logback.marker.Markers
|
import net.logstash.logback.marker.Markers
|
||||||
@@ -44,7 +44,7 @@ class ProductServiceImpl(
|
|||||||
return productRepository.save(product)
|
return productRepository.save(product)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun delete(guid: UUID): Product? {
|
override fun delete(guid: UUID): Product {
|
||||||
val product = findByGuid(guid) ?: throw ProductNotFoundException()
|
val product = findByGuid(guid) ?: throw ProductNotFoundException()
|
||||||
|
|
||||||
if (product.isDeleted()) {
|
if (product.isDeleted()) {
|
||||||
@@ -64,4 +64,4 @@ class ProductServiceImpl(
|
|||||||
producer.produceProductInfo(topic ?: defaultSyncTopic, product)
|
producer.produceProductInfo(topic ?: defaultSyncTopic, product)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
package com.example.demo.services.database.product.exceptions
|
|
||||||
|
|
||||||
class ProductNotFoundException: RuntimeException()
|
|
||||||
@@ -3,7 +3,7 @@ package com.example.demo.services.database.city
|
|||||||
import com.example.demo.BaseDbTest
|
import com.example.demo.BaseDbTest
|
||||||
import com.example.demo.models.City
|
import com.example.demo.models.City
|
||||||
import com.example.demo.providers.CityRepository
|
import com.example.demo.providers.CityRepository
|
||||||
import com.example.demo.services.database.city.exceptions.CityNotFoundException
|
import com.example.demo.services.database.exceptions.CityNotFoundException
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
@@ -55,4 +55,4 @@ class CityServiceImplDbTest: BaseDbTest() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.example.demo.BaseDbTest
|
|||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.providers.ProductRepository
|
import com.example.demo.providers.ProductRepository
|
||||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
import com.example.demo.services.database.exceptions.ProductNotFoundException
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.test.context.ContextConfiguration
|
import org.springframework.test.context.ContextConfiguration
|
||||||
@@ -66,4 +66,4 @@ class ProductServiceImplDbTest: BaseDbTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.example.demo.services.database.product
|
|||||||
import com.example.demo.BaseUnitTest
|
import com.example.demo.BaseUnitTest
|
||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.providers.ProductRepository
|
import com.example.demo.providers.ProductRepository
|
||||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
import com.example.demo.services.database.exceptions.ProductNotFoundException
|
||||||
import com.example.demo.services.kafka.Producer
|
import com.example.demo.services.kafka.Producer
|
||||||
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
@@ -96,4 +96,4 @@ class ProductServiceImplTest: BaseUnitTest() {
|
|||||||
productService.syncToKafka(guid, specificTopic)
|
productService.syncToKafka(guid, specificTopic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user