From 7e2726244fa748a32b9a005c4ae0d51485445d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mats=20T=C3=B6rnberg?= Date: Wed, 26 Nov 2025 18:54:07 +0100 Subject: [PATCH] update inventory service --- cmd/inventory/main.go | 43 +++++++++++++++++++++++++++++-------------- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/cmd/inventory/main.go b/cmd/inventory/main.go index 0eb72d3..3e28be2 100644 --- a/cmd/inventory/main.go +++ b/cmd/inventory/main.go @@ -6,7 +6,6 @@ import ( "log" "net/http" "os" - "strings" "sync" "github.com/matst80/go-redis-inventory/pkg/inventory" @@ -19,7 +18,8 @@ import ( ) type Server struct { - service *inventory.RedisInventoryService + inventoryService *inventory.RedisInventoryService + reservationService *inventory.RedisCartReservationService } func (srv *Server) livezHandler(w http.ResponseWriter, r *http.Request) { @@ -33,17 +33,11 @@ func (srv *Server) readyzHandler(w http.ResponseWriter, r *http.Request) { } func (srv *Server) getInventoryHandler(w http.ResponseWriter, r *http.Request) { - // Parse path: /inventory/{sku}/{location} - path := r.URL.Path - parts := strings.Split(strings.Trim(path, "/"), "/") - if len(parts) != 3 || parts[0] != "inventory" { - http.Error(w, "Invalid path", http.StatusBadRequest) - return - } - sku := inventory.SKU(parts[1]) - locationID := inventory.LocationID(parts[2]) - quantity, err := srv.service.GetInventory(r.Context(), sku, locationID) + sku := inventory.SKU(r.PathValue("sku")) + locationID := inventory.LocationID(r.PathValue("locationId")) + + quantity, err := srv.inventoryService.GetInventory(r.Context(), sku, locationID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -54,6 +48,20 @@ func (srv *Server) getInventoryHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(response) } +func (srv *Server) getReservationHandler(w http.ResponseWriter, r *http.Request) { + sku := inventory.SKU(r.PathValue("sku")) + locationID := inventory.LocationID(r.PathValue("locationId")) + + summary, err := srv.reservationService.GetReservationSummary(r.Context(), sku, locationID) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(summary) +} + var country = "se" var redisAddress = "10.10.3.18:6379" var redisPassword = "slaskredis" @@ -87,12 +95,19 @@ func main() { return } - server := &Server{service: s} + r, err := inventory.NewRedisCartReservationService(rdb) + if err != nil { + log.Fatalf("Unable to connect to reservation redis: %v", err) + return + } + + server := &Server{inventoryService: s, reservationService: r} // Set up HTTP routes http.HandleFunc("/livez", server.livezHandler) http.HandleFunc("/readyz", server.readyzHandler) - http.HandleFunc("/inventory/", server.getInventoryHandler) + http.HandleFunc("/inventory/{sku}/{locationId}", server.getInventoryHandler) + http.HandleFunc("/reservations/{sku}/{locationId}", server.getReservationHandler) stockhandler := &StockHandler{ MainStockLocationID: inventory.LocationID(country), diff --git a/go.mod b/go.mod index 2572924..66f9013 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.25.4 require ( github.com/gogo/protobuf v1.3.2 github.com/google/uuid v1.6.0 - github.com/matst80/go-redis-inventory v0.0.0-20251125181530-7ebbc97e3841 + github.com/matst80/go-redis-inventory v0.0.0-20251126173508-51b30de2d86e github.com/matst80/slask-finder v0.0.0-20251125182907-9e57f193127a github.com/prometheus/client_golang v1.23.2 github.com/rabbitmq/amqp091-go v1.10.0 diff --git a/go.sum b/go.sum index 71b2570..3a3f984 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8 github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= github.com/matst80/go-redis-inventory v0.0.0-20251125181530-7ebbc97e3841 h1:CCHa+LuxwGOPPbIpuFYNT7VtC5rHjSulB337Y+Yyys8= github.com/matst80/go-redis-inventory v0.0.0-20251125181530-7ebbc97e3841/go.mod h1:d42+bHxhmMg73OQM+5i5U7vyEQfuVLJkt7QPsEbr8kI= +github.com/matst80/go-redis-inventory v0.0.0-20251126173508-51b30de2d86e h1:Z7A73W6jsxFuFKWvB1efQmTjs0s7+x2B7IBM2ukkI6Y= +github.com/matst80/go-redis-inventory v0.0.0-20251126173508-51b30de2d86e/go.mod h1:9P52UwIlLWLZvObfO29aKTWUCA9Gm62IuPJ/qv4Xvs0= github.com/matst80/slask-finder v0.0.0-20251125182907-9e57f193127a h1:EfUO5BNDK3a563zQlwJYTNNv46aJFT9gbSItAwZOZ/Y= github.com/matst80/slask-finder v0.0.0-20251125182907-9e57f193127a/go.mod h1:VIPNkIvU0dZKwbSuv75zZcB93MXISm2UyiIPly/ucXQ= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=