public IActionResult AddApplication(AddApplicationCommand command)
        {
            if (string.IsNullOrEmpty(command.Currency) ||
                string.IsNullOrEmpty(command.ClientId) ||
                string.IsNullOrEmpty(command.DepartmentAddress) ||
                command.Amount == 0)
            {
                _logger.LogError("Request is invalid");
                return(BadRequest());
            }

            _logger.LogInformation($"Processing request: {JsonConvert.SerializeObject(command)}");

            _channel.QueueDeclare(queue: AddQueueName,
                                  durable: false,
                                  exclusive: false,
                                  autoDelete: false,
                                  arguments: null);

            var mqCommand = new AddApplicationMqCommand
            {
                ClientId          = command.ClientId,
                Amount            = command.Amount,
                Currency          = command.Currency,
                DepartmentAddress = command.DepartmentAddress,
                ClientIp          = HttpContext.Connection.RemoteIpAddress.ToString(),
            };

            string message;

            try
            {
                message = JsonConvert.SerializeObject(mqCommand);
                var body = Encoding.UTF8.GetBytes(message);

                _channel.BasicPublish(exchange: "",
                                      routingKey: AddQueueName,
                                      basicProperties: _props,
                                      body: body);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error from message queue: {e.Message}");
                return(StatusCode((int)HttpStatusCode.BadGateway));
            }

            _logger.LogInformation($"Sent message: {message}");

            _channel.BasicConsume(
                consumer: _consumer,
                queue: _replyQueueName,
                autoAck: true);

            var response = _respQueue.Take();

            _logger.LogInformation($"Received message from server: {response}");
            return(Ok(response));
        }
Ejemplo n.º 2
0
        public int AddApplication(AddApplicationMqCommand command)
        {
            var p = new DynamicParameters();

            p.Add("@ClientId", command.ClientId, DbType.Int32);
            p.Add("@DepartmentAddress", command.DepartmentAddress, DbType.String);
            p.Add("@Amount", command.Amount, DbType.Decimal);
            p.Add("@Currency", command.Currency, DbType.String);
            p.Add("@State", "Ready", DbType.String);

            try
            {
                return(_db.Query <int>("AddApplication", p, commandType: CommandType.StoredProcedure).Single());
            }
            catch (SqlException e)
            {
                _logger.LogError($"Error in database: {e.Message}");
                throw;
            }
        }