예제 #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: "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();
                }
        }
예제 #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: "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();
                }
        }