Skip to content

Commit

Permalink
basket-api: error handling return 404 when customer cart does not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jurabek committed Jan 15, 2024
1 parent 8ff6640 commit 440b3c2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
Binary file modified src/backend/services/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions src/backend/services/basket-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/swaggo/files v1.0.0
Expand Down
1 change: 1 addition & 0 deletions src/backend/services/basket-api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/jurabek/basket.api/internal/models"
"github.com/jurabek/basket.api/internal/repositories"
"github.com/pkg/errors"
)

type GetCreateDeleter interface {
Expand Down Expand Up @@ -75,8 +77,11 @@ func (bc *BasketHandler) Get(c *gin.Context) {
id := c.Param("id")

result, err := bc.BasketRepository.Get(c.Request.Context(), id)

if err != nil {
if errors.Is(err, repositories.ErrCartNotFound) {
c.JSON(http.StatusNotFound, models.NewHTTPError(http.StatusNotFound, errors.Wrap(err, "itemID: "+id)))
return
}
httpError := models.NewHTTPError(http.StatusBadRequest, err)
c.JSON(http.StatusBadRequest, httpError)
return
Expand Down
13 changes: 7 additions & 6 deletions src/backend/services/basket-api/internal/models/basket_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package models

// BasketItem items for users basket
type BasketItem struct {
FoodID int `json:"food_id"`
UnitPrice float32 `json:"unit_price"`
OldUnitPrice float32 `json:"old_unit_price"`
Quantity int `json:"quantity"`
Picture string `json:"picture"`
FoodName string `json:"food_name"`
FoodID int `json:"food_id"`
UnitPrice float32 `json:"unit_price"`
OldUnitPrice float32 `json:"old_unit_price"`
Quantity int `json:"quantity"`
Picture string `json:"picture"`
FoodName string `json:"food_name"`
FoodDescription string `json:"food_description"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repositories
import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/jurabek/basket.api/internal/models"
Expand All @@ -19,6 +20,8 @@ func NewRedisBasketRepository(client *redis.Client) *RedisBasketRepository {
return &RedisBasketRepository{client: client}
}

var ErrCartNotFound = errors.New("cart not found");

// Get returns CustomerBasket otherwise nill
func (r *RedisBasketRepository) Get(ctx context.Context, customerID string) (*models.CustomerBasket, error) {
var (
Expand All @@ -27,6 +30,9 @@ func (r *RedisBasketRepository) Get(ctx context.Context, customerID string) (*mo
)
data, err := r.client.Get(ctx, customerID).Bytes()
if err != nil {
if(err == redis.Nil){
return nil, ErrCartNotFound
}
return nil, fmt.Errorf("error getting key %s: %v", customerID, err)
}

Expand Down

0 comments on commit 440b3c2

Please sign in to comment.