Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("RECEIVER has started. \nPress any key to stop.");
            var factory = new ConnectionFactory()
            {
                UserName = "******",
                Password = "******",
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(QUEUE_NAME, false, false, false, null);
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var             body            = ea.Body;
                        var             jsonString      = Encoding.UTF8.GetString(body);
                        NewOrderMessage newOrderMessage = JsonConvert.DeserializeObject <NewOrderMessage>(jsonString);
                        Console.WriteLine($"Received{jsonString}");
                        using (var context = new FoodServiceContext())
                        {
                            using (var transaction = context.Database.BeginTransaction())
                            {
                                try
                                {
                                    context.Order.Add(newOrderMessage.NewOrder);
                                    context.OrderCustomer.Add(newOrderMessage.NewOrderCustomer);
                                    context.OrderEmployee.Add(newOrderMessage.NewOrderEmployee);
                                    context.OrderItem.AddRange(newOrderMessage.NewOrderItems);

                                    context.SaveChanges();
                                    transaction.Commit();

                                    channel.BasicAck(ea.DeliveryTag, false);
                                }
                                catch (Exception e)
                                {
                                    transaction.Rollback();
                                }
                            }
                        }
                    };
                    try
                    {
                        channel.BasicConsume(QUEUE_NAME, false, consumer);
                        Console.ReadKey();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
        }
Esempio n. 2
0
 public void CheckIsNotProductOutOfStock()
 {
     using (var context = new FoodServiceContext(_optionsBuilder.Options))
     {
         context.Products.Add(new Product {
             Name = "sdsd", Quantity = 0
         });
         context.SaveChanges();
         IProductService productService = new ProductService(context);
         Assert.False(productService.IsProductOutOfStock(context.Products.First().ProductId));
     }
 }
Esempio n. 3
0
 public void GetAll()
 {
     using (var context = new FoodServiceContext(_optionsBuilder.Options))
     {
         context.Products.Add(new Product {
             Name = "sdsd", Quantity = 2
         });
         context.SaveChanges();
         IProductService productService = new ProductService(context);
         Assert.NotEmpty(productService.GetAll());
     }
 }