refactor services exceptions

This commit is contained in:
Denis Savosin
2024-10-14 10:03:53 +07:00
parent 3fbc94553d
commit 25fb73ffa4
13 changed files with 33 additions and 30 deletions

View File

@@ -9,7 +9,7 @@ import com.example.demo.http.responses.page.PageResponse
import com.example.demo.models.Product
import com.example.demo.services.database.exceptions.AlreadyDeletedException
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 io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
@@ -107,4 +107,4 @@ class ProductController(
return ResponseEntity(makeOkResponse(), HttpStatus.OK)
}
}
}

View File

@@ -1,7 +1,7 @@
package com.example.demo.services.database.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.kafka.dto.CityCreateDto
import org.springframework.stereotype.Service
@@ -11,9 +11,9 @@ import java.util.*
interface CityService {
fun findByGuid(guid: UUID): City?
fun create(name: String): City?
fun create(kafkaCityDto: CityCreateDto): City?
fun create(name: String): City
fun create(kafkaCityDto: CityCreateDto): City
@Throws(CityNotFoundException::class, AlreadyDeletedException::class)
fun delete(guid: UUID): City?
}
fun delete(guid: UUID): City
}

View File

@@ -2,7 +2,7 @@ package com.example.demo.services.database.city
import com.example.demo.models.City
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.kafka.dto.CityCreateDto
import java.time.OffsetDateTime
@@ -14,7 +14,7 @@ class CityServiceImpl(
): CityService {
override fun findByGuid(guid: UUID): City? = cityRepository.findByGuid(guid)
override fun create(name: String): City? {
override fun create(name: String): City {
val city = City(
id = null,
guid = UUID.randomUUID(),
@@ -27,7 +27,7 @@ class CityServiceImpl(
return cityRepository.save(city)
}
override fun create(kafkaCityDto: CityCreateDto): City? {
override fun create(kafkaCityDto: CityCreateDto): City {
val updatedAt = kafkaCityDto.updatedAt
val deletedAt = kafkaCityDto.deletedAt
@@ -43,7 +43,7 @@ class CityServiceImpl(
return cityRepository.save(city)
}
override fun delete(guid: UUID): City? {
override fun delete(guid: UUID): City {
val city = findByGuid(guid) ?: throw CityNotFoundException()
if (city.isDeleted()) {
@@ -56,4 +56,4 @@ class CityServiceImpl(
return cityRepository.save(deletedCity)
}
}
}

View File

@@ -1,3 +0,0 @@
package com.example.demo.services.database.city.exceptions
class CityNotFoundException: RuntimeException()

View File

@@ -0,0 +1,3 @@
package com.example.demo.services.database.exceptions
class CityNotFoundException: ModelNotFoundException("city")

View File

@@ -0,0 +1,3 @@
package com.example.demo.services.database.exceptions
open class ModelNotFoundException(entityName: String): RuntimeException("$entityName not found")

View File

@@ -0,0 +1,3 @@
package com.example.demo.services.database.exceptions
class ProductNotFoundException: ModelNotFoundException("product")

View File

@@ -2,7 +2,7 @@ package com.example.demo.services.database.product
import com.example.demo.models.Product
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 org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
@@ -18,8 +18,8 @@ interface ProductService {
fun create(name: String, price: Long, description: String?): Product
@Throws(ProductNotFoundException::class, AlreadyDeletedException::class)
fun delete(guid: UUID): Product?
fun delete(guid: UUID): Product
@Throws(ProductNotFoundException::class, InvalidArgumentException::class)
fun syncToKafka(guid: UUID, topic: String?)
}
}

View File

@@ -3,7 +3,7 @@ package com.example.demo.services.database.product
import com.example.demo.models.Product
import com.example.demo.providers.ProductRepository
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.utils.LoggerDelegate
import net.logstash.logback.marker.Markers
@@ -44,7 +44,7 @@ class ProductServiceImpl(
return productRepository.save(product)
}
override fun delete(guid: UUID): Product? {
override fun delete(guid: UUID): Product {
val product = findByGuid(guid) ?: throw ProductNotFoundException()
if (product.isDeleted()) {
@@ -64,4 +64,4 @@ class ProductServiceImpl(
producer.produceProductInfo(topic ?: defaultSyncTopic, product)
}
}
}

View File

@@ -1,3 +0,0 @@
package com.example.demo.services.database.product.exceptions
class ProductNotFoundException: RuntimeException()

View File

@@ -3,7 +3,7 @@ package com.example.demo.services.database.city
import com.example.demo.BaseDbTest
import com.example.demo.models.City
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 org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
@@ -55,4 +55,4 @@ class CityServiceImplDbTest: BaseDbTest() {
}
}
}
}

View File

@@ -4,7 +4,7 @@ import com.example.demo.BaseDbTest
import com.example.demo.models.Product
import com.example.demo.providers.ProductRepository
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.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
@@ -66,4 +66,4 @@ class ProductServiceImplDbTest: BaseDbTest() {
}
}
}
}
}

View File

@@ -3,7 +3,7 @@ package com.example.demo.services.database.product
import com.example.demo.BaseUnitTest
import com.example.demo.models.Product
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.exceptions.InvalidArgumentException
import org.junit.jupiter.api.assertThrows
@@ -96,4 +96,4 @@ class ProductServiceImplTest: BaseUnitTest() {
productService.syncToKafka(guid, specificTopic)
}
}
}
}