update inventory service
This commit is contained in:
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user