Skip to content

Commit

Permalink
Adding some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-iacovelli committed Oct 22, 2024
1 parent 10d2edd commit 66bae95
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions foodDetails/controller/FoodDetailsController.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
foodDetailsService "food-details-integrator-be/foodDetails/service"
"github.com/apsystole/log"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
Expand All @@ -27,6 +28,7 @@ func (f foodDetailsController) GetFoodDetails(c *gin.Context) {
result, err := f.foodDetailsService.GetProductDetails(barcode)

if err != nil {
log.Error(err)
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
Expand All @@ -43,12 +45,14 @@ func (f foodDetailsController) GetKcalForQuantity(c *gin.Context) {
quantityConverted, errParse := strconv.ParseFloat(quantity, 64)

if errParse != nil {
log.Error(errParse)
c.JSON(http.StatusBadRequest, gin.H{
"error": errParse.Error(),
})
}
result, err := f.foodDetailsService.GetKcalForFoodQuantity(barcode, quantityConverted, unit)
if err != nil {
log.Error(err)
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
Expand Down
9 changes: 6 additions & 3 deletions foodDetails/service/FoodDetailsService.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package service

import (
"fmt"
"food-details-integrator-be/foodDetails/model"
"github.com/apsystole/log"
"github.com/mashingan/smapping"
"github.com/openfoodfacts/openfoodfacts-go"
"os"
Expand Down Expand Up @@ -33,10 +33,11 @@ func (s FoodDetailsService) GetProductDetails(barcode string) (model.FoodDetails
if err != nil {
foodDetails, err = s.getFoodDetailsFromApi(barcode)
} else {
fmt.Println("cache hit")
log.Infoln("Data from cache 🚀for barcode: ", barcode)
}
err = cs.SetObject(barcode, foodDetails)
} else {
log.Infoln("Redis is disabled, fetching data from API")
foodDetails, err = s.getFoodDetailsFromApi(barcode)
}
return foodDetails, err
Expand All @@ -59,14 +60,16 @@ func (s FoodDetailsService) getFoodDetailsFromApi(barcode string) (model.FoodDet
api := openfoodfacts.NewClient("world", "", "")
foodDetails := model.FoodDetails{}

log.Infoln("Fetching data from API for barcode: ", barcode)

if os.Getenv("IS_SANDBOX") == "true" {
api.Sandbox()
}

product, err := api.Product(barcode)

if err != nil {
fmt.Printf("error %s \n", err)
log.Errorf("Error while fetching data from API for barcode: %s, error: %s", barcode, err)
return model.FoodDetails{}, err
} else {
mappedField := smapping.MapTags(product, "json")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module food-details-integrator-be
go 1.18

require (
github.com/apsystole/log v0.3.0
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/cache/v8 v8.4.3
github.com/go-redis/redis/v8 v8.11.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apsystole/log v0.3.0 h1:uES5wJvdvzL6Q4KXJt6OQnDtmPCbohFkdx6v7OpbDC4=
github.com/apsystole/log v0.3.0/go.mod h1:BBvif4d0jOPNYgXzNLVdWE//WjefJ9Hwfy/2Y5U+SV0=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down

0 comments on commit 66bae95

Please sign in to comment.