Beispiel #1
0
        private OrderTransformationResult TransformOrder(OrderTransformation transformationType, int id)
        {
            var   transformationResult = new OrderTransformationResult();
            Chain chain = transformationType == OrderTransformation.Dissimilar
                              ? DissimilarChainFactory.Create(new BaseChain(Orders[id]))
                              : HighOrderFactory.Create(new Chain(Orders[id]), transformationType.GetLink());

            for (int i = 0; i < Orders.Count; i++)
            {
                if (Orders[i].SequenceEqual(chain.Building))
                {
                    transformationResult.OrderId        = i;
                    transformationResult.Transformation = transformationType.GetDisplayValue();
                    break;
                }
            }
            return(transformationResult);
        }
Beispiel #2
0
        public IActionResult  Post([FromBody] OrderQueueModel orderModel)
        {
            List <string> errorList = new List <string>();

            try
            {
                if (ModelState.IsValid)
                {
                    var order = OrderTransformation.TransformOrderResultModelInDomain(orderModel);
                    orderService.AddInQueue(order);
                }
                else
                {
                    return(StatusCode(403, GetModelErrors()));
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(400, ex.Message));
            }
        }
Beispiel #3
0
 public IEnumerable <OrderResultModel> Get()
 {
     return(OrderTransformation.TransformOrdersInModels(orderService.GetAll()));
 }
Beispiel #4
0
        public void AddOrderInQueue(Order order)
        {
            try
            {
                using (IConnection connection = new ConnectionFactory()
                {
                    HostName = configuration.GetSection("QueueAddress").Value, Port = int.Parse(configuration.GetSection("QueuePort").Value.ToString())
                }.CreateConnection())
                {
                    using (IModel channel = connection.CreateModel())
                    {
                        replyQueueName = channel.QueueDeclare().QueueName;
                        consumer       = new EventingBasicConsumer(channel);


                        var tcs        = new TaskCompletionSource <string>();
                        var resultTask = tcs.Task;

                        var correlationId = Guid.NewGuid().ToString();

                        IBasicProperties props = channel.CreateBasicProperties();
                        props.CorrelationId = correlationId;
                        props.ReplyTo       = replyQueueName;


                        EventHandler <BasicDeliverEventArgs> handler = null;
                        handler = (model, ea) =>
                        {
                            if (ea.BasicProperties.CorrelationId == correlationId)
                            {
                                consumer.Received -= handler;

                                var body     = ea.Body;
                                var response = Encoding.UTF8.GetString(body);

                                tcs.SetResult(response);
                            }
                        };
                        consumer.Received += handler;

                        String jsonified   = JsonConvert.SerializeObject(OrderTransformation.TransformOrderInQueueModel(order));
                        byte[] orderBuffer = Encoding.UTF8.GetBytes(jsonified);
                        channel.BasicPublish(
                            exchange: "",
                            routingKey: configuration.GetSection("QueueName").Value,
                            basicProperties: props,
                            body: orderBuffer);


                        channel.BasicConsume(
                            consumer: consumer,
                            queue: replyQueueName,
                            autoAck: true);


                        var result = JsonConvert.DeserializeObject <QueueResult>(resultTask.Result);


                        if (result.Status == true)
                        {
                            order.Status = "Processado!";
                        }
                        else
                        {
                            order.Status = result.Msgs[0].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }