Exemple #1
0
 public void PlaceOrder(Models.Order order)
 {
     Entities.StoreOrder o = new Entities.StoreOrder();
     if (order.Location.LocationID != null)
     {
         o.LocationId = (int)order.Location.LocationID;
     }
     if (order.Customer.CustomerID != null)
     {
         o.CustomerId = (int)order.Customer.CustomerID;
     }
     o.CheckedOut = DateTime.Now;
     ctx.StoreOrders.Add(o);
     foreach (Models.Item i in order.Items)
     {
         Entities.OrderItem oi = new Entities.OrderItem();
         if (i.Product.ProductID != null)
         {
             oi.ProductId = (int)i.Product.ProductID;
         }
         oi.Quantity = i.Quantity;
         o.OrderItems.Add(oi);
     }
     ctx.SaveChanges();
     try {
         transaction.Commit();
         using var log = new LoggerConfiguration()
                         .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day, shared: true)
                         .CreateLogger();
         log.Information("TRANSACTION: Committed");
         transaction.Dispose();
         transaction = ctx.Database.BeginTransaction();
     } catch (Exception e) {
         Console.WriteLine(e.StackTrace);
         using var log = new LoggerConfiguration()
                         .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day, shared: true)
                         .CreateLogger();
         log.Information("TRANSACTION: Rolled back due to database throwing exception");
         transaction.Rollback();
     }
 }
        public Models.Order ParseOrder(Entities.StoreOrder order)
        {
            Models.Order o = new Models.Order();
            o.OrderID    = order.OrderId;
            o.Customer   = ParseCustomer(order.Customer);
            o.Location   = ParseLocation(order.Location);
            o.CheckedOut = order.CheckedOut != null;
            if (o.CheckedOut)
            {
                o.CheckoutTimestamp = (DateTime)order.CheckedOut;
            }
            List <Models.Item> items = new List <Models.Item>();

            foreach (Entities.OrderItem item in order.OrderItems)
            {
                Models.Item i = new Models.Item();
                i.Product  = ParseProduct(item.Product);
                i.Quantity = item.Quantity;
                items.Add(i);
            }
            o.Items = items;
            return(o);
        }