public void ReserveProduct(string sessionId, int productId) { using (var transaction = _transactionFactory.StartTransaction()) { if (_productRepository.GetProduct(productId).Amount <= 0) { transaction.Rollback(); return; } var basket = _basketRepository.GetActiveBasketBySessionId(sessionId); if (basket == null) { var purchasesItems = new List <PurchasesItem>(); var newItem = new PurchasesItem(); newItem.Amount = 1; newItem.ProductId = productId; purchasesItems.Add(newItem); basket = new Basket(); basket.BasketCreation = DateTime.Now; basket.PurchasesItems = purchasesItems; basket.SessionId = sessionId; var product = _productRepository.GetProduct(productId); product.Amount--; _productRepository.UpdateProduct(product); _basketRepository.CreateBasket(basket); } else { var purchasesItem = basket.PurchasesItems.Find(p => { return(p.ProductId == productId); }); if (purchasesItem == null) { var newItem = new PurchasesItem(); newItem.Amount = 1; newItem.ProductId = productId; basket.PurchasesItems.Add(newItem); _basketRepository.UpdateBasket(basket); } else { purchasesItem.Amount++; _basketRepository.UpdateBasket(basket); } var product = _productRepository.GetProduct(productId); product.Amount--; _productRepository.UpdateProduct(product); } transaction.Commit(); } }
public IPersistable Save(ConfigureModel toSave) { using (var transaction = _transactionFactory.StartTransaction()) { _queryRunner.CreateOrUpdateProviderConfiguration(transaction, toSave); transaction.Commit(); } return(toSave); }
public void MakeOrder(Basket basket, Order order) { if (basket == null || order == null) { return; } using (var transaction = _transactionFactory.StartTransaction()) { basket.IsActive = false; _basketRepository.UpdateBasket(basket); order.BasketId = basket.Id; order.OrderDate = DateTime.Now; _orderRepository.CreateOrder(order); transaction.Commit(); } }