Example #1
0
 public bool CheckoutCustomer(Customer customer, Order order)
 {
     using (var db = new DbContextClass())
     {
         customer.CurrentOrder = null;
         order.date            = DateTime.Now;
         if (db.Orders.Contains(order))
         {
             db.Remove(order);
             db.Update(customer);
             // db.Database.ExecuteSqlRaw("UPDATE dbo.Customers SET CurrentOrderOrderId = null WHERE CustomerId = '" + customer.CustomerId + "'");
             db.SaveChanges();
             // Console.WriteLine("change? :" + );
         }
         db.Add(order);
         customer.FinishedOrders.Add(order);
         db.Update(customer);
         return(db.SaveChanges() > 0);
     }
 }
Example #2
0
 /// <summary>
 /// check if customer has an order saved in the db
 /// if yes, delete that order
 /// if no, just do nothing
 /// </summary>
 /// <param name="customer"></param>
 /// <returns></returns>
 internal bool DeleteOrderIfExists(Customer customer)
 {
     using (var db = new DbContextClass())
     {
         Order o = null;
         try
         {
             o = db.Orders.Where(o => o.OrderId == db.Customers.Where(c => c.CustomerId == customer.CustomerId).First().CurrentOrder.OrderId).First();
         }
         catch (System.Exception)
         {
             customer.CurrentOrder = null;
             return(true);
         }
         customer.CurrentOrder = null;
         // db.Database.ExecuteSqlRaw("UPDATE dbo.Customers SET CurrentOrderOrderId = null WHERE CustomerId = '" + customer.CustomerId + "'");
         db.Update(customer);
         // db.SaveChanges();
         db.Orders.Remove(o);
         return(db.SaveChanges() > 0);
     }
 }
Example #3
0
        public bool SaveCustomerChanges(Customer customer)
        {
            using (var db = new DbContextClass())
            {
                if (!db.Orders.Contains(customer.CurrentOrder))
                {
                    SaveNewOrder(customer.CurrentOrder);
                }
                else
                {
                    customer.CurrentOrder.Pizzas.ForEach(pizza =>
                    {
                        if (!db.Pizzas.Contains(pizza))
                        {
                            db.Pizzas.Add(pizza);
                        }
                        else
                        {
                            pizza.ToppingList.ForEach(topping =>
                            {
                                if (!db.Toppings.Contains(topping))
                                {
                                    db.Toppings.Add(topping);
                                }
                            });
                            if (!db.Crusts.Contains(pizza.PizzaCrust))
                            {
                                db.Crusts.Add(pizza.PizzaCrust);
                            }
                            if (!db.Sizes.Contains(pizza.PizzaSize))
                            {
                                db.Sizes.Add(pizza.PizzaSize);
                            }
                        }
                    });

                    db.SaveChanges();
                }
                // try
                // {
                //     // db.DbContextOptionsBuilder.EnableSensitiveDataLogging = true;
                //     db.Orders.Where(o => o.OrderId == customer.CurrentOrder.OrderId).First();
                // }
                // catch (System.Exception e)
                // {
                //     Console.WriteLine("couldn't find it " + e.Message + "\n" + e.StackTrace);
                //     SaveNewOrder(customer.CurrentOrder);
                // }
                try
                {
                    db.Update(customer.CurrentOrder);
                    db.Update(customer);
                }
                catch (Microsoft.EntityFrameworkCore.DbUpdateException e)
                {
                    // db.Orders.Add(customer.CurrentOrder);
                    // db.Update(customer);
                    Console.WriteLine("an error occurred while saving your changes: " + e.Message + "\n" + e.StackTrace);
                }
                catch (System.InvalidOperationException e)
                {
                    Console.WriteLine("an error occurred while saving your changes: " + e.Message + "\n" + e.StackTrace);
                }
                return(db.SaveChanges() > 0);
            }
        }