Пример #1
0
        public static void CreateOrderProcessTask([Queue("orders")] CloudQueue orderQueue)
        {
            Console.WriteLine("Starting Create Order Process Task");
            try
            {
                var builder = new ConfigurationBuilder();
                builder.Add(new JsonConfigurationSource("config.json"));
                var config           = builder.Build();
                var connectionString = config["Data:DefaultConnection:ConnectionString"];

                using (var context = new PartsUnlimitedContext(connectionString))
                {
                    var orders = context.Orders.Where(x => !x.Processed).ToList();
                    Console.WriteLine("Found {0} orders to process", orders.Count);

                    foreach (var order in orders)
                    {
                        var productIds = context.OrderDetails.Where(x => x.OrderId == order.OrderId).Select(x => x.ProductId).ToList();
                        var items      = context.Products
                                         .Where(x => productIds.Contains(x.ProductId))
                                         .ToList();

                        var orderItems = items.Select(
                            x => new LegacyOrderItem
                        {
                            SkuNumber = x.SkuNumber,
                            Price     = x.Price
                        }).ToList();

                        var queueOrder = new LegacyOrder
                        {
                            Address      = order.Address,
                            Country      = order.Country,
                            City         = order.City,
                            Phone        = order.Phone,
                            CustomerName = order.Name,
                            OrderDate    = order.OrderDate,
                            PostalCode   = order.PostalCode,
                            State        = order.State,
                            TotalCost    = order.Total,
                            Discount     = order.Total,
                            Items        = orderItems
                        };
                        var settings = new JsonSerializerSettings {
                            ContractResolver = new CamelCasePropertyNamesContractResolver()
                        };
                        var message = JsonConvert.SerializeObject(queueOrder, settings);
                        orderQueue.AddMessage(new CloudQueueMessage(message));
                        order.Processed = true;
                    }
                    context.SaveChanges();
                    Console.WriteLine("Orders successfully processed.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #2
0
        public static void CreateOrderProcessTask([Queue("orders")] CloudQueue orderQueue)
        {
            Console.WriteLine("Starting Create Order Process Task");
            try
            {
                var builder = new ConfigurationBuilder();
                //builder.Add(new JsonConfigurationSource("config.json"));
                builder.AddJsonFile("config.json");
                var config = builder.Build();
                var connectionString = config["Data:DefaultConnection:ConnectionString"];

                using (var context = new PartsUnlimitedContext(connectionString))
                {
                    var orders = context.Orders.Where(x => !x.Processed).ToList();
                    Console.WriteLine("Found {0} orders to process", orders.Count);

                    foreach (var order in orders)
                    {
                        var productIds = context.OrderDetails.Where(x => x.OrderId == order.OrderId).Select(x => x.ProductId).ToList();
                        var items = context.Products
                            .Where(x => productIds.Contains(x.ProductId))
                            .ToList();

                        var orderItems = items.Select(
                                x => new LegacyOrderItem
                                {
                                    SkuNumber = x.SkuNumber,
                                    Price = x.Price
                                }).ToList();

                        var queueOrder = new LegacyOrder
                        {
                            Address = order.Address,
                            Country = order.Country,
                            City = order.City,
                            Phone = order.Phone,
                            CustomerName = order.Name,
                            OrderDate = order.OrderDate,
                            PostalCode = order.PostalCode,
                            State = order.State,
                            TotalCost = order.Total,
                            Discount = order.Total,
                            Items = orderItems
                        };
                        var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
                        var message = JsonConvert.SerializeObject(queueOrder, settings);
                        orderQueue.AddMessage(new CloudQueueMessage(message));
                        order.Processed = true;
                    }
                    context.SaveChanges();
                    Console.WriteLine("Orders successfully processed.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }