Skip to content

Instantly share code, notes, and snippets.

@plentysmart
Last active August 29, 2015 14:01
Show Gist options
  • Save plentysmart/6ae9e9efeffb02d86ba5 to your computer and use it in GitHub Desktop.
Save plentysmart/6ae9e9efeffb02d86ba5 to your computer and use it in GitHub Desktop.
public class CartService : ICartService
{
private readonly ICartRepository _cartRepository;
private readonly IProductRepository _productRepository;
public CartService(ICartRepository cartRepository, IProductRepository productRepository)
{
_cartRepository = cartRepository;
_productRepository = productRepository;
}
public void AddToCart(long cartId, long productId, int quantity)
{
var cart = _cartRepository.Get(cartId);
if (cart == null)
throw new ArgumentException("Cart not found. CartId: " + cartId);
var product = _productRepository.Get(productId);
if (product == null)
throw new ArgumentException("Product not found. ProductId: " + productId);
var cartItem = new CartItem(cart.Id, productId, quantity);
cart.Items.Add(cartItem);
_cartRepository.Save(cart);
}
}
public class Customer
{
public long Id { get; private set; }
public string Name { get; private set; }
}
public class CartItem
{
public CartItem(long id, long productId, int quantity)
{
Id = id;
ProductId = productId;
Quantity = quantity;
}
public long Id { get; private set; }
public long CartId { get; private set; }
public long ProductId { get; private set; }
public int Quantity { get; set; }
}
public class Cart
{
public Cart()
{
Items = new List<CartItem>();
}
public long Id { get; private set; }
public long CustomerId { get; set; }
public List<CartItem> Items { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment