Skip to content

Commit

Permalink
Implement gRPC API for orders
Browse files Browse the repository at this point in the history
  • Loading branch information
blaz-cerpnjak committed Mar 14, 2024
1 parent 171a3aa commit fe596ec
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 0 deletions.
1 change: 1 addition & 0 deletions OrderProcessingAPI/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'org.jetbrains.kotlin.jvm' version "1.9.22"
id "org.jetbrains.kotlin.plugin.allopen" version "1.9.22"
id 'io.quarkus'
id "org.jetbrains.kotlin.plugin.noarg" version "1.9.22"
}

repositories {
Expand Down
70 changes: 70 additions & 0 deletions OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/Order.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.blazc.model.order

import com.blazc.OrderGrpc
import com.blazc.model.payment.PaymentType
import com.blazc.utils.DateFormatter
import org.bson.types.ObjectId
import java.time.LocalDateTime

data class Order (
var id: ObjectId? = null,
var sellerId: ObjectId? = null,
var deliveryPersonId: ObjectId? = null,
var address: String,
var customerName: String,
var items: List<OrderItem>,
var status: OrderStatus,
var orderDate: LocalDateTime,
var paymentType: PaymentType,
var totalPrice: Int
) {

constructor() : this(
id = ObjectId(),
sellerId = ObjectId(),
deliveryPersonId = ObjectId(),
address = "",
customerName = "",
items = emptyList(),
status = OrderStatus.PENDING,
orderDate = LocalDateTime.now(),
paymentType = PaymentType.CASH,
totalPrice = 0
)

companion object {
fun fromGrpc(order: OrderGrpc.Order): Order {
return Order(
id = ObjectId(order.id),
sellerId = ObjectId(order.sellerId),
deliveryPersonId = ObjectId(order.deliveryPersonId),
address = order.address,
customerName = order.customerName,
items = order.itemsList.map {
OrderItem.fromGrpc(it)
},
status = OrderStatus.fromGrpc(order.status),
orderDate = DateFormatter.toLocalDateTime(order.orderDate),
paymentType = PaymentType.fromGrpc(order.paymentType),
totalPrice = order.totalPrice
)
}

fun toGrpc(order: Order): OrderGrpc.Order {
return OrderGrpc.Order.newBuilder()
.setId(order.id.toString())
.setSellerId(order.sellerId.toString())
.setDeliveryPersonId(order.deliveryPersonId.toString())
.setAddress(order.address)
.setCustomerName(order.customerName)
.addAllItems(order.items.map {
OrderItem.toGrpc(it)
})
.setStatus(OrderStatus.toGrpc(order.status))
.setOrderDate(DateFormatter.toString(order.orderDate))
.setPaymentType(PaymentType.toGrpc(order.paymentType))
.setTotalPrice(order.totalPrice)
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.blazc.model.order

import com.blazc.OrderGrpc
import com.blazc.model.product.Product
import org.bson.types.ObjectId

data class OrderItem(
var id: ObjectId? = null,
var product: Product,
var quantity: Int,
var price: Int
) {

constructor() : this(
id = ObjectId(),
product = Product(),
quantity = 0,
price = 0
)

companion object {
fun fromGrpc(orderItem: OrderGrpc.OrderItem): OrderItem {
return OrderItem(
id = ObjectId(orderItem.id),
product = Product.fromGrpc(orderItem.product),
quantity = orderItem.quantity,
price = orderItem.price
)
}

fun toGrpc(orderItem: OrderItem): OrderGrpc.OrderItem {
return OrderGrpc.OrderItem.newBuilder()
.setId(orderItem.id.toString())
.setProduct(Product.toGrpc(orderItem.product))
.setQuantity(orderItem.quantity)
.setPrice(orderItem.price)
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.blazc.model.order

import com.blazc.OrderGrpc

enum class OrderStatus {
PENDING,
PREPARING,
READY_FOR_PICKUP,
DELIVERING,
DELIVERED,
CANCELLED;

companion object {
fun fromGrpc(status: OrderGrpc.OrderStatus): OrderStatus {
return when (status) {
OrderGrpc.OrderStatus.PENDING -> PENDING
OrderGrpc.OrderStatus.PREPARING -> PREPARING
OrderGrpc.OrderStatus.READY_FOR_PICKUP -> READY_FOR_PICKUP
OrderGrpc.OrderStatus.DELIVERING -> DELIVERING
OrderGrpc.OrderStatus.DELIVERED -> DELIVERED
OrderGrpc.OrderStatus.CANCELLED -> CANCELLED
OrderGrpc.OrderStatus.UNRECOGNIZED -> PENDING
}
}

fun toGrpc(status: OrderStatus): OrderGrpc.OrderStatus {
return when (status) {
PENDING -> OrderGrpc.OrderStatus.PENDING
PREPARING -> OrderGrpc.OrderStatus.PREPARING
READY_FOR_PICKUP -> OrderGrpc.OrderStatus.READY_FOR_PICKUP
DELIVERING -> OrderGrpc.OrderStatus.DELIVERING
DELIVERED -> OrderGrpc.OrderStatus.DELIVERED
CANCELLED -> OrderGrpc.OrderStatus.CANCELLED
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.blazc.model.payment

import com.blazc.OrderGrpc

enum class PaymentType {
CREDIT_CARD,
CASH;

companion object {
fun fromGrpc(type: OrderGrpc.PaymentType): PaymentType {
return when (type) {
OrderGrpc.PaymentType.CREDIT_CARD -> PaymentType.CREDIT_CARD
OrderGrpc.PaymentType.CASH -> PaymentType.CASH
OrderGrpc.PaymentType.UNRECOGNIZED -> PaymentType.CASH
}
}

fun toGrpc(type: PaymentType): OrderGrpc.PaymentType {
return when (type) {
PaymentType.CREDIT_CARD -> OrderGrpc.PaymentType.CREDIT_CARD
PaymentType.CASH -> OrderGrpc.PaymentType.CASH
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.blazc.model.product

import com.blazc.OrderGrpc
import org.bson.types.ObjectId


data class Product(
var id: ObjectId? = null,
var name: String,
var price: Int // 100 = 1
) {

constructor() : this(
id = ObjectId(),
name = "",
price = 0
)

companion object {
fun fromGrpc(productGrpc: OrderGrpc.Product): Product {
return Product(
id = ObjectId(productGrpc.id),
name = productGrpc.name,
price = productGrpc.price
)
}

fun toGrpc(product: Product): OrderGrpc.Product {
return OrderGrpc.Product.newBuilder()
.setId(product.id.toString())
.setName(product.name)
.setPrice(product.price)
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.blazc.repository

import com.blazc.model.order.Order
import io.quarkus.mongodb.panache.PanacheMongoRepository
import jakarta.enterprise.context.ApplicationScoped
import org.bson.types.ObjectId

@ApplicationScoped
class OrderRepository : PanacheMongoRepository<Order> {

fun findAllBySellerId(sellerId: ObjectId): List<Order> {
return find("sellerId", sellerId).list()
}

fun findAllByDeliveryPersonId(deliveryPersonId: ObjectId): List<Order> {
return find("deliveryPersonId", deliveryPersonId).list()
}

}
Loading

0 comments on commit fe596ec

Please sign in to comment.