Skip to content

Commit

Permalink
Add get all products route
Browse files Browse the repository at this point in the history
  • Loading branch information
blaz-cerpnjak committed Apr 11, 2024
1 parent d30197c commit 9db4143
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions API_GatewayWeb/HTTP_API/Product.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) getAllProducts(ctx *gin.Context) {
products, err := a.logic.GetAllProducts(ctx.Request.Context())
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.JSON(200, products)
}

func (a *Controller) getAllProductsByRestaurantId(ctx *gin.Context) {
id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
if err != nil {
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 @@ -30,6 +30,7 @@ func (a *Controller) registerRestaurantRoutes(api *gin.RouterGroup) {
}

func (a *Controller) registerProductRoutes(api *gin.RouterGroup) {
api.GET("/", a.getAllProducts)
api.GET("/restaurant/:id", a.getAllProductsByRestaurantId)
api.POST("/", a.createProduct)
api.GET("/:id", a.getProductById)
Expand Down
25 changes: 25 additions & 0 deletions API_GatewayWeb/Logic/Product.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ import (
"net/http"
)

func (c *Controller) GetAllProducts(ctx context.Context) (products []DataStructures.Product, err error) {
url := fmt.Sprintf("%s/product", getEnv("RESTAURANTS_API", "http://localhost:8082"))

request, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err.Error())
return
}

response, err := c.httpClient.Do(request)
if err != nil {
fmt.Println(err.Error())
return
}
defer response.Body.Close()

err = json.NewDecoder(response.Body).Decode(&products)
if err != nil {
fmt.Println(err.Error())
return nil, err
}

return
}

func (c *Controller) GetAllProductsByRestaurantId(ctx context.Context, id primitive.ObjectID) (products []DataStructures.Product, err error) {
url := fmt.Sprintf("%s/product/restaurant/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), id.Hex())

Expand Down

0 comments on commit 9db4143

Please sign in to comment.