From c4d3bcd4d401a9ef2bd93ae9ba281d6d3a00dcf2 Mon Sep 17 00:00:00 2001 From: Harsh Verma <55652117+Harsh3305@users.noreply.github.com> Date: Wed, 7 Jun 2023 12:36:05 +0530 Subject: [PATCH] Change order schema (#3) * Remove @Document as it will no longer use to store data in mongodb * Added new models * Link multiple models * Increment version of library --- build.gradle.kts | 2 +- .../mart/orderlibrary/model/OrderRequest.kt | 13 +++++----- .../mart/orderlibrary/model/OrderResponse.kt | 24 ++++++++++++++--- .../mart/orderlibrary/model/order/Order.kt | 25 ++++++++++++++++++ .../model/order/ProductOrdered.kt | 26 +++++++++++++++++++ 5 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/com/hrv/mart/orderlibrary/model/order/Order.kt create mode 100644 src/main/kotlin/com/hrv/mart/orderlibrary/model/order/ProductOrdered.kt diff --git a/build.gradle.kts b/build.gradle.kts index 047cfd5..b90d1d1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "com.hrv.mart" -version = "0.0.2" +version = "0.0.3" java.sourceCompatibility = JavaVersion.VERSION_17 repositories { diff --git a/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderRequest.kt b/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderRequest.kt index 8d7de66..35b74e6 100644 --- a/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderRequest.kt +++ b/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderRequest.kt @@ -1,6 +1,8 @@ package com.hrv.mart.orderlibrary.model import com.hrv.mart.cartresponse.model.CartResponse +import com.hrv.mart.orderlibrary.model.order.Order +import com.hrv.mart.orderlibrary.model.order.ProductOrdered data class OrderRequest ( @@ -8,10 +10,9 @@ data class OrderRequest ( val products: List, val price: Long ) { - fun getOrderResponse() = - OrderResponse( - userId=userId, - products=products, - price=price - ) + fun getOrder() = + Order.parseFrom(this) + fun getProductOrdered(order: Order) = + ProductOrdered.parseFrom(this, order) + } \ No newline at end of file diff --git a/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderResponse.kt b/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderResponse.kt index 5fe4ec2..0cfd1fc 100644 --- a/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderResponse.kt +++ b/src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderResponse.kt @@ -1,15 +1,31 @@ package com.hrv.mart.orderlibrary.model -import com.hrv.mart.orderlibrary.model.Status import com.hrv.mart.cartresponse.model.CartResponse -import org.springframework.data.mongodb.core.mapping.Document +import com.hrv.mart.orderlibrary.model.order.Order +import com.hrv.mart.orderlibrary.model.order.ProductOrdered import java.time.LocalDateTime -@Document("Order") data class OrderResponse ( val userId: String, val products: List, val price: Long, val status: Status = Status.PROCESS, val dateTimeOfOrder: LocalDateTime = LocalDateTime.now() -) \ No newline at end of file +) { + companion object { + fun parseFrom (order: Order, productOrdered: List) = + OrderResponse( + userId = order.userId, + price = order.price, + products = productOrdered + .map { + CartResponse( + productId = it.productId, + quantity = it.quantity + ) + }, + status = order.status, + dateTimeOfOrder = order.dateTimeOfOrder + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/hrv/mart/orderlibrary/model/order/Order.kt b/src/main/kotlin/com/hrv/mart/orderlibrary/model/order/Order.kt new file mode 100644 index 0000000..659e85c --- /dev/null +++ b/src/main/kotlin/com/hrv/mart/orderlibrary/model/order/Order.kt @@ -0,0 +1,25 @@ +package com.hrv.mart.orderlibrary.model.order + +import com.hrv.mart.orderlibrary.model.OrderRequest +import com.hrv.mart.orderlibrary.model.Status +import org.bson.types.ObjectId +import org.springframework.data.mongodb.core.mapping.Document +import java.time.LocalDateTime + +@Document("Order") +data class Order ( + val userId: String, + val price: Long, + // Params with default values + val orderId: String=ObjectId.get().toString(), + val status: Status = Status.PROCESS, + val dateTimeOfOrder: LocalDateTime = LocalDateTime.now() +) { + companion object { + fun parseFrom(orderRequest: OrderRequest) = + Order( + userId=orderRequest.userId, + price = orderRequest.price + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/hrv/mart/orderlibrary/model/order/ProductOrdered.kt b/src/main/kotlin/com/hrv/mart/orderlibrary/model/order/ProductOrdered.kt new file mode 100644 index 0000000..72c7bfd --- /dev/null +++ b/src/main/kotlin/com/hrv/mart/orderlibrary/model/order/ProductOrdered.kt @@ -0,0 +1,26 @@ +package com.hrv.mart.orderlibrary.model.order + +import com.hrv.mart.orderlibrary.model.OrderRequest +import org.springframework.data.mongodb.core.mapping.Document + +@Document("ProductOrdered") +data class ProductOrdered ( + val orderId: String, + val productId: String, + val quantity: Long, + val userId: String +) { + companion object { + fun parseFrom(orderRequest: OrderRequest, order: Order) = + orderRequest + .products + .map {cartResponse -> + ProductOrdered( + userId = order.userId, + productId = cartResponse.productId, + quantity = cartResponse.quantity, + orderId = order.orderId + ) + } + } +} \ No newline at end of file