Exemplo n.º 1
0
        public void PlaceOrder(int orderId, Guid storeId)
        {
            CookieContext context = _contextFactory.Context(storeId);

            if (orderId == 0) //just to check that this is the new order with id 0
            {
                var newOrder = _cache.GetString(storeId.ToString());
                if (!string.IsNullOrEmpty(newOrder))
                {
                    var order = JsonConvert.DeserializeObject <Order>(newOrder);

                    foreach (var line in order.OrderLines)
                    {
                        //have to get a reference to the actual cookie, not the attached one, otherwise EF will protest
                        line.Cookie = context.Cookies.Where(c => c.Id == line.Cookie.Id).FirstOrDefault();
                    }

                    order.Status = "Placed";

                    //have to get a reference to the actual store, not the attached one, otherwise EF will protest
                    order.Store = context.Stores.Where(s => s.Id == order.Store.Id).FirstOrDefault();

                    context.Orders.Add(order);

                    if (context.SaveChanges() > 0) //check for success
                    {
                        //if all went well, remove the item from cache
                        _cache.Remove(storeId.ToString());
                    }
                }
            }
        }
Exemplo n.º 2
0
 public void CancelOrder(int orderId, Guid storeId)
 {
     if (orderId == 0)
     {
         //cancel the order in cache
         _cache.Remove(storeId.ToString());
     }
     else
     {
         //the order is in the database, remove it
         var order = _context.Orders.Where(o => o.Id == orderId).FirstOrDefault();
         if (order != null)
         {
             _context.Remove(order);
             _context.SaveChanges();
         }
     }
 }
Exemplo n.º 3
0
        public void SyncStoresToDatabases()
        {
            var storesToSync = _currentContext.Stores.ToList();

            var servers = _currentContext.DatabaseServers.ToList();

            foreach (var server in servers)
            {
                var optionsBuilder = new DbContextOptionsBuilder();
                optionsBuilder.UseSqlServer(GetDataStoreForDatabaseServer(server));
                var context = new CookieContext(optionsBuilder.Options);

                try
                {
                    var stores = context.Stores.ToList();

                    //loop over the stores to sync
                    foreach (var store in storesToSync)
                    {
                        //if the store is not in the current context's stores list
                        if (stores.Where(s => s.Name == store.Name).FirstOrDefault() == null)
                        {
                            //build a new storeobject, so that the connection to the old DBContext is gone
                            Store storeToAdd = new Store
                            {
                                Name           = store.Name,
                                Country        = store.Country,
                                DatabaseServer = context.DatabaseServers.Where(d => d.Id == store.DatabaseServer.Id).FirstOrDefault(),
                                Orders         = store.Orders
                            };

                            context.Stores.Add(storeToAdd);
                            context.SaveChanges();
                        }
                    }
                }
                catch (SqlException ex)
                {
                    //database cannot be openened for some reason
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds cookies to orders
        /// </summary>
        /// <param name="cookieGuidId"></param>
        public void AddCookieToOrder(string cookieGuidId)
        {
            var cookieId = new Guid(cookieGuidId);

            //get the oder with the status new (if any)
            Order currentOrder = _context.Orders
                                 .Where(o => o.Status == "New")
                                 //.Include(o => o.OrderLines)
                                 //.ThenInclude(OrderLine => OrderLine.Cookie)
                                 .FirstOrDefault();

            //if there is a order with status new
            if (currentOrder != null)
            {
                //loop through the lines of the order
                //and check if the cookie that we want to add is
                //already in there
                bool orderLineExists = false;
                foreach (var lines in currentOrder.OrderLines)
                {
                    if (lines.Cookie.Id == cookieId)
                    {
                        lines.Quantity++;
                        orderLineExists = true;

                        currentOrder.Price += lines.Cookie.Price;
                    }
                }

                //if the cookie is new in this order
                if (!orderLineExists)
                {
                    //get the cookie
                    var cookie = _context.Cookies.Where(c => c.Id == cookieId).FirstOrDefault();

                    //add it to a new orderline
                    currentOrder.OrderLines.Add(new OrderLine
                    {
                        Cookie   = cookie,
                        Quantity = 1
                    });

                    currentOrder.Price += cookie.Price;
                }

                _context.Update(currentOrder);
                _context.SaveChanges();
            }
            else
            {
                //if there is no order with status new
                //create one
                currentOrder        = new Order();
                currentOrder.Date   = DateTimeOffset.Now;
                currentOrder.Status = "New";

                var cookie = _context.Cookies.Where(c => c.Id == cookieId).FirstOrDefault();

                currentOrder.OrderLines.Add(new OrderLine
                {
                    Cookie   = cookie,
                    Quantity = 1
                });

                currentOrder.Price += cookie.Price;

                _context.Add(currentOrder);
                _context.SaveChanges();
            }
        }