update backoffice to update inventory
Some checks failed
Build and Publish / BuildAndDeployAmd64 (push) Successful in 43s
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled

This commit is contained in:
2025-11-25 18:34:03 +01:00
parent 4dcc6fe1bd
commit f63ad5bc84

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"log" "log"
"net/http" "net/http"
@@ -10,8 +11,10 @@ import (
actor "git.tornberg.me/go-cart-actor/pkg/actor" actor "git.tornberg.me/go-cart-actor/pkg/actor"
"git.tornberg.me/go-cart-actor/pkg/cart" "git.tornberg.me/go-cart-actor/pkg/cart"
"git.tornberg.me/mats/go-redis-inventory/pkg/inventory"
"github.com/matst80/slask-finder/pkg/messaging" "github.com/matst80/slask-finder/pkg/messaging"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"github.com/redis/go-redis/v9"
) )
type CartFileInfo struct { type CartFileInfo struct {
@@ -71,11 +74,24 @@ func startMutationConsumer(ctx context.Context, conn *amqp.Connection, hub *Hub)
return nil return nil
} }
var redisAddress = os.Getenv("REDIS_ADDRESS")
var redisPassword = os.Getenv("REDIS_PASSWORD")
func main() { func main() {
dataDir := envOrDefault("DATA_DIR", "data") dataDir := envOrDefault("DATA_DIR", "data")
addr := envOrDefault("ADDR", ":8080") addr := envOrDefault("ADDR", ":8080")
amqpURL := os.Getenv("AMQP_URL") 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) _ = os.MkdirAll(dataDir, 0755)
reg := cart.NewCartMultationRegistry() reg := cart.NewCartMultationRegistry()
@@ -89,6 +105,26 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("GET /carts", fs.CartsHandler) mux.HandleFunc("GET /carts", fs.CartsHandler)
mux.HandleFunc("GET /cart/{id}", fs.CartHandler) 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("/promotions", fs.PromotionsHandler)
mux.HandleFunc("/vouchers", fs.VoucherHandler) mux.HandleFunc("/vouchers", fs.VoucherHandler)
mux.HandleFunc("/promotion/{id}", fs.PromotionPartHandler) mux.HandleFunc("/promotion/{id}", fs.PromotionPartHandler)