diff --git a/cmd/backoffice/main.go b/cmd/backoffice/main.go index c86624d..5e51073 100644 --- a/cmd/backoffice/main.go +++ b/cmd/backoffice/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "errors" "log" "net/http" @@ -10,8 +11,10 @@ import ( actor "git.tornberg.me/go-cart-actor/pkg/actor" "git.tornberg.me/go-cart-actor/pkg/cart" + "git.tornberg.me/mats/go-redis-inventory/pkg/inventory" "github.com/matst80/slask-finder/pkg/messaging" amqp "github.com/rabbitmq/amqp091-go" + "github.com/redis/go-redis/v9" ) type CartFileInfo struct { @@ -71,11 +74,24 @@ func startMutationConsumer(ctx context.Context, conn *amqp.Connection, hub *Hub) return nil } +var redisAddress = os.Getenv("REDIS_ADDRESS") +var redisPassword = os.Getenv("REDIS_PASSWORD") + func main() { dataDir := envOrDefault("DATA_DIR", "data") addr := envOrDefault("ADDR", ":8080") amqpURL := os.Getenv("AMQP_URL") + rdb := redis.NewClient(&redis.Options{ + Addr: redisAddress, + Password: redisPassword, + DB: 0, + }) + inventoryService, err := inventory.NewRedisInventoryService(rdb) + if err != nil { + log.Fatalf("Error creating inventory service: %v\n", err) + } + _ = os.MkdirAll(dataDir, 0755) reg := cart.NewCartMultationRegistry() @@ -89,6 +105,26 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("GET /carts", fs.CartsHandler) mux.HandleFunc("GET /cart/{id}", fs.CartHandler) + mux.HandleFunc("PUT /inventory/{locationId}/{sku}", func(w http.ResponseWriter, r *http.Request) { + inventoryLocationId := r.PathValue("locationId") + inventorySku := r.PathValue("sku") + pipe := rdb.Pipeline() + var payload struct { + Quantity int64 `json:"quantity"` + } + err := json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + http.Error(w, "invalid payload", http.StatusBadRequest) + return + } + inventoryService.UpdateInventory(r.Context(), pipe, inventory.SKU(inventorySku), inventory.LocationID(inventoryLocationId), payload.Quantity) + _, err = pipe.Exec(r.Context()) + if err != nil { + http.Error(w, "failed to update inventory", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + }) mux.HandleFunc("/promotions", fs.PromotionsHandler) mux.HandleFunc("/vouchers", fs.VoucherHandler) mux.HandleFunc("/promotion/{id}", fs.PromotionPartHandler)