trying to add order and order_product tables

with dtos, repositories and service
This commit is contained in:
Denis Savosin
2024-10-16 15:17:02 +07:00
parent 03d50b0be6
commit 08d3445ac4
16 changed files with 249 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
package com.github.dannecron.demo.models
data class CustomerLocal(val name: String, val city: City, val orders: List<Order>) {
data class CustomerLocal(val name: String, val city: City, val orders: List<OrderLocal>) {
/**
* Return the most expensive product among all delivered products
*/

View File

@@ -1,3 +1,31 @@
package com.github.dannecron.demo.models
data class Order(val products: List<Product>, val isDelivered: Boolean)
import com.github.dannecron.demo.services.serializables.OffsetDateTimeSerialization
import com.github.dannecron.demo.services.serializables.UuidSerialization
import kotlinx.serialization.Serializable
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.Table
import java.time.OffsetDateTime
import java.util.*
@Table(value = "order")
@Serializable
data class Order(
@Id
val id: Long?,
@Serializable(with = UuidSerialization::class)
val guid: UUID,
val customerId: Long,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "delivered_at")
val deliveredAt: OffsetDateTime?,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "created_at")
val createdAt: OffsetDateTime,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "updated_at")
val updatedAt: OffsetDateTime?
) {
fun isDelivered(): Boolean = deliveredAt != null
}

View File

@@ -0,0 +1,3 @@
package com.github.dannecron.demo.models
data class OrderLocal(val products: List<Product>, val isDelivered: Boolean)

View File

@@ -0,0 +1,41 @@
package com.github.dannecron.demo.models
import com.github.dannecron.demo.services.serializables.OffsetDateTimeSerialization
import com.github.dannecron.demo.services.serializables.UuidSerialization
import kotlinx.serialization.Serializable
import org.springframework.data.annotation.Id
import org.springframework.data.annotation.Transient
import org.springframework.data.domain.Persistable
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.Table
import java.time.OffsetDateTime
import java.util.*
@Table(value = "order_product")
@Serializable
data class OrderProduct(
@Id
@Serializable(with = UuidSerialization::class)
val guid: UUID,
@Column(value = "order_id")
val orderId: Long,
@Column(value = "product_id")
val productId: Long,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "created_at")
val createdAt: OffsetDateTime,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "updated_at")
val updatedAt: OffsetDateTime?,
): Persistable<UUID> {
@Transient
var isNewInstance: Boolean? = null
override fun getId(): UUID {
return guid
}
override fun isNew(): Boolean {
return isNewInstance ?: true
}
}

View File

@@ -16,17 +16,17 @@ class MockedShopProvider: com.github.dannecron.demo.providers.ShopProvider {
name = "Foo-1",
city = makeCity(id = 1, name = "Foo"),
orders = listOf(
Order(products = listOf(productOne, productTwo), isDelivered = true),
Order(products = listOf(productThree), isDelivered = false),
OrderLocal(products = listOf(productOne, productTwo), isDelivered = true),
OrderLocal(products = listOf(productThree), isDelivered = false),
)
),
CustomerLocal(
name = "Foo-2",
city = makeCity(id = 2, name = "Bar"),
orders = listOf(
Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = true),
Order(products = listOf(productFour), isDelivered = true),
OrderLocal(products = listOf(productOne), isDelivered = false),
OrderLocal(products = listOf(productTwo), isDelivered = true),
OrderLocal(products = listOf(productFour), isDelivered = true),
)
),
))

View File

@@ -0,0 +1,7 @@
package com.github.dannecron.demo.providers
import com.github.dannecron.demo.models.OrderProduct
import org.springframework.data.repository.CrudRepository
import java.util.*
interface OrderProductRepository: CrudRepository<OrderProduct, UUID>

View File

@@ -0,0 +1,8 @@
package com.github.dannecron.demo.providers
import com.github.dannecron.demo.models.Order
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
@Repository
interface OrderRepository: CrudRepository<Order, Long>

View File

@@ -0,0 +1,42 @@
package com.github.dannecron.demo.services.database.order
import com.github.dannecron.demo.models.Customer
import com.github.dannecron.demo.models.Order
import com.github.dannecron.demo.models.OrderProduct
import com.github.dannecron.demo.models.Product
import com.github.dannecron.demo.providers.OrderProductRepository
import com.github.dannecron.demo.providers.OrderRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.OffsetDateTime
import java.util.*
@Service
class OrderServiceImpl(
@Autowired val orderRepository: OrderRepository,
@Autowired val orderProductRepository: OrderProductRepository,
) {
@Transactional
fun createOrder(customer: Customer, products: Set<Product>): Order {
val order = Order(
id = null,
guid = UUID.randomUUID(),
customerId = customer.id!!,
deliveredAt = null,
createdAt = OffsetDateTime.now(),
updatedAt = null,
)
return orderRepository.save(order).also {
savedOrder -> products.toList().forEach {
product -> orderProductRepository.save(OrderProduct(
guid = UUID.randomUUID(),
orderId = savedOrder.id!!,
productId = product.id!!,
createdAt = OffsetDateTime.now(),
updatedAt = null
))
}
}
}
}