/// <summary> /// To Delete Order /// </summary> /// <param name="OrderId"></param> /// <returns></returns> public bool DeleteOrder(Guid OrderId) { try { using (var dbContext = new OrderManagementEntities()) { var orderInfo = dbContext.Orders.FirstOrDefault(x => x.UUID == OrderId); if (orderInfo == null) { throw new Exception("Order Not found or Invalid Order"); } orderInfo.IsDeleted = true; dbContext.Orders.Add(orderInfo); dbContext.Entry(orderInfo).State = EntityState.Modified; dbContext.SaveChanges(); //Updating Available Quantity of products UpdateCanceledProductQuantity(dbContext, orderInfo); return(true); } } catch (Exception ex) { throw ex; } }
/// <summary> /// To Update an Order /// </summary> /// <param name="UpdateOrderModel"></param> /// <returns></returns> public bool UpdateOrder(UpdateOrderModel UpdateOrderModel) { try { using (var dbContext = new OrderManagementEntities()) { var products = new List <Product>(); var OrderStatusParentID = 1; var userInfo = ValidateUser(UpdateOrderModel.UserID, dbContext); if (!userInfo.isBuyer) { throw new Exception("Invalid User"); } var orderInfo = dbContext.Orders.FirstOrDefault(x => x.UUID == UpdateOrderModel.OrderId); if (orderInfo == null) { throw new Exception("Order Not found or Invalid Order"); } if (orderInfo.UserID != userInfo.Id) { throw new Exception("Orders are not associated with give user"); } var orderState = GetLookupValues(OrderStatusParentID, dbContext) .FirstOrDefault(x => x.UUID == UpdateOrderModel.OrderState); if (orderState == null) { throw new Exception("Invalid Order Status"); } orderInfo.ShippingAddress = UpdateOrderModel.ShippingAddress; orderInfo.Items = xmlSerialize(UpdateOrderModel.Items, dbContext, ref products, orderInfo); orderInfo.OrderState = orderState.Id; dbContext.Orders.Add(orderInfo); dbContext.Entry(orderInfo).State = EntityState.Modified; dbContext.SaveChanges(); //Updating Available Quantity of products UpdateProductsAvailableQuantity(products); return(true); } } catch (Exception ex) { throw ex; } }
/// <summary> /// To Update Products Available Quantity /// </summary> /// <param name="Products"></param> private void UpdateProductsAvailableQuantity(List <Product> Products) { try { using (var dbContext = new OrderManagementEntities()) { foreach (var pro in Products) { dbContext.Products.Add(pro); dbContext.Entry(pro).State = EntityState.Modified; dbContext.SaveChanges(); } } } catch (Exception ex) { throw ex; } }