/// <summary> /// The CreateOrder /// </summary> /// <param name="order">The obj<see cref="Order"/></param> /// <param name="userName">The userName<see cref="string"/></param> /// <param name="transaction">The transaction<see cref="TransactionalInformation"/></param> /// <returns>The <see cref="Order"/></returns> public Order CreateOrder(Order order, string userName, out TransactionalInformation transaction) { transaction = new TransactionalInformation(); ShoppingCartBusinessService shoppingCartBusinessService = ShoppingCartBusinessService.GetCart(userName); try { OrderBusinessRules orderBusinessRules = new OrderBusinessRules(); ValidationResult results = orderBusinessRules.Validate(order); bool validationSucceeded = results.IsValid; IList <ValidationFailure> failures = results.Errors; if (validationSucceeded == false) { transaction = ValidationErrors.PopulateValidationErrors(failures); return(order); } _dataService.CreateSession(); var insertParam = new DynamicParameters(); insertParam.Add("@CustomerName", order.CustomerName); insertParam.Add("@CreatedBy", order.CreatedBy); insertParam.Add("@CreatedDate", order.CreatedDate); insertParam.Add("@CustomerAddress", order.CustomerAddress); insertParam.Add("@CustomerEmail", order.CustomerEmail); insertParam.Add("@CustomerMessage", order.CustomerMessage); insertParam.Add("@CustomerMobile", order.CustomerMobile); insertParam.Add("@PaymentMethod", order.PaymentMethod); insertParam.Add("@PaymentStatus", order.PaymentStatus); insertParam.Add("@Status", order.Status); insertParam.Add("@Total", order.Total); insertParam.Add("@ID", dbType: DbType.Int32, direction: ParameterDirection.Output); _dataService.OrderRepository.Insert(OrderScript.Insert, insertParam, CommandType.Text); order.ID = insertParam.Get <int>("@ID"); CreateOrderDetail(order, shoppingCartBusinessService); transaction.ReturnStatus = true; transaction.ReturnMessage.Add("successfull"); } catch (Exception ex) { string errorMessage = ex.Message; transaction.ReturnMessage.Add(errorMessage); transaction.ReturnStatus = false; } finally { _dataService.CloseSession(); } return(order); }
/// <summary> /// The CreateOrderDetail /// </summary> /// <param name="order">The order<see cref="Order"/></param> /// <param name="shoppingCartBusinessService">The shoppingCartBusinessService<see cref="ShoppingCartBusinessService"/></param> /// <returns>The <see cref="int"/></returns> private int CreateOrderDetail(Order order, ShoppingCartBusinessService shoppingCartBusinessService) { var cartItems = shoppingCartBusinessService.GetCartItems(); // Iterate over the items in the cart, adding the order details for each foreach (var item in cartItems) { var orderDetail = new OrderDetail { OrderID = order.ID, ProductID = (int)item.ProductId, Quantitty = item.Count }; // Set the order total of the shopping cart var insertOrderDetailsParam = new DynamicParameters(); insertOrderDetailsParam.Add("@OrderID", order.ID); insertOrderDetailsParam.Add("@ProductId", (int)item.ProductId); insertOrderDetailsParam.Add("@Quantitty", item.Count); _dataService.OrderDetailRepository.Insert(OrderDetailsScript.Insert, insertOrderDetailsParam, CommandType.Text); } // Set the order's total to the orderTotal count order.Total = shoppingCartBusinessService.GetTotalPrice(); var insertParam = new DynamicParameters(); insertParam.Add("@Total", order.Total); insertParam.Add("@ID", order.ID); _dataService.OrderRepository.Update(OrderScript.UpdateTotal, insertParam, CommandType.Text); // Save the order _dataService.CommitTransaction(true); // Empty the shopping cart shoppingCartBusinessService.EmptyCart(); // Return the OrderId as the confirmation number return(order.ID); }