예제 #1
0
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "UserDetail",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var             body       = ea.Body.Span;
                        var             data       = Encoding.UTF8.GetString(body);
                        UserDetailQueue userDetail = JsonConvert.DeserializeObject <UserDetailQueue>(data);
                        Console.WriteLine(" [x] Received {0}", userDetail.UserId + " : " + userDetail.ProductId);

                        using (DenoContext context = new DenoContext())
                        {
                            UserDetails userDetailModel = new UserDetails();
                            userDetailModel.UserId      = userDetail.UserId;
                            userDetailModel.ProductId   = userDetail.ProductId;
                            userDetailModel.CreatedDate = DateTime.Now;
                            userDetailModel.IsActive    = true;
                            context.UserDetails.Add(userDetailModel);
                            context.SaveChanges();

                            foreach (TransactionHistory dataTransaction in userDetail.TransactionList)
                            {
                                string TableName = dataTransaction.TableName;
                                int    ID        = dataTransaction.ID;
                                //Different Resource Type Case SQL
                                //Strategy Design Pattern
                                if (dataTransaction.Type == TransactionType.SqlDB && dataTransaction.State == TransactionState.Pending)
                                {
                                    context.ExecuteQuery($"UPDATE {TableName}  SET IsActive = 1 WHERE Id = {ID}");
                                    dataTransaction.State = TransactionState.Completed;
                                }

                                //Run Different Bussines Logic By Step Name
                                if (dataTransaction.Step == TransactionStep.Product && dataTransaction.State == TransactionState.Completed)
                                {
                                }
                            }
                        }
                        //-------------------------
                    };
                    channel.BasicConsume(queue: "UserDetail",
                                         autoAck: true,
                                         consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }
예제 #2
0
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "Product",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var             body    = ea.Body.Span;
                        var             data    = Encoding.UTF8.GetString(body);
                        ProductConsumer product = JsonConvert.DeserializeObject <ProductConsumer>(data);
                        Console.WriteLine(" [x] Received {0}", product.Name + " : " + product.Price);

                        using (DenoContext context = new DenoContext())
                        {
                            if (!context.Products.Any(pro => pro.Name == product.Name && pro.Price == product.Price))
                            {
                                Products productModel = new Products();
                                productModel.Name     = product.Name;
                                productModel.Price    = product.Price;
                                productModel.IsActive = false;
                                context.Products.Add(productModel);
                                context.SaveChanges();

                                UserDetailQueue userDetail = new UserDetailQueue();
                                userDetail.ProductId = productModel.Id;
                                userDetail.UserId    = product.UserId;
                                userDetail.IsActive  = false;

                                List <TransactionHistory> listTransaction = product.TransactionList;
                                listTransaction.Add(new TransactionHistory()
                                {
                                    TableName = "Products",
                                    ID        = productModel.Id,
                                    State     = TransactionState.Pending,
                                    Step      = TransactionStep.Product,
                                    Type      = TransactionType.SqlDB
                                });
                                userDetail.TransactionList = listTransaction;

                                Console.WriteLine(PushRabbitMQ(userDetail));
                            }
                            else
                            {
                                int             productID  = context.Products.FirstOrDefault(pro => pro.Name == product.Name && pro.Price == product.Price).Id;
                                UserDetailQueue userDetail = new UserDetailQueue();
                                userDetail.ProductId = productID;
                                userDetail.UserId    = product.UserId;
                                userDetail.IsActive  = false;

                                List <TransactionHistory> listTransaction = product.TransactionList;
                                userDetail.TransactionList = listTransaction;

                                Console.WriteLine(PushRabbitMQ(userDetail));
                            }
                        }
                        //-------------------------
                    };
                    channel.BasicConsume(queue: "Product",
                                         autoAck: true,
                                         consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }
예제 #3
0
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "User",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body.Span;
                        var data = Encoding.UTF8.GetString(body);
                        //UserShop user = JsonConvert.DeserializeObject<UserShop>(data);
                        UserShop user = JsonSerializer.Deserialize <UserShop>(data);
                        Console.WriteLine(" [x] Received {0}", user.name + " : " + user.surname);

                        using (DenoContext context = new DenoContext())
                        {
                            if (!context.Users.Any(us => us.Name == user.name && us.Surname == user.surname))
                            {
                                Users userModel = new Users();
                                userModel.Name     = user.name;
                                userModel.Surname  = user.surname;
                                userModel.No       = user.no;
                                userModel.IsActive = false;
                                context.Users.Add(userModel);
                                context.SaveChanges();

                                ProductConsumer product = new ProductConsumer();
                                product.Name     = user.productName;
                                product.Price    = user.productPrice;
                                product.UserId   = userModel.Id;
                                product.IsActive = false;

                                List <TransactionHistory> listTransaction = new List <TransactionHistory>();
                                listTransaction.Add(new TransactionHistory()
                                {
                                    TableName = "Users",
                                    ID        = userModel.Id,
                                    State     = TransactionState.Pending,
                                    Step      = TransactionStep.User,
                                    Type      = TransactionType.SqlDB
                                });
                                product.TransactionList = listTransaction;

                                Console.WriteLine(PushRabbitMQ(product));
                            }
                            else
                            {
                                int             userID  = context.Users.FirstOrDefault(us => us.Name == user.name && us.Surname == user.surname).Id;
                                ProductConsumer product = new ProductConsumer();
                                product.Name     = user.productName;
                                product.Price    = user.productPrice;
                                product.UserId   = userID;
                                product.IsActive = false;

                                List <TransactionHistory> listTransaction = new List <TransactionHistory>();
                                product.TransactionList = listTransaction;

                                Console.WriteLine(PushRabbitMQ(product));
                            }
                        }
                        //-------------------------
                    };
                    channel.BasicConsume(queue: "User",
                                         autoAck: true,
                                         consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }