示例#1
0
        public async Task Consume(ConsumeContext <ICreateProductCommand> context)
        {
            try
            {
                var connectionStringSettings = new ConnectionStringSettings();
                var blobStorageSettings      = new BlobStorageSettings();
                _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings);
                _configurationRoot.GetSection("BlobStorage").Bind(blobStorageSettings);

                var dbContext      = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection);
                var repository     = new RepositoryWrapper(dbContext);
                var productService = new ProductCatalogService(repository);

                var product = repository.Product.GetByCondition(x => x.Code.Equals(context.Message.Code) || x.Name.Equals(context.Message.Name));
                if (product?.Count() == 0)
                {
                    //upload photo to blob
                    var repo       = BlobConfigurator.GetMessageDataRepository(blobStorageSettings);
                    var bytesArray = Convert.FromBase64String(context.Message.Photo);
                    var payload    = repo.PutBytes(bytesArray).Result;
                    context.Message.BlobName = payload.Address.AbsolutePath;
                }

                //create product
                var result = await productService.CreateProduct(context.Message);

                var createdEvent = new ProductCreatedEvent(result);
                await context.RespondAsync(createdEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task CreateProduct_CreatedAsync()
        {
            // Arrange
            var code    = "123";
            var product = new Product()
            {
                Code = code
            };

            var httpClient = new TestHelper().CreateMockHttpClient(JsonConvert.SerializeObject(product), HttpStatusCode.Created);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.CreateProduct(product);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(code, result.Code);
        }