db dto = entity

This commit is contained in:
Savosin Denis
2025-05-06 14:08:14 +07:00
parent 7c12208883
commit b855aba506
15 changed files with 44 additions and 46 deletions

View File

@@ -11,7 +11,7 @@ import java.util.UUID
@Table("city") @Table("city")
@Serializable @Serializable
data class City( data class CityEntity(
@Id @Id
val id: Long?, val id: Long?,
@Serializable(with = UuidSerialization::class) @Serializable(with = UuidSerialization::class)
@@ -26,6 +26,4 @@ data class City(
@Serializable(with = OffsetDateTimeSerialization::class) @Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "deleted_at") @Column(value = "deleted_at")
val deletedAt: OffsetDateTime?, val deletedAt: OffsetDateTime?,
) { )
fun isDeleted(): Boolean = deletedAt != null
}

View File

@@ -11,7 +11,7 @@ import java.util.UUID
@Table("customer") @Table("customer")
@Serializable @Serializable
data class Customer( data class CustomerEntity(
@Id @Id
val id: Long?, val id: Long?,
@Serializable(with = UuidSerialization::class) @Serializable(with = UuidSerialization::class)

View File

@@ -13,7 +13,7 @@ import kotlin.math.roundToInt
@Table(value = "product") @Table(value = "product")
@Serializable @Serializable
data class Product( data class ProductEntity(
@Id @Id
val id: Long?, val id: Long?,
@Serializable(with = UuidSerialization::class) @Serializable(with = UuidSerialization::class)

View File

@@ -11,7 +11,7 @@ import java.util.UUID
@Table(value = "order") @Table(value = "order")
@Serializable @Serializable
data class Order( data class OrderEntity(
@Id @Id
val id: Long?, val id: Long?,
@Serializable(with = UuidSerialization::class) @Serializable(with = UuidSerialization::class)

View File

@@ -13,7 +13,7 @@ import java.util.UUID
@Table(value = "order_product") @Table(value = "order_product")
@Serializable @Serializable
data class OrderProduct( data class OrderProductEntity(
@Id @Id
@Serializable(with = UuidSerialization::class) @Serializable(with = UuidSerialization::class)
val guid: UUID, val guid: UUID,

View File

@@ -1,6 +1,6 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.entity.City import com.github.dannecron.demo.db.entity.CityEntity
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.repository.CrudRepository import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.query.Param import org.springframework.data.repository.query.Param
@@ -9,9 +9,9 @@ import java.time.OffsetDateTime
import java.util.UUID import java.util.UUID
@Repository @Repository
interface CityRepository: CrudRepository<City, Long> { interface CityRepository: CrudRepository<CityEntity, Long> {
fun findByGuid(guid: UUID): City? fun findByGuid(guid: UUID): CityEntity?
@Query(value = "UPDATE city SET deleted_at = :deletedAt WHERE guid = :guid RETURNING *") @Query(value = "UPDATE city SET deleted_at = :deletedAt WHERE guid = :guid RETURNING *")
fun softDelete(@Param("guid") guid: UUID, @Param("deletedAt") deletedAt: OffsetDateTime): City? fun softDelete(@Param("guid") guid: UUID, @Param("deletedAt") deletedAt: OffsetDateTime): CityEntity?
} }

View File

@@ -1,11 +1,11 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.entity.Customer import com.github.dannecron.demo.db.entity.CustomerEntity
import org.springframework.data.repository.CrudRepository import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.util.UUID import java.util.UUID
@Repository @Repository
interface CustomerRepository: CrudRepository<Customer, Long> { interface CustomerRepository: CrudRepository<CustomerEntity, Long> {
fun findByGuid(guid: UUID): Customer? fun findByGuid(guid: UUID): CustomerEntity?
} }

View File

@@ -1,11 +1,11 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.entity.order.OrderProduct import com.github.dannecron.demo.db.entity.order.OrderProductEntity
import org.springframework.data.repository.CrudRepository import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.util.UUID import java.util.UUID
@Repository @Repository
interface OrderProductRepository: CrudRepository<OrderProduct, UUID> { interface OrderProductRepository: CrudRepository<OrderProductEntity, UUID> {
fun findByOrderId(orderId: Long): List<OrderProduct> fun findByOrderId(orderId: Long): List<OrderProductEntity>
} }

View File

@@ -1,10 +1,10 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.entity.order.Order import com.github.dannecron.demo.db.entity.order.OrderEntity
import org.springframework.data.repository.CrudRepository import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
interface OrderRepository: CrudRepository<Order, Long> { interface OrderRepository: CrudRepository<OrderEntity, Long> {
fun findByCustomerId(customerId: Long): List<Order> fun findByCustomerId(customerId: Long): List<OrderEntity>
} }

View File

@@ -1,6 +1,6 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.entity.Product import com.github.dannecron.demo.db.entity.ProductEntity
import org.springframework.data.jdbc.repository.query.Query import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.repository.CrudRepository import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.PagingAndSortingRepository
@@ -9,9 +9,9 @@ import java.time.OffsetDateTime
import java.util.UUID import java.util.UUID
@Repository @Repository
interface ProductRepository: CrudRepository<Product, Long>, PagingAndSortingRepository<Product, Long> { interface ProductRepository: CrudRepository<ProductEntity, Long>, PagingAndSortingRepository<ProductEntity, Long> {
fun findByGuid(guid: UUID): Product? fun findByGuid(guid: UUID): ProductEntity?
@Query(value = "UPDATE Product SET deleted_at = :deletedAt WHERE guid = :guid RETURNING *") @Query(value = "UPDATE Product SET deleted_at = :deletedAt WHERE guid = :guid RETURNING *")
fun softDelete(guid: UUID, deletedAt: OffsetDateTime): Product? fun softDelete(guid: UUID, deletedAt: OffsetDateTime): ProductEntity?
} }

View File

@@ -1,7 +1,7 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.City import com.github.dannecron.demo.db.entity.CityEntity
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 org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql
@@ -12,13 +12,13 @@ import kotlin.test.assertEquals
import kotlin.test.assertNull import kotlin.test.assertNull
@ContextConfiguration(classes = [CityRepository::class]) @ContextConfiguration(classes = [CityRepository::class])
class CityRepositoryTest : BaseDbTest() { class CityEntityRepositoryTest : BaseDbTest() {
@Autowired @Autowired
private lateinit var cityRepository: CityRepository private lateinit var cityRepository: CityRepository
private val cityGuid = UUID.fromString("21a1a3a8-621a-40f7-b64f-7e118aa241b9") private val cityGuid = UUID.fromString("21a1a3a8-621a-40f7-b64f-7e118aa241b9")
private val city = City( private val cityEntity = CityEntity(
id = 1000, id = 1000,
guid = cityGuid, guid = cityGuid,
name = "Tokyo", name = "Tokyo",
@@ -31,7 +31,7 @@ class CityRepositoryTest : BaseDbTest() {
@Sql(scripts = ["/sql/insert_city.sql"]) @Sql(scripts = ["/sql/insert_city.sql"])
fun findByGuid() { fun findByGuid() {
val result = cityRepository.findByGuid(cityGuid) val result = cityRepository.findByGuid(cityGuid)
assertEquals(city, result) assertEquals(cityEntity, result)
val emptyResult = cityRepository.findByGuid(UUID.randomUUID()) val emptyResult = cityRepository.findByGuid(UUID.randomUUID())
assertNull(emptyResult) assertNull(emptyResult)
@@ -41,7 +41,7 @@ class CityRepositoryTest : BaseDbTest() {
@Sql(scripts = ["/sql/insert_city.sql"]) @Sql(scripts = ["/sql/insert_city.sql"])
fun softDelete() { fun softDelete() {
val deletedAt = OffsetDateTime.parse("2025-01-02T12:10:05+00:00") val deletedAt = OffsetDateTime.parse("2025-01-02T12:10:05+00:00")
val expectedCity = city.copy(deletedAt = deletedAt) val expectedCity = cityEntity.copy(deletedAt = deletedAt)
val result = cityRepository.softDelete(cityGuid, deletedAt) val result = cityRepository.softDelete(cityGuid, deletedAt)
assertEquals(expectedCity, result) assertEquals(expectedCity, result)

View File

@@ -1,7 +1,7 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.Customer import com.github.dannecron.demo.db.entity.CustomerEntity
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 org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql
@@ -12,13 +12,13 @@ import kotlin.test.assertEquals
import kotlin.test.assertNull import kotlin.test.assertNull
@ContextConfiguration(classes = [CustomerRepository::class]) @ContextConfiguration(classes = [CustomerRepository::class])
class CustomerRepositoryTest : BaseDbTest() { class CustomerEntityRepositoryTest : BaseDbTest() {
@Autowired @Autowired
private lateinit var customerRepository: CustomerRepository private lateinit var customerRepository: CustomerRepository
private val customerGuid = UUID.fromString("823c50de-4c81-49bd-a69a-2d52be42b728") private val customerGuid = UUID.fromString("823c50de-4c81-49bd-a69a-2d52be42b728")
private val customer = Customer( private val customerEntity = CustomerEntity(
id = 1000, id = 1000,
guid = customerGuid, guid = customerGuid,
name = "Customer", name = "Customer",
@@ -31,7 +31,7 @@ class CustomerRepositoryTest : BaseDbTest() {
@Sql(scripts = ["/sql/insert_city.sql", "/sql/insert_customer.sql"]) @Sql(scripts = ["/sql/insert_city.sql", "/sql/insert_customer.sql"])
fun findByGuid() { fun findByGuid() {
val result = customerRepository.findByGuid(customerGuid) val result = customerRepository.findByGuid(customerGuid)
assertEquals(customer, result) assertEquals(customerEntity, result)
val emptyResult = customerRepository.findByGuid(UUID.randomUUID()) val emptyResult = customerRepository.findByGuid(UUID.randomUUID())
assertNull(emptyResult) assertNull(emptyResult)

View File

@@ -1,7 +1,7 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.order.OrderProduct import com.github.dannecron.demo.db.entity.order.OrderProductEntity
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 org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql
@@ -11,13 +11,13 @@ import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ContextConfiguration(classes = [OrderProductRepository::class]) @ContextConfiguration(classes = [OrderProductRepository::class])
class OrderProductRepositoryTest : BaseDbTest() { class OrderEntityProductEntityRepositoryTest : BaseDbTest() {
@Autowired @Autowired
private lateinit var orderProductRepository: OrderProductRepository private lateinit var orderProductRepository: OrderProductRepository
private val orderId = 1000L private val orderId = 1000L
private val orderProduct = OrderProduct( private val orderProductEntity = OrderProductEntity(
guid = UUID.fromString("930f54e2-c60d-448e-83b1-0d259ff2c2d3"), guid = UUID.fromString("930f54e2-c60d-448e-83b1-0d259ff2c2d3"),
orderId = orderId, orderId = orderId,
productId = 1000, productId = 1000,
@@ -38,6 +38,6 @@ class OrderProductRepositoryTest : BaseDbTest() {
fun findByOrderId() { fun findByOrderId() {
val result = orderProductRepository.findByOrderId(orderId) val result = orderProductRepository.findByOrderId(orderId)
assertEquals(1, result.size) assertEquals(1, result.size)
assertEquals(orderProduct, result[0]) assertEquals(orderProductEntity, result[0])
} }
} }

View File

@@ -1,7 +1,7 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.order.Order import com.github.dannecron.demo.db.entity.order.OrderEntity
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 org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql
@@ -11,14 +11,14 @@ import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ContextConfiguration(classes = [OrderRepository::class]) @ContextConfiguration(classes = [OrderRepository::class])
class OrderRepositoryTest : BaseDbTest() { class OrderEntityRepositoryTest : BaseDbTest() {
@Autowired @Autowired
private lateinit var orderRepository: OrderRepository private lateinit var orderRepository: OrderRepository
private val orderGuid = UUID.fromString("2c960a08-7187-4e91-9ef3-275c91b1342c") private val orderGuid = UUID.fromString("2c960a08-7187-4e91-9ef3-275c91b1342c")
private val customerId = 1000L private val customerId = 1000L
private val order = Order( private val orderEntity = OrderEntity(
id = 1000, id = 1000,
guid = orderGuid, guid = orderGuid,
customerId = customerId, customerId = customerId,
@@ -38,6 +38,6 @@ class OrderRepositoryTest : BaseDbTest() {
fun findByGuid() { fun findByGuid() {
val result = orderRepository.findByCustomerId(customerId) val result = orderRepository.findByCustomerId(customerId)
assertEquals(1, result.size) assertEquals(1, result.size)
assertEquals(order, result[0]) assertEquals(orderEntity, result[0])
} }
} }

View File

@@ -1,7 +1,7 @@
package com.github.dannecron.demo.db.repository package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.Product import com.github.dannecron.demo.db.entity.ProductEntity
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 org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql
@@ -12,13 +12,13 @@ import kotlin.test.assertEquals
import kotlin.test.assertNull import kotlin.test.assertNull
@ContextConfiguration(classes = [ProductRepository::class]) @ContextConfiguration(classes = [ProductRepository::class])
class ProductRepositoryTest : BaseDbTest() { class ProductEntityRepositoryTest : BaseDbTest() {
@Autowired @Autowired
private lateinit var productRepository: ProductRepository private lateinit var productRepository: ProductRepository
private val productGuid = UUID.fromString("1fb5c7e4-8ce2-43b8-8ca7-1089b04959b9") private val productGuid = UUID.fromString("1fb5c7e4-8ce2-43b8-8ca7-1089b04959b9")
private val product = Product( private val productEntity = ProductEntity(
id = 1000, id = 1000,
guid = productGuid, guid = productGuid,
name = "product", name = "product",
@@ -33,7 +33,7 @@ class ProductRepositoryTest : BaseDbTest() {
@Sql(scripts = ["/sql/insert_product.sql"]) @Sql(scripts = ["/sql/insert_product.sql"])
fun findByGuid() { fun findByGuid() {
val result = productRepository.findByGuid(productGuid) val result = productRepository.findByGuid(productGuid)
assertEquals(product, result) assertEquals(productEntity, result)
val emptyResult = productRepository.findByGuid(UUID.randomUUID()) val emptyResult = productRepository.findByGuid(UUID.randomUUID())
assertNull(emptyResult) assertNull(emptyResult)