Skip to content

Commit

Permalink
Change order structure
Browse files Browse the repository at this point in the history
  • Loading branch information
blaz-cerpnjak committed Apr 14, 2024
1 parent 9db4143 commit 6cf07b4
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 112 deletions.
9 changes: 1 addition & 8 deletions API_GatewayWeb/DataStructures/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,9 @@ type Order struct {
DeliveryPersonId primitive.ObjectID `json:"deliveryPersonId" bson:"deliveryPersonId"`
Address string `json:"address" bson:"address"`
CustomerName string `json:"customerName" bson:"customerName"`
OrderItems []OrderItem `json:"items" bson:"items"`
Items []Product `json:"items" bson:"items"`
Status string `json:"status" bson:"status"`
Timestamp int64 `json:"timestamp" bson:"timestamp"`
PaymentType string `json:"paymentType" bson:"paymentType"`
TotalPrice int32 `json:"totalPrice" bson:"totalPrice"` // in cents
}

type OrderItem struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Product Product `json:"product" bson:"product"`
Quantity int32 `json:"quantity" bson:"quantity"`
Price int32 `json:"price" bson:"price"` // in cents
}
11 changes: 3 additions & 8 deletions API_GatewayWeb/DataStructures/order_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ service OrderService {
rpc CreateOrder (Order) returns (Confirmation) {}
rpc GetOrder (GetOrderRequest) returns (Order) {}
rpc UpdateOrder (Order) returns (Confirmation) {}
rpc GetOrders (Empty) returns (stream Order) {}
rpc GetOrdersBySeller (GetOrdersRequest) returns (stream Order) {}
rpc GetOrdersByDeliveryPerson (GetOrdersRequest) returns (stream Order) {}
rpc DeleteOrder (DeleteOrderRequest) returns (Confirmation) {}
Expand Down Expand Up @@ -39,25 +40,19 @@ message Order {
string deliveryPersonId = 3;
string address = 4;
string customerName = 5;
repeated OrderItem items = 6;
repeated Product items = 6;
OrderStatus status = 7;
int64 timestamp = 8;
PaymentType paymentType = 9;
int32 totalPrice = 10;
}

message OrderItem {
string id = 1;
Product product = 2;
int32 quantity = 3;
int32 price = 4;
}

message Product {
string id = 1;
string name = 2;
int32 price = 3;
string image = 4;
string restaurantId = 5;
}

enum PaymentType {
Expand Down
10 changes: 10 additions & 0 deletions API_GatewayWeb/HTTP_API/Order.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

func (a *Controller) getAllOrders(ctx *gin.Context) {
orders, err := a.logic.GetAllOrders(ctx.Request.Context())
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, orders)
}

func (a *Controller) createOrder(ctx *gin.Context) {
var order DataStructures.Order
err := ctx.BindJSON(&order)
Expand Down
1 change: 1 addition & 0 deletions API_GatewayWeb/HTTP_API/Routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (a *Controller) registerProductRoutes(api *gin.RouterGroup) {
}

func (a *Controller) registerOrderRoutes(api *gin.RouterGroup) {
api.GET("/", a.getAllOrders)
api.POST("/", a.createOrder)
api.GET("/restaurant/:id", a.getOrdersBySellerId)
api.PUT("/:id", a.updateOrder)
Expand Down
29 changes: 29 additions & 0 deletions API_GatewayWeb/Logic/Order.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ func (c *Controller) Health(ctx context.Context) (status string, err error) {
return
}

func (c *Controller) GetAllOrders(ctx context.Context) (orders []DataStructures.Order, err error) {
orderStream, err := c.grpc.Client.GetOrders(ctx, &pb.Empty{})
if err != nil {
fmt.Println(err.Error())
return
}

orders = make([]DataStructures.Order, 0)

for {
orderGrpc, err := orderStream.Recv()
if err != nil {
fmt.Println(err.Error())
break
}

order, err := Converter.ConvertOrderFromGrpc(orderGrpc)
if err != nil {
fmt.Println(err.Error())
continue
}

fmt.Println(order)
orders = append(orders, order)
}

return
}

func (c *Controller) CreateOrder(ctx context.Context, order DataStructures.Order) (confirmation *pb.Confirmation, err error) {

orderGrpc := Converter.ConvertOrderToGrpc(order)
Expand Down
56 changes: 30 additions & 26 deletions API_GatewayWeb/pkg/Converter/OrdersGrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Converter
import (
"API_GatewayWeb/DataStructures"
pb "API_GatewayWeb/DataStructures/com.blazc"
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
Expand Down Expand Up @@ -38,17 +39,14 @@ func ConvertFromPaymentTypeString(paymentType string) (paymentTypeGrpc pb.Paymen

func ConvertOrderToGrpc(order DataStructures.Order) (orderGrpc *pb.Order) {

orderItemsGrpc := make([]*pb.OrderItem, 0)
for _, item := range order.OrderItems {
orderItemsGrpc = append(orderItemsGrpc, &pb.OrderItem{
Id: item.Id.Hex(),
Price: item.Price,
Quantity: item.Quantity,
Product: &pb.Product{
Id: item.Id.Hex(),
Name: item.Product.Name,
Price: item.Product.Price,
},
itemsGrpc := make([]*pb.Product, 0)
for _, item := range order.Items {
itemsGrpc = append(itemsGrpc, &pb.Product{
Id: item.Id.Hex(),
Name: item.Name,
Price: item.Price,
Image: item.Image,
RestaurantId: item.RestaurantId.Hex(),
})
}

Expand All @@ -58,7 +56,7 @@ func ConvertOrderToGrpc(order DataStructures.Order) (orderGrpc *pb.Order) {
DeliveryPersonId: order.DeliveryPersonId.Hex(),
Address: order.Address,
CustomerName: order.CustomerName,
Items: orderItemsGrpc,
Items: itemsGrpc,
Status: ConvertFromOrderStatusString(order.Status),
Timestamp: time.Now().UTC().Unix(),
PaymentType: ConvertFromPaymentTypeString(order.PaymentType),
Expand All @@ -72,48 +70,54 @@ func ConvertOrderFromGrpc(orderGrpc *pb.Order) (order DataStructures.Order, err

order.Id, err = primitive.ObjectIDFromHex(orderGrpc.Id)
if err != nil {
fmt.Println("orderId error: ", err.Error())
return
}

order.SellerId, err = primitive.ObjectIDFromHex(orderGrpc.SellerId)
if err != nil {
fmt.Println("sellerId error: ", err.Error())
return
}

order.DeliveryPersonId, err = primitive.ObjectIDFromHex(orderGrpc.DeliveryPersonId)
if err != nil {
fmt.Println("deliveryPersonId error: ", err.Error())
return
}

order.Address = orderGrpc.Address
order.CustomerName = orderGrpc.CustomerName

items := make([]DataStructures.OrderItem, 0)
items := make([]DataStructures.Product, 0)
for _, item := range orderGrpc.Items {
id, err := primitive.ObjectIDFromHex(item.Id)

productId, err := primitive.ObjectIDFromHex(item.Id)
if err != nil {
fmt.Println("productId error: ", err.Error())
return order, err
}

productId, err := primitive.ObjectIDFromHex(item.Product.Id)
fmt.Println("restaurantId: ", item.RestaurantId)

restaurantId, err := primitive.ObjectIDFromHex(item.RestaurantId)
if err != nil {
fmt.Println("restaurantId error: ", err.Error())
return order, err
}

orderItem := DataStructures.OrderItem{
Id: id,
Price: item.Price,
Quantity: item.Quantity,
Product: DataStructures.Product{
Id: productId,
Name: item.Product.Name,
Price: item.Product.Price,
},
product := DataStructures.Product{
Id: productId,
Name: item.Name,
Price: item.Price,
Image: item.Image,
RestaurantId: restaurantId,
}
items = append(items, orderItem)

items = append(items, product)
}

order.OrderItems = items
order.Items = items
order.Status = orderGrpc.Status.String()
order.Timestamp = orderGrpc.Timestamp
order.PaymentType = orderGrpc.PaymentType.String()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.blazc.model.order

import com.blazc.OrderGrpc
import com.blazc.model.payment.PaymentType
import com.blazc.model.product.Product
import org.bson.types.ObjectId
import java.time.Instant

Expand All @@ -11,7 +12,7 @@ data class Order (
var deliveryPersonId: ObjectId? = null,
var address: String,
var customerName: String,
var items: List<OrderItem>,
var items: List<Product>,
var status: OrderStatus,
var timestamp: Long,
var paymentType: PaymentType,
Expand Down Expand Up @@ -40,7 +41,7 @@ data class Order (
address = order.address,
customerName = order.customerName,
items = order.itemsList.map {
OrderItem.fromGrpc(it)
Product.fromGrpc(it)
},
status = OrderStatus.fromGrpc(order.status),
timestamp = order.timestamp,
Expand All @@ -56,9 +57,7 @@ data class Order (
.setDeliveryPersonId(order.deliveryPersonId.toString())
.setAddress(order.address)
.setCustomerName(order.customerName)
.addAllItems(order.items.map {
OrderItem.toGrpc(it)
})
.addAllItems(order.items.map { Product.toGrpc(it) })
.setStatus(OrderStatus.toGrpc(order.status))
.setTimestamp(order.timestamp)
.setPaymentType(PaymentType.toGrpc(order.paymentType))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import org.bson.types.ObjectId

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

constructor() : this(
id = ObjectId(),
restaurantId = ObjectId(),
name = "",
price = 0,
image = ""
Expand All @@ -22,6 +24,7 @@ data class Product(
fun fromGrpc(productGrpc: OrderGrpc.Product): Product {
return Product(
id = ObjectId(productGrpc.id),
restaurantId = ObjectId(),
name = productGrpc.name,
price = productGrpc.price,
image = productGrpc.image
Expand All @@ -31,6 +34,7 @@ data class Product(
fun toGrpc(product: Product): OrderGrpc.Product {
return OrderGrpc.Product.newBuilder()
.setId(product.id.toString())
.setRestaurantId(product.restaurantId.toString())
.setName(product.name)
.setPrice(product.price)
.setImage(product.image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class OrderService : OrderServiceGrpc.OrderServiceImplBase() {
@Inject
lateinit var orderRepository: OrderRepository

@Channel("orders")
private lateinit var emmiter: Emitter<String>
//@Channel("orders")
//private lateinit var emmiter: Emitter<String>

override fun health(request: OrderGrpc.Empty, responseObserver: StreamObserver<OrderGrpc.Confirmation>) {
val confirmation = OrderGrpc.Confirmation.newBuilder()
Expand Down Expand Up @@ -52,7 +52,7 @@ class OrderService : OrderServiceGrpc.OrderServiceImplBase() {
.setMessage("created")
.build()

emmiter.send("${order.id}:${order.status}")
//emmiter.send("${order.id}:${order.status}")

responseObserver.onNext(response)
responseObserver.onCompleted()
Expand Down Expand Up @@ -86,6 +86,16 @@ class OrderService : OrderServiceGrpc.OrderServiceImplBase() {
responseObserver.onCompleted()
}

override fun getOrders(request: OrderGrpc.Empty?, responseObserver: StreamObserver<OrderGrpc.Order>) {
val orders = orderRepository.listAll()

orders.forEach {
responseObserver.onNext(Order.toGrpc(it))
}

responseObserver.onCompleted()
}

override fun getOrdersBySeller(request: OrderGrpc.GetOrdersRequest, responseObserver: StreamObserver<OrderGrpc.Order>) {
val sellerId: ObjectId?

Expand Down Expand Up @@ -146,7 +156,7 @@ class OrderService : OrderServiceGrpc.OrderServiceImplBase() {
.setMessage("updated")
.build()

emmiter.send("${order.id}:${order.status}")
//emmiter.send("${order.id}:${order.status}")

responseObserver.onNext(response)
responseObserver.onCompleted()
Expand Down
Loading

0 comments on commit 6cf07b4

Please sign in to comment.