Files
go-cart-actor/pkg/inventory/memory_service.go
2025-11-04 12:48:41 +01:00

48 lines
1.1 KiB
Go

package inventory
import (
"errors"
"sync"
)
type MemoryInventoryService struct {
warehouses map[LocationID]*Warehouse
mu sync.RWMutex
}
func NewMemoryInventoryService() *MemoryInventoryService {
return &MemoryInventoryService{
warehouses: make(map[LocationID]*Warehouse),
}
}
func (s *MemoryInventoryService) AddWarehouse(warehouse *Warehouse) {
s.mu.Lock()
defer s.mu.Unlock()
s.warehouses[warehouse.ID] = warehouse
}
func (s *MemoryInventoryService) GetInventory(sku SKU, locationID LocationID) (*InventoryItem, error) {
s.mu.RLock()
defer s.mu.RUnlock()
warehouse, ok := s.warehouses[locationID]
if !ok {
return nil, errors.New("warehouse not found")
}
for _, item := range warehouse.Inventory {
if item.SKU == sku {
return &item, nil
}
}
return nil, errors.New("sku not found in warehouse")
}
func (s *MemoryInventoryService) ReserveInventory(req ...ReserveRequest) error {
// We'll implement the reservation logic using Lua script execution here
// For now, let's just return an error indicating it's not implemented
return errors.New("reservation not implemented yet")
}