Exemplo n.º 1
0
 public RabbitMqCommunicator(RabbitMqSettings rabbitMqSettings, IModel rabbitMqChannel, IMongoUnitOfWork mongoUnitOfWork)
 {
     _rabbitMqSettings = rabbitMqSettings;
     _rabbitMqChannel  = rabbitMqChannel;
     _players          = mongoUnitOfWork.GetRepository <PlayerInfo>();
 }
        public OperationResult <ProductPostDTO> Add(ProductPost newProduct)
        {
            var sqlServerProductRepository = _sqlUnitOfWork.GetRepository <IProductSQLServerRepository>();
            var sqlServerClientRepository  = _sqlUnitOfWork.GetRepository <IClientSQLServerRepository>();
            var mongoProductRepository     = _mongoUnitOfWork.GetRepository <IProductMongoRepository>();

            var clientResult  = sqlServerClientRepository.Get(client => client.Id.Equals(newProduct.ClientId));
            var client        = clientResult.Data.FirstOrDefault();
            var isClientExist = client != null;
            var result        = new OperationResult <ProductPostDTO>();

            if (isClientExist)
            {
                newProduct.Tags?.Add(client.Name);
                var bsonId           = ObjectId.GenerateNewId();
                var sqlServerProduct = _mapper.Map <Product>(newProduct);
                var mongoProduct     = _mapper.Map <DAL.Models.Mongo.Product>(newProduct);
                var elasticProduct   = _mapper.Map <DAL.Models.ElasticSearch.Product>(newProduct);

                mongoProduct.ClientName = client.Name;
                mongoProduct.ClientId   = client.Id;
                mongoProduct.Id         = bsonId;

                var mongoResult = mongoProductRepository.Add(mongoProduct);
                result.Errors = mongoResult.Errors;
                result.Type   = mongoResult.Type;

                if (mongoResult.IsSuccess)
                {
                    sqlServerProduct.Id     = Guid.NewGuid();
                    sqlServerProduct.BsonId = bsonId.ToString();

                    var sqlServerResult = sqlServerProductRepository.Add(sqlServerProduct);
                    result.Errors = sqlServerResult.Errors;
                    result.Type   = sqlServerResult.Type;

                    if (sqlServerResult.IsSuccess)
                    {
                        elasticProduct.Id = bsonId.ToString();

                        var elasticResult = _productElasticSearchRepository.Add(elasticProduct);
                        result.Errors = elasticResult.Errors;
                        result.Type   = elasticResult.Type;

                        if (elasticResult.IsSuccess)
                        {
                            result.Type = ResultType.Success;
                            result.Data = new ProductPostDTO
                            {
                                Id   = bsonId.ToString(),
                                Name = mongoProduct.Name
                            };
                            _sqlUnitOfWork.Save();
                        }
                    }
                }
            }
            else
            {
                result.Type = ResultType.BadRequest;
                result.Errors.Add("Not valid client id");
            }

            return(result);
        }