some refactoring

This commit is contained in:
Denis Savosin
2024-10-02 12:18:55 +07:00
parent 16f43c6172
commit 9b00237657
33 changed files with 109 additions and 82 deletions

View File

@@ -1,13 +1,13 @@
package com.example.demo.config package com.example.demo.config
import com.example.demo.provider.CityRepository import com.example.demo.providers.CityRepository
import com.example.demo.provider.MockedShopProvider import com.example.demo.providers.MockedShopProvider
import com.example.demo.provider.ProductRepository import com.example.demo.providers.ProductRepository
import com.example.demo.provider.ShopProvider import com.example.demo.providers.ShopProvider
import com.example.demo.services.CityService import com.example.demo.services.database.city.CityService
import com.example.demo.services.CityServiceImpl import com.example.demo.services.database.city.CityServiceImpl
import com.example.demo.services.ProductService import com.example.demo.services.database.product.ProductService
import com.example.demo.services.ProductServiceImpl import com.example.demo.services.database.product.ProductServiceImpl
import com.example.demo.services.kafka.Producer import com.example.demo.services.kafka.Producer
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value import org.springframework.beans.factory.annotation.Value

View File

@@ -1,6 +1,6 @@
package com.example.demo.http.controllers package com.example.demo.http.controllers
import com.example.demo.provider.html.renderProductTable import com.example.demo.providers.html.renderProductTable
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController

View File

@@ -4,7 +4,9 @@ import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.http.exceptions.UnprocessableException import com.example.demo.http.exceptions.UnprocessableException
import com.example.demo.http.requests.CreateProductRequest import com.example.demo.http.requests.CreateProductRequest
import com.example.demo.http.responses.makeOkResponse import com.example.demo.http.responses.makeOkResponse
import com.example.demo.services.ProductService 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.kafka.exceptions.InvalidArgumentException import com.example.demo.services.kafka.exceptions.InvalidArgumentException
import jakarta.validation.Valid import jakarta.validation.Valid
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
@@ -65,7 +67,13 @@ class ProductController(
fun deleteProduct( fun deleteProduct(
@PathVariable guid: UUID, @PathVariable guid: UUID,
): ResponseEntity<Any> { ): ResponseEntity<Any> {
productService.delete(guid) try {
productService.delete(guid)
} catch (notFoundException: ProductNotFoundException) {
throw NotFoundException()
} catch (alreadyDeletedException: AlreadyDeletedException) {
throw UnprocessableException("product already deleted")
}
return ResponseEntity(makeOkResponse(), HttpStatus.OK) return ResponseEntity(makeOkResponse(), HttpStatus.OK)
} }

View File

@@ -1,7 +1,7 @@
package com.example.demo.http.controllers package com.example.demo.http.controllers
import com.example.demo.http.exceptions.NotFoundException import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.provider.ShopProvider import com.example.demo.providers.ShopProvider
import jakarta.servlet.http.HttpServletResponse import jakarta.servlet.http.HttpServletResponse
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement import kotlinx.serialization.json.encodeToJsonElement

View File

@@ -1,3 +0,0 @@
package com.example.demo.provider.html
class Center: Tag("center")

View File

@@ -1,3 +0,0 @@
package com.example.demo.provider.html
class TD: Tag("td")

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider package com.example.demo.providers
import com.example.demo.models.City import com.example.demo.models.City
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider package com.example.demo.providers
import com.example.demo.models.* import com.example.demo.models.*
import java.time.OffsetDateTime import java.time.OffsetDateTime

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider package com.example.demo.providers
import com.example.demo.models.Product import com.example.demo.models.Product
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider package com.example.demo.providers
import com.example.demo.models.Shop import com.example.demo.models.Shop

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
class Attribute(val name : String, val value : String) { class Attribute(val name : String, val value : String) {
override fun toString() = """$name="$value" """ override fun toString() = """$name="$value" """

View File

@@ -0,0 +1,3 @@
package com.example.demo.providers.html
class Center: Tag("center")

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
class Html: Tag("html") class Html: Tag("html")

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
fun getTitleColor() = "#b9c9fe" fun getTitleColor() = "#b9c9fe"
fun getCellColor(index: Int, row: Int) = if ((index + row) %2 == 0) "#dce4ff" else "#eff2ff" fun getCellColor(index: Int, row: Int) = if ((index + row) %2 == 0) "#dce4ff" else "#eff2ff"

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
data class InnerProduct(val description: String, val price: Double, val popularity: Int) data class InnerProduct(val description: String, val price: Double, val popularity: Int)

View File

@@ -0,0 +1,3 @@
package com.example.demo.providers.html
class TD: Tag("td")

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
class TR: Tag("tr") class TR: Tag("tr")

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
class Table: Tag("table") class Table: Tag("table")

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
open class Tag(val name: String) { open class Tag(val name: String) {
val children: MutableList<Tag> = ArrayList() val children: MutableList<Tag> = ArrayList()

View File

@@ -1,4 +1,4 @@
package com.example.demo.provider.html package com.example.demo.providers.html
class Text(val text: String): Tag("b") { class Text(val text: String): Tag("b") {
override fun toString() = text override fun toString() = text

View File

@@ -1,17 +0,0 @@
package com.example.demo.services
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.http.exceptions.UnprocessableException
import com.example.demo.models.City
import org.springframework.stereotype.Service
import java.util.*
@Service
interface CityService {
fun findByGuid(guid: UUID): City?
fun create(name: String): City?
@Throws(NotFoundException::class, UnprocessableException::class)
fun delete(guid: UUID): City?
}

View File

@@ -0,0 +1,17 @@
package com.example.demo.services.database.city
import com.example.demo.models.City
import com.example.demo.services.database.exceptions.AlreadyDeletedException
import com.example.demo.services.database.city.exceptions.CityNotFoundException
import org.springframework.stereotype.Service
import java.util.*
@Service
interface CityService {
fun findByGuid(guid: UUID): City?
fun create(name: String): City?
@Throws(CityNotFoundException::class, AlreadyDeletedException::class)
fun delete(guid: UUID): City?
}

View File

@@ -1,9 +1,9 @@
package com.example.demo.services package com.example.demo.services.database.city
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.http.exceptions.UnprocessableException
import com.example.demo.models.City import com.example.demo.models.City
import com.example.demo.provider.CityRepository import com.example.demo.providers.CityRepository
import com.example.demo.services.database.exceptions.AlreadyDeletedException
import com.example.demo.services.database.city.exceptions.CityNotFoundException
import java.time.OffsetDateTime import java.time.OffsetDateTime
import java.util.* import java.util.*
@@ -26,10 +26,10 @@ class CityServiceImpl(
} }
override fun delete(guid: UUID): City? { override fun delete(guid: UUID): City? {
val city = findByGuid(guid) ?: throw NotFoundException() val city = findByGuid(guid) ?: throw CityNotFoundException()
if (city.isDeleted()) { if (city.isDeleted()) {
throw UnprocessableException("city already deleted") throw AlreadyDeletedException()
} }
val deletedCity = city.copy( val deletedCity = city.copy(

View File

@@ -0,0 +1,3 @@
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 AlreadyDeletedException: RuntimeException()

View File

@@ -1,8 +1,8 @@
package com.example.demo.services package com.example.demo.services.database.product
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.http.exceptions.UnprocessableException
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.product.exceptions.ProductNotFoundException
import com.example.demo.services.kafka.exceptions.InvalidArgumentException import com.example.demo.services.kafka.exceptions.InvalidArgumentException
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.util.* import java.util.*
@@ -13,9 +13,9 @@ interface ProductService {
fun create(name: String, price: Long, description: String?): Product fun create(name: String, price: Long, description: String?): Product
@Throws(NotFoundException::class, UnprocessableException::class) @Throws(ProductNotFoundException::class, AlreadyDeletedException::class)
fun delete(guid: UUID): Product? fun delete(guid: UUID): Product?
@Throws(NotFoundException::class, InvalidArgumentException::class) @Throws(ProductNotFoundException::class, InvalidArgumentException::class)
fun syncToKafka(guid: UUID, topic: String?) fun syncToKafka(guid: UUID, topic: String?)
} }

View File

@@ -1,9 +1,9 @@
package com.example.demo.services package com.example.demo.services.database.product
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.http.exceptions.UnprocessableException
import com.example.demo.models.Product import com.example.demo.models.Product
import com.example.demo.provider.ProductRepository 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.kafka.Producer import com.example.demo.services.kafka.Producer
import java.time.OffsetDateTime import java.time.OffsetDateTime
import java.util.* import java.util.*
@@ -31,10 +31,10 @@ class ProductServiceImpl(
} }
override fun delete(guid: UUID): Product? { override fun delete(guid: UUID): Product? {
val product = findByGuid(guid) ?: throw NotFoundException() val product = findByGuid(guid) ?: throw ProductNotFoundException()
if (product.isDeleted()) { if (product.isDeleted()) {
throw UnprocessableException("product already deleted") throw AlreadyDeletedException()
} }
val deletedProduct = product.copy( val deletedProduct = product.copy(
@@ -52,7 +52,7 @@ class ProductServiceImpl(
} }
override fun syncToKafka(guid: UUID, topic: String?) { override fun syncToKafka(guid: UUID, topic: String?) {
val product = findByGuid(guid) ?: throw NotFoundException() val product = findByGuid(guid) ?: throw ProductNotFoundException()
producer.produceProductInfo(topic ?: defaultSyncTopic, product) producer.produceProductInfo(topic ?: defaultSyncTopic, product)
} }

View File

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

View File

@@ -1,9 +1,8 @@
package com.example.demo.http.controllers package com.example.demo.http.controllers
import com.example.demo.http.controllers.ProductController
import com.example.demo.models.Product
import com.example.demo.http.responses.ResponseStatus import com.example.demo.http.responses.ResponseStatus
import com.example.demo.services.ProductService import com.example.demo.models.Product
import com.example.demo.services.database.product.ProductService
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.nullValue import org.hamcrest.Matchers.nullValue

View File

@@ -1,8 +1,7 @@
package com.example.demo.http.controllers package com.example.demo.http.controllers
import com.example.demo.http.controllers.ShopController
import com.example.demo.models.* import com.example.demo.models.*
import com.example.demo.provider.ShopProvider import com.example.demo.providers.ShopProvider
import org.mockito.kotlin.doReturn import org.mockito.kotlin.doReturn
import org.mockito.kotlin.whenever import org.mockito.kotlin.whenever
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired

View File

@@ -1,10 +1,14 @@
package com.example.demo.services package com.example.demo.services.database.city
import com.example.demo.BaseFeatureTest import com.example.demo.BaseFeatureTest
import com.example.demo.models.City import com.example.demo.models.City
import com.example.demo.provider.CityRepository import com.example.demo.providers.CityRepository
import com.example.demo.services.database.exceptions.AlreadyDeletedException
import com.example.demo.services.database.city.exceptions.CityNotFoundException
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
import java.util.*
import kotlin.test.* import kotlin.test.*
@ContextConfiguration(classes = [CityRepository::class, CityServiceImpl::class]) @ContextConfiguration(classes = [CityRepository::class, CityServiceImpl::class])
@@ -35,6 +39,14 @@ class CityServiceImplTest: BaseFeatureTest() {
assertEquals(city.id, deletedCity.id) assertEquals(city.id, deletedCity.id)
assertNotNull(deletedCity.deletedAt) assertNotNull(deletedCity.deletedAt)
assertTrue(deletedCity.isDeleted()) assertTrue(deletedCity.isDeleted())
assertThrows<AlreadyDeletedException> {
cityServiceImpl.delete(city.guid)
}
assertThrows<CityNotFoundException> {
cityServiceImpl.delete(UUID.randomUUID())
}
} finally { } finally {
val id = city?.id val id = city?.id
if (id != null) { if (id != null) {

View File

@@ -1,10 +1,10 @@
package com.example.demo.services package com.example.demo.services.database.product
import com.example.demo.BaseFeatureTest import com.example.demo.BaseFeatureTest
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.http.exceptions.UnprocessableException
import com.example.demo.models.Product import com.example.demo.models.Product
import com.example.demo.provider.ProductRepository import com.example.demo.providers.ProductRepository
import com.example.demo.services.database.exceptions.AlreadyDeletedException
import com.example.demo.services.database.product.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
@@ -52,11 +52,11 @@ class ProductServiceImplFeatureTest: BaseFeatureTest() {
assertTrue(deletedProduct.isDeleted()) assertTrue(deletedProduct.isDeleted())
// try to delete already deleted product // try to delete already deleted product
assertThrows<UnprocessableException> { assertThrows<AlreadyDeletedException> {
productService.delete(product.guid) productService.delete(product.guid)
} }
assertThrows<NotFoundException> { assertThrows<ProductNotFoundException> {
productService.delete(UUID.randomUUID()) productService.delete(UUID.randomUUID())
} }
} finally { } finally {

View File

@@ -1,10 +1,10 @@
package com.example.demo.services package com.example.demo.services.database.product
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.models.Product import com.example.demo.models.Product
import com.example.demo.provider.ProductRepository import com.example.demo.providers.ProductRepository
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 com.example.demo.services.database.product.exceptions.ProductNotFoundException
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.kotlin.* import org.mockito.kotlin.*
@@ -67,7 +67,7 @@ class ProductServiceImplTest {
whenever(productRepository.findByGuid(eq(guid))) whenever(productRepository.findByGuid(eq(guid)))
.thenReturn(null) .thenReturn(null)
assertThrows<NotFoundException> { assertThrows<ProductNotFoundException> {
productService.syncToKafka(guid, specificTopic) productService.syncToKafka(guid, specificTopic)
} }