Skip to content

Commit

Permalink
Implement web api gateway for users, restaurants and products services
Browse files Browse the repository at this point in the history
  • Loading branch information
blaz-cerpnjak committed Mar 27, 2024
1 parent 20f7f1e commit 2eb4bd6
Show file tree
Hide file tree
Showing 13 changed files with 1,027 additions and 0 deletions.
50 changes: 50 additions & 0 deletions API_GatewayWeb/DataStructures/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package DataStructures

import (
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)

type User struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Name string `json:"name" bson:"name"`
Lastname string `json:"lastname" bson:"lastname"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Phone string `json:"phone" bson:"phone"`
Role string `json:"role" bson:"role"`
}

type Restaurant struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Name string `json:"name" bson:"name"`
Address string `json:"address" bson:"address"`
Contact string `json:"contact" bson:"contact"`
}

type Product struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
RestaurantId primitive.ObjectID `json:"restaurantId" bson:"restaurantId"`
Name string `json:"name" bson:"name"`
Price int32 `json:"price" bson:"price"` // in cents
}

type Order struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
SellerId primitive.ObjectID `json:"sellerId" bson:"sellerId"`
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"`
Status int8 `json:"status" bson:"status"`
OrderDate time.Time `json:"orderDate" bson:"orderDate"`
PaymentType int8 `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
}
74 changes: 74 additions & 0 deletions API_GatewayWeb/DataStructures/order_service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
syntax = "proto3";

package com.blazc;

option go_package = "com.blazc";

service OrderService {
rpc CreateOrder (Order) returns (Confirmation) {}
rpc GetOrder (GetOrderRequest) returns (Order) {}
rpc UpdateOrder (Order) returns (Confirmation) {}
rpc GetOrdersBySeller (GetOrdersRequest) returns (stream Order) {}
rpc GetOrdersByDeliveryPerson (GetOrdersRequest) returns (stream Order) {}
rpc DeleteOrder (DeleteOrderRequest) returns (Confirmation) {}
rpc Health (Empty) returns (Confirmation) {}
}

message Empty {}

message Confirmation {
string message = 2;
string error = 3;
}

message GetOrdersRequest {
string id = 1;
}

message GetOrderRequest {
string id = 1;
}

message DeleteOrderRequest {
string id = 1;
}

message Order {
string id = 1;
string sellerId = 2;
string deliveryPersonId = 3;
string address = 4;
string customerName = 5;
repeated OrderItem items = 6;
OrderStatus status = 7;
string orderDate = 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;
}

enum PaymentType {
CREDIT_CARD = 0;
CASH = 2;
}

enum OrderStatus {
PENDING = 0;
PREPARING = 1;
READY_FOR_PICKUP = 2;
DELIVERING = 3;
DELIVERED = 4;
CANCELLED = 5;
}
89 changes: 89 additions & 0 deletions API_GatewayWeb/HTTP_API/Product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package HTTP_API

import (
"API_GatewayWeb/DataStructures"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func (a *Controller) getAllProductsByRestaurantId(ctx *gin.Context) {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

restaurant, err := a.logic.GetAllProductsByRestaurantId(ctx.Request.Context(), id)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, restaurant)
}

func (a *Controller) getProductById(ctx *gin.Context) {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

product, err := a.logic.GetProductById(ctx.Request.Context(), id)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, product)
}

func (a *Controller) createProduct(ctx *gin.Context) {
var product DataStructures.Product
err := ctx.BindJSON(&product)
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

createdProduct, err := a.logic.CreateProduct(ctx.Request.Context(), product)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, createdProduct)
}

func (a *Controller) updateProduct(ctx *gin.Context) {
var product DataStructures.Product
err := ctx.BindJSON(&product)
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

updated, err := a.logic.UpdateProduct(ctx.Request.Context(), product)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, updated)
}

func (a *Controller) deleteProduct(ctx *gin.Context) {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

err = a.logic.DeleteProduct(ctx.Request.Context(), id)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, gin.H{"message": "Product deleted"})
}
83 changes: 83 additions & 0 deletions API_GatewayWeb/HTTP_API/Restaurant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package HTTP_API

import (
"API_GatewayWeb/DataStructures"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)

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

ctx.JSON(200, restaurants)
}

func (a *Controller) getRestaurantById(ctx *gin.Context) {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

restaurant, err := a.logic.GetRestaurantById(ctx.Request.Context(), id)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, restaurant)
}

func (a *Controller) createRestaurant(ctx *gin.Context) {
var restaurant DataStructures.Restaurant
err := ctx.BindJSON(&restaurant)
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

createdRestaurant, err := a.logic.CreateRestaurant(ctx.Request.Context(), restaurant)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, createdRestaurant)
}

func (a *Controller) updateRestaurant(ctx *gin.Context) {
var restaurant DataStructures.Restaurant
err := ctx.BindJSON(&restaurant)
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

updated, err := a.logic.UpdateRestaurant(ctx.Request.Context(), restaurant)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, updated)
}

func (a *Controller) deleteRestaurant(ctx *gin.Context) {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

err = a.logic.DeleteRestaurant(ctx.Request.Context(), id)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, gin.H{})
}
37 changes: 37 additions & 0 deletions API_GatewayWeb/HTTP_API/Routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package HTTP_API

import "github.com/gin-gonic/gin"

func (a *Controller) registerRoutes(engine *gin.Engine) {

api := engine.Group("/api/v1")
api.GET("/", a.Ping)

a.registerUserRoutes(api.Group("/users"))
a.registerRestaurantRoutes(api.Group("/restaurants"))
a.registerProductRoutes(api.Group("/products"))
}

func (a *Controller) registerUserRoutes(api *gin.RouterGroup) {
api.GET("/", a.getAllUsers)
api.POST("/", a.createUser)
api.GET("/:id", a.getUserById)
api.PUT("/:id", a.updateUser)
api.DELETE("/:id", a.deleteUser)
}

func (a *Controller) registerRestaurantRoutes(api *gin.RouterGroup) {
api.GET("/", a.getAllRestaurants)
api.POST("/", a.createRestaurant)
api.GET("/:id", a.getRestaurantById)
api.PUT("/:id", a.updateRestaurant)
api.DELETE("/:id", a.deleteRestaurant)
}

func (a *Controller) registerProductRoutes(api *gin.RouterGroup) {
api.GET("/restaurant/:id", a.getAllProductsByRestaurantId)
api.POST("/", a.createProduct)
api.GET("/:id", a.getProductById)
api.PUT("/:id", a.updateProduct)
api.DELETE("/:id", a.deleteProduct)
}
Loading

0 comments on commit 2eb4bd6

Please sign in to comment.