update
This commit is contained in:
@@ -2,6 +2,7 @@ package inventory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -80,6 +81,32 @@ func (s *RedisInventoryService) UpdateInventory(ctx context.Context, rdb redis.P
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) IncrementInventory(ctx context.Context, rdb redis.Pipeliner, sku SKU, locationID LocationID, quantity int64) error {
|
||||
key := getInventoryKey(sku, locationID)
|
||||
cmd := rdb.IncrBy(ctx, key, quantity)
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
func (s *RedisInventoryService) SendInventoryChanged(ctx context.Context, sku SKU, locationID LocationID) error {
|
||||
key := getInventoryKey(sku, locationID)
|
||||
currentValCmd := s.client.Get(ctx, key)
|
||||
if err := currentValCmd.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
currentVal, err := currentValCmd.Int64()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payloadMap := map[string]int64{key: currentVal}
|
||||
payloadBytes, err := json.Marshal(payloadMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload := string(payloadBytes)
|
||||
cmd := s.client.Publish(ctx, "inventory_changed", payload)
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInsufficientInventory = errors.New("insufficient inventory")
|
||||
ErrInvalidQuantity = errors.New("invalid quantity")
|
||||
|
||||
Reference in New Issue
Block a user