Complete refactor to new grpc control plane and only http proxy for carts #4
@@ -1,51 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// mutation_add_request.go
|
|
||||||
//
|
|
||||||
// Registers the AddRequest mutation. This mutation is a higher-level intent
|
|
||||||
// (add by SKU + quantity) which may translate into either:
|
|
||||||
// - Increasing quantity of an existing line (same SKU), OR
|
|
||||||
// - Creating a new item by performing a product lookup (via getItemData inside CartGrain.AddItem)
|
|
||||||
//
|
|
||||||
// Behavior:
|
|
||||||
// - Validates non-empty SKU and quantity > 0
|
|
||||||
// - If an item with the SKU already exists: increments its quantity
|
|
||||||
// - Else delegates to CartGrain.AddItem (which itself produces an AddItem mutation)
|
|
||||||
// - Totals recalculated automatically (WithTotals)
|
|
||||||
//
|
|
||||||
// NOTE:
|
|
||||||
// - This handler purposely avoids duplicating the detailed AddItem logic;
|
|
||||||
// it reuses CartGrain.AddItem which then flows through the AddItem mutation
|
|
||||||
// registry handler.
|
|
||||||
// - Double total recalculation can occur (AddItem has WithTotals too), but
|
|
||||||
// is acceptable for clarity. Optimize later if needed.
|
|
||||||
//
|
|
||||||
// Potential future improvements:
|
|
||||||
// - Stock validation before increasing quantity
|
|
||||||
// - Reservation logic or concurrency guards around stock updates
|
|
||||||
// - Coupon / pricing rules applied conditionally during add-by-sku
|
|
||||||
|
|
||||||
// func HandleAddRequest(g *CartGrain, m *messages.AddRequest) error {
|
|
||||||
// if m == nil {
|
|
||||||
// return fmt.Errorf("AddRequest: nil payload")
|
|
||||||
// }
|
|
||||||
// if m.Sku == "" {
|
|
||||||
// return fmt.Errorf("AddRequest: sku is empty")
|
|
||||||
// }
|
|
||||||
// if m.Quantity < 1 {
|
|
||||||
// return fmt.Errorf("AddRequest: invalid quantity %d", m.Quantity)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Existing line: accumulate quantity only.
|
|
||||||
// if existing, found := g.FindItemWithSku(m.Sku); found {
|
|
||||||
// existing.Quantity += int(m.Quantity)
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
// data, err := GetItemAddMessage(m.Sku, int(m.Quantity), m.Country, m.StoreId)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// return AddItem(g, data)
|
|
||||||
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
@@ -182,10 +182,15 @@ func (s *PoolServer) HandleAddRequest(w http.ResponseWriter, r *http.Request, id
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply, err := s.ApplyLocal(id, &addRequest)
|
msg, err := GetItemAddMessage(addRequest.Sku, int(addRequest.Quantity), addRequest.Country, addRequest.StoreId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
reply, err := s.ApplyLocal(id, msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return s.WriteResult(w, reply)
|
return s.WriteResult(w, reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ message AddRequest {
|
|||||||
optional string storeId = 4;
|
optional string storeId = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SetCartRequest {
|
message ClearCartRequest {
|
||||||
repeated AddRequest items = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message AddItem {
|
message AddItem {
|
||||||
|
|||||||
Reference in New Issue
Block a user