mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
some refactoring
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package com.example.demo.config
|
||||
|
||||
import com.example.demo.provider.CityRepository
|
||||
import com.example.demo.provider.MockedShopProvider
|
||||
import com.example.demo.provider.ProductRepository
|
||||
import com.example.demo.provider.ShopProvider
|
||||
import com.example.demo.services.CityService
|
||||
import com.example.demo.services.CityServiceImpl
|
||||
import com.example.demo.services.ProductService
|
||||
import com.example.demo.services.ProductServiceImpl
|
||||
import com.example.demo.providers.CityRepository
|
||||
import com.example.demo.providers.MockedShopProvider
|
||||
import com.example.demo.providers.ProductRepository
|
||||
import com.example.demo.providers.ShopProvider
|
||||
import com.example.demo.services.database.city.CityService
|
||||
import com.example.demo.services.database.city.CityServiceImpl
|
||||
import com.example.demo.services.database.product.ProductService
|
||||
import com.example.demo.services.database.product.ProductServiceImpl
|
||||
import com.example.demo.services.kafka.Producer
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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.ResponseBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@@ -4,7 +4,9 @@ import com.example.demo.http.exceptions.NotFoundException
|
||||
import com.example.demo.http.exceptions.UnprocessableException
|
||||
import com.example.demo.http.requests.CreateProductRequest
|
||||
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 jakarta.validation.Valid
|
||||
import org.springframework.http.HttpStatus
|
||||
@@ -65,7 +67,13 @@ class ProductController(
|
||||
fun deleteProduct(
|
||||
@PathVariable guid: UUID,
|
||||
): 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)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.example.demo.http.controllers
|
||||
|
||||
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 kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.encodeToJsonElement
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.example.demo.provider.html
|
||||
|
||||
class Center: Tag("center")
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.example.demo.provider.html
|
||||
|
||||
class TD: Tag("td")
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider
|
||||
package com.example.demo.providers
|
||||
|
||||
import com.example.demo.models.City
|
||||
import org.springframework.data.jdbc.repository.query.Query
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider
|
||||
package com.example.demo.providers
|
||||
|
||||
import com.example.demo.models.*
|
||||
import java.time.OffsetDateTime
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider
|
||||
package com.example.demo.providers
|
||||
|
||||
import com.example.demo.models.Product
|
||||
import org.springframework.data.jdbc.repository.query.Query
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider
|
||||
package com.example.demo.providers
|
||||
|
||||
import com.example.demo.models.Shop
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class Attribute(val name : String, val value : String) {
|
||||
override fun toString() = """$name="$value" """
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class Center: Tag("center")
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class Html: Tag("html")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
fun getTitleColor() = "#b9c9fe"
|
||||
fun getCellColor(index: Int, row: Int) = if ((index + row) %2 == 0) "#dce4ff" else "#eff2ff"
|
||||
@@ -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)
|
||||
|
||||
3
src/main/kotlin/com/example/demo/providers/html/TD.kt
Normal file
3
src/main/kotlin/com/example/demo/providers/html/TD.kt
Normal file
@@ -0,0 +1,3 @@
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class TD: Tag("td")
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class TR: Tag("tr")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class Table: Tag("table")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
open class Tag(val name: String) {
|
||||
val children: MutableList<Tag> = ArrayList()
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.demo.provider.html
|
||||
package com.example.demo.providers.html
|
||||
|
||||
class Text(val text: String): Tag("b") {
|
||||
override fun toString() = text
|
||||
@@ -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?
|
||||
}
|
||||
@@ -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?
|
||||
}
|
||||
@@ -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.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.util.*
|
||||
|
||||
@@ -26,10 +26,10 @@ class CityServiceImpl(
|
||||
}
|
||||
|
||||
override fun delete(guid: UUID): City? {
|
||||
val city = findByGuid(guid) ?: throw NotFoundException()
|
||||
val city = findByGuid(guid) ?: throw CityNotFoundException()
|
||||
|
||||
if (city.isDeleted()) {
|
||||
throw UnprocessableException("city already deleted")
|
||||
throw AlreadyDeletedException()
|
||||
}
|
||||
|
||||
val deletedCity = city.copy(
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.example.demo.services.database.city.exceptions
|
||||
|
||||
class CityNotFoundException: RuntimeException()
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.example.demo.services.database.exceptions
|
||||
|
||||
class AlreadyDeletedException: RuntimeException()
|
||||
@@ -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.services.database.exceptions.AlreadyDeletedException
|
||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
||||
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
||||
import org.springframework.stereotype.Service
|
||||
import java.util.*
|
||||
@@ -13,9 +13,9 @@ interface ProductService {
|
||||
|
||||
fun create(name: String, price: Long, description: String?): Product
|
||||
|
||||
@Throws(NotFoundException::class, UnprocessableException::class)
|
||||
@Throws(ProductNotFoundException::class, AlreadyDeletedException::class)
|
||||
fun delete(guid: UUID): Product?
|
||||
|
||||
@Throws(NotFoundException::class, InvalidArgumentException::class)
|
||||
@Throws(ProductNotFoundException::class, InvalidArgumentException::class)
|
||||
fun syncToKafka(guid: UUID, topic: String?)
|
||||
}
|
||||
@@ -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.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 java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
@@ -31,10 +31,10 @@ class ProductServiceImpl(
|
||||
}
|
||||
|
||||
override fun delete(guid: UUID): Product? {
|
||||
val product = findByGuid(guid) ?: throw NotFoundException()
|
||||
val product = findByGuid(guid) ?: throw ProductNotFoundException()
|
||||
|
||||
if (product.isDeleted()) {
|
||||
throw UnprocessableException("product already deleted")
|
||||
throw AlreadyDeletedException()
|
||||
}
|
||||
|
||||
val deletedProduct = product.copy(
|
||||
@@ -52,7 +52,7 @@ class ProductServiceImpl(
|
||||
}
|
||||
|
||||
override fun syncToKafka(guid: UUID, topic: String?) {
|
||||
val product = findByGuid(guid) ?: throw NotFoundException()
|
||||
val product = findByGuid(guid) ?: throw ProductNotFoundException()
|
||||
|
||||
producer.produceProductInfo(topic ?: defaultSyncTopic, product)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.example.demo.services.database.product.exceptions
|
||||
|
||||
class ProductNotFoundException: RuntimeException()
|
||||
@@ -1,9 +1,8 @@
|
||||
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.services.ProductService
|
||||
import com.example.demo.models.Product
|
||||
import com.example.demo.services.database.product.ProductService
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import org.hamcrest.Matchers.contains
|
||||
import org.hamcrest.Matchers.nullValue
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.example.demo.http.controllers
|
||||
|
||||
import com.example.demo.http.controllers.ShopController
|
||||
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.whenever
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
|
||||
@@ -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.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.test.context.ContextConfiguration
|
||||
import java.util.*
|
||||
import kotlin.test.*
|
||||
|
||||
@ContextConfiguration(classes = [CityRepository::class, CityServiceImpl::class])
|
||||
@@ -35,6 +39,14 @@ class CityServiceImplTest: BaseFeatureTest() {
|
||||
assertEquals(city.id, deletedCity.id)
|
||||
assertNotNull(deletedCity.deletedAt)
|
||||
assertTrue(deletedCity.isDeleted())
|
||||
|
||||
assertThrows<AlreadyDeletedException> {
|
||||
cityServiceImpl.delete(city.guid)
|
||||
}
|
||||
|
||||
assertThrows<CityNotFoundException> {
|
||||
cityServiceImpl.delete(UUID.randomUUID())
|
||||
}
|
||||
} finally {
|
||||
val id = city?.id
|
||||
if (id != null) {
|
||||
@@ -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.http.exceptions.NotFoundException
|
||||
import com.example.demo.http.exceptions.UnprocessableException
|
||||
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.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
@@ -52,11 +52,11 @@ class ProductServiceImplFeatureTest: BaseFeatureTest() {
|
||||
assertTrue(deletedProduct.isDeleted())
|
||||
|
||||
// try to delete already deleted product
|
||||
assertThrows<UnprocessableException> {
|
||||
assertThrows<AlreadyDeletedException> {
|
||||
productService.delete(product.guid)
|
||||
}
|
||||
|
||||
assertThrows<NotFoundException> {
|
||||
assertThrows<ProductNotFoundException> {
|
||||
productService.delete(UUID.randomUUID())
|
||||
}
|
||||
} finally {
|
||||
@@ -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.provider.ProductRepository
|
||||
import com.example.demo.providers.ProductRepository
|
||||
import com.example.demo.services.kafka.Producer
|
||||
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.runner.RunWith
|
||||
import org.mockito.kotlin.*
|
||||
@@ -67,7 +67,7 @@ class ProductServiceImplTest {
|
||||
whenever(productRepository.findByGuid(eq(guid)))
|
||||
.thenReturn(null)
|
||||
|
||||
assertThrows<NotFoundException> {
|
||||
assertThrows<ProductNotFoundException> {
|
||||
productService.syncToKafka(guid, specificTopic)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user