Skip to content

Instantly share code, notes, and snippets.

@pavsaund
Last active October 19, 2016 11:16
Show Gist options
  • Save pavsaund/abe1467d9b2aa3a2abdd2c34d2bdd03f to your computer and use it in GitHub Desktop.
Save pavsaund/abe1467d9b2aa3a2abdd2c34d2bdd03f to your computer and use it in GitHub Desktop.
Capturing Intent with code
Added extra tracing and logging whenever a customer adds an item to their cart.
This should help our operation team to monitor the system
public class ShoppingCartWithCustomerTraceLogging : ShoppingCart
{
public override void AddCartItem(CartItem item)
{
base.AddCartItem(item);
Track(CustomerAction.CartItemAddedToCart);
}
void Track(Action action)
{
CustomerTrackingService.SignalTotalInShoppingCartUpdated(action, CartTotal);
}
}
public class when_adding_a_cart_item
{
Because customer_service_and_our_customer_journey_tracker_needs_trace_info
= () => shoppingCart.AddCartItem(cartItem);
It sends_the_correct_message_to_the_event_aggregator
= () => MessageHandler.Messages.ShouldContain(message =>
message.type == CustomerActiom.CartItemAddedToCart);
}
  Feature: Add item to shopping cart
    ShoppingCart should have add item if it doesnt exist in cart
    ShoppingCart should increase quantity if item already in cart
    If there is no coffee left then money should be refunded

  Scenario: Add StockKeepingUnit 1234 with quantity 2 to shopping cart
    Given there are 2 items of StockKeepingUnit 1234 already in cart
    When I press the Add to cart button
    Then total number of items should be 4
    And TraceLogging should recieve message CustomerActiom.CartItemAddedToCart
public class When_adding_a_cart_item
{
[SetUp]
public void Because_Customer_service_and_our_customer_journey_tracker_needs_trace_info()
{
_shoppingCart.AddCartItem(cartItem);
}
[Test]
public void It_sends_the_correct_message_to_the_event_aggregator
{
var messagePlaced = MessageHandler.Messages.FirstOrDefault(message => message.Type == CustomerActiom.CartItemAddedToCart);
Assert.NotNull(messagePlaced);
Assert.Equal(messagePlaced.Type, CustomerAction.CartItemAddedToCart);
}
}
public class ShoppingCart
{
public IList<CartItem> Items { get; set; }
public decimal CartTotal { get; set; }
public void Add(CartItem item)
{
if (Items.Any(x => x.StockKeepingUnit == item.StockKeepingUnit))
{
var i = Items.First(x => x.StockKeepingUnit == item.StockKeepingUnit);
item.Qty += i.Qty;
}
else
{
Items.Add(item);
}
// Extra tracking we need to do for customer service and extra debug tracing
Track();
}
private void Track()
{
var t = Items.Sum(x => x.TotaPrice);
CartTotal = t;
CustomerTrackingService.TrackTotalInShoppingCart(t);
}
}
public class ShoppingCart
{
public IList<CartItem> Items { get; set; }
public decimal CartTotal { get { return Items.Sum(item => item.Total); } }
public void AddCartItemAndDoTracking(CartItem item)
{
AddOrIncreaseQuantityFor(item);
TrackForLoggingDebuggingAndCustomerServiceCenter(CustomerAction.CartItemAddedToCart);
}
private void AddOrIncreaseQuantityFor(CartItem addedItem)
{
if (CartContainsStockKeepingUnit(addedItem.StockKeepingUnit))
{
var itemInCart = Items.First(inCart => inCart.StockKeepingUnit == addedItem.StockKeepingUnit);
itemInCart.Quantity += addedItem.Quantity;
}
else
{
Items.Add(item);
}
}
bool CartContainsStockKeepingUnit(StockKeepingUnit sku)
{
return Items.Any(x => x.Sku == sku)
}
void TrackForLoggingDebuggingAndCustomerServiceCenter(Action action)
{
CustomerTrackingService.SignalTotalInShoppingCartUpdated(action, CartTotal);
}
}
public class ShoppingCartWithCustomerTraceLogging : ShoppingCart
{
/// <summary>
/// Preferred way to add item to a customers shopping cart
/// Will add the needed extra logging required.
/// </summary>
/// <param name="item"> A CartItem that contains the qty, sku, and item price and item total price</param>
public override void AddCartItem(CartItem item)
{
base.AddCartItem(item);
Track(CustomerAction.CartItemAddedToCart);
}
void Track(Action action)
{
CustomerTrackingService.SignalTotalInShoppingCartUpdated(action, CartTotal);
}
}
@pavsaund
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment