/// <summary> /// The AddtoCart /// </summary> /// <param name="productId">The productId<see cref="int"/></param> /// <param name="count">The count<see cref="int"/></param> public void AddtoCart(int productId, int count = 1) { CheckLoginStatus(); ProductItem stock = _marketRepository.GetStock(productId); if (stock == null) { throw new InvalidOperationException($"Product {productId} is out of stock."); } if (count <= 0) { throw new ArgumentOutOfRangeException("count", "Count cannot less than 1."); } if (stock.Count < count) { throw new ArgumentOutOfRangeException("count", $"There is no enough product in stock. left {stock.Count}, need {count}"); } var locker = LockFile("cart.lk"); _customerRepository.AddToCart(ShoppingCartId, productId, count); UnlockFile(locker); }
/// <summary> /// The RemoveProduct /// </summary> /// <param name="productID">The productID<see cref="int"/></param> public void RemoveProduct(int productID) { var locker = LockFile("product.lk"); var product = _marketRepository.FindProduct(productID); if (product == null) { UnlockFile(locker); throw new InvalidOperationException("This product does not exist."); } var locker2 = LockFile("stock.lk"); var stock = _marketRepository.GetStock(productID); if (stock != null) { UnlockFile(locker2); UnlockFile(locker); throw new InvalidOperationException($"There are still some {product.Name}({stock.Count}) in the stock."); } _marketRepository.RemoveProduct(product); UnlockFile(locker2); UnlockFile(locker); }