Skip to content

Commit

Permalink
Change order schema (#3)
Browse files Browse the repository at this point in the history
* Remove @document as it will no longer use to store data in mongodb

* Added new models

* Link multiple models

* Increment version of library
  • Loading branch information
Harsh3305 authored Jun 7, 2023
1 parent 479b7a1 commit c4d3bcd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "com.hrv.mart"
version = "0.0.2"
version = "0.0.3"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderRequest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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 (
val userId: String,
val products: List<CartResponse>,
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)

}
24 changes: 20 additions & 4 deletions src/main/kotlin/com/hrv/mart/orderlibrary/model/OrderResponse.kt
Original file line number Diff line number Diff line change
@@ -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<CartResponse>,
val price: Long,
val status: Status = Status.PROCESS,
val dateTimeOfOrder: LocalDateTime = LocalDateTime.now()
)
) {
companion object {
fun parseFrom (order: Order, productOrdered: List<ProductOrdered>) =
OrderResponse(
userId = order.userId,
price = order.price,
products = productOrdered
.map {
CartResponse(
productId = it.productId,
quantity = it.quantity
)
},
status = order.status,
dateTimeOfOrder = order.dateTimeOfOrder
)
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/com/hrv/mart/orderlibrary/model/order/Order.kt
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Original file line number Diff line number Diff line change
@@ -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
)
}
}
}

0 comments on commit c4d3bcd

Please sign in to comment.