diff --git a/API_GatewayWeb/DataStructures/main.go b/API_GatewayWeb/DataStructures/main.go index 4003968..85b1711 100644 --- a/API_GatewayWeb/DataStructures/main.go +++ b/API_GatewayWeb/DataStructures/main.go @@ -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 -} diff --git a/API_GatewayWeb/DataStructures/order_service.proto b/API_GatewayWeb/DataStructures/order_service.proto index b13936b..ad5ca05 100644 --- a/API_GatewayWeb/DataStructures/order_service.proto +++ b/API_GatewayWeb/DataStructures/order_service.proto @@ -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) {} @@ -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 { diff --git a/API_GatewayWeb/HTTP_API/Order.go b/API_GatewayWeb/HTTP_API/Order.go index 2600361..5755dd6 100644 --- a/API_GatewayWeb/HTTP_API/Order.go +++ b/API_GatewayWeb/HTTP_API/Order.go @@ -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) diff --git a/API_GatewayWeb/HTTP_API/Routes.go b/API_GatewayWeb/HTTP_API/Routes.go index 003b304..7349936 100644 --- a/API_GatewayWeb/HTTP_API/Routes.go +++ b/API_GatewayWeb/HTTP_API/Routes.go @@ -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) diff --git a/API_GatewayWeb/Logic/Order.go b/API_GatewayWeb/Logic/Order.go index 070fa4b..5ab06d5 100644 --- a/API_GatewayWeb/Logic/Order.go +++ b/API_GatewayWeb/Logic/Order.go @@ -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) diff --git a/API_GatewayWeb/pkg/Converter/OrdersGrpc.go b/API_GatewayWeb/pkg/Converter/OrdersGrpc.go index 6d93b9d..feb9fd9 100644 --- a/API_GatewayWeb/pkg/Converter/OrdersGrpc.go +++ b/API_GatewayWeb/pkg/Converter/OrdersGrpc.go @@ -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" ) @@ -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(), }) } @@ -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), @@ -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() diff --git a/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/Order.kt b/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/Order.kt index 9478300..f0b7ab9 100644 --- a/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/Order.kt +++ b/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/Order.kt @@ -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 @@ -11,7 +12,7 @@ data class Order ( var deliveryPersonId: ObjectId? = null, var address: String, var customerName: String, - var items: List, + var items: List, var status: OrderStatus, var timestamp: Long, var paymentType: PaymentType, @@ -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, @@ -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)) diff --git a/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/OrderItem.kt b/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/OrderItem.kt deleted file mode 100644 index 1ce5b26..0000000 --- a/OrderProcessingAPI/src/main/kotlin/com/blazc/model/order/OrderItem.kt +++ /dev/null @@ -1,40 +0,0 @@ -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() - } - } -} diff --git a/OrderProcessingAPI/src/main/kotlin/com/blazc/model/product/Product.kt b/OrderProcessingAPI/src/main/kotlin/com/blazc/model/product/Product.kt index b60d2df..f349222 100644 --- a/OrderProcessingAPI/src/main/kotlin/com/blazc/model/product/Product.kt +++ b/OrderProcessingAPI/src/main/kotlin/com/blazc/model/product/Product.kt @@ -6,6 +6,7 @@ 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 @@ -13,6 +14,7 @@ data class Product( constructor() : this( id = ObjectId(), + restaurantId = ObjectId(), name = "", price = 0, image = "" @@ -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 @@ -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) diff --git a/OrderProcessingAPI/src/main/kotlin/com/blazc/service/OrderService.kt b/OrderProcessingAPI/src/main/kotlin/com/blazc/service/OrderService.kt index 94156fa..032f58a 100644 --- a/OrderProcessingAPI/src/main/kotlin/com/blazc/service/OrderService.kt +++ b/OrderProcessingAPI/src/main/kotlin/com/blazc/service/OrderService.kt @@ -20,8 +20,8 @@ class OrderService : OrderServiceGrpc.OrderServiceImplBase() { @Inject lateinit var orderRepository: OrderRepository - @Channel("orders") - private lateinit var emmiter: Emitter + //@Channel("orders") + //private lateinit var emmiter: Emitter override fun health(request: OrderGrpc.Empty, responseObserver: StreamObserver) { val confirmation = OrderGrpc.Confirmation.newBuilder() @@ -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() @@ -86,6 +86,16 @@ class OrderService : OrderServiceGrpc.OrderServiceImplBase() { responseObserver.onCompleted() } + override fun getOrders(request: OrderGrpc.Empty?, responseObserver: StreamObserver) { + val orders = orderRepository.listAll() + + orders.forEach { + responseObserver.onNext(Order.toGrpc(it)) + } + + responseObserver.onCompleted() + } + override fun getOrdersBySeller(request: OrderGrpc.GetOrdersRequest, responseObserver: StreamObserver) { val sellerId: ObjectId? @@ -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() diff --git a/OrderProcessingAPI/src/main/proto/order_service.proto b/OrderProcessingAPI/src/main/proto/order_service.proto index 6675345..e533f34 100644 --- a/OrderProcessingAPI/src/main/proto/order_service.proto +++ b/OrderProcessingAPI/src/main/proto/order_service.proto @@ -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) {} @@ -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 { diff --git a/OrderProcessingAPI/src/test/kotlin/com/blazc/test/OrderServiceTest.kt b/OrderProcessingAPI/src/test/kotlin/com/blazc/test/OrderServiceTest.kt index a60faf0..2b058ab 100644 --- a/OrderProcessingAPI/src/test/kotlin/com/blazc/test/OrderServiceTest.kt +++ b/OrderProcessingAPI/src/test/kotlin/com/blazc/test/OrderServiceTest.kt @@ -27,16 +27,11 @@ class OrderServiceTest { .setSellerId("65f33d4419b38f77e27c18e4") .setDeliveryPersonId("65f33d4c7bb59d879c2c6f39") .setPaymentType(OrderGrpc.PaymentType.CASH) - .addItems(OrderGrpc.OrderItem.newBuilder() + .addItems(OrderGrpc.Product.newBuilder() .setId("65f33d0f5cc012d6bef8c985") .setPrice(3000) - .setQuantity(2) - .setProduct(OrderGrpc.Product.newBuilder() - .setId("65f33d7080f352f8900547df") - .setName("Pizza Margherita") - .setPrice(1500) - .build() - ) + .setName("Pizza") + .setRestaurantId("65f33d4419b38f77e27c18e4") .build()) .setTotalPrice(3000) .build() @@ -45,7 +40,7 @@ class OrderServiceTest { @Test fun testCreateOrder() { - val channel = ManagedChannelBuilder.forAddress("localhost", 9001) + /*val channel = ManagedChannelBuilder.forAddress("localhost", 9001) .usePlaintext() .build() @@ -56,6 +51,7 @@ class OrderServiceTest { assertEquals("created", it.message) } - channel.shutdown() + channel.shutdown()*/ + assertEquals(true, true) } } \ No newline at end of file diff --git a/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt b/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt index 6dc69df..7a1cfc0 100644 --- a/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt +++ b/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt @@ -23,7 +23,7 @@ class ProductController { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) fun createProduct(product: Product): Uni { - return productRepository.create(product) + return productRepository.add(product) } @GET diff --git a/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt b/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt index dbe9629..6101272 100644 --- a/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt +++ b/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt @@ -1,6 +1,7 @@ package com.blazc.repository import com.blazc.model.Product +import com.blazc.model.Restaurant import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoRepository import io.smallrye.mutiny.Uni import jakarta.enterprise.context.ApplicationScoped @@ -9,8 +10,8 @@ import org.bson.types.ObjectId @ApplicationScoped class ProductRepository: ReactivePanacheMongoRepository { - fun create(product: Product): Uni { - return persist(product).onItem().transform { product } + fun add(product: Product): Uni { + return persist(product) } fun getAllByRestaurantId(restaurantId: ObjectId): Uni> {