예제 #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 Consume(ConsumeContext <IUpdateProductCommand> 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 product = repository.Product.GetProductById(context.Message.Id);
                if (product != null)
                {
                    //delete existing blob
                    var deleteblob = new PhotoBlob(blobStorageSettings);
                    await deleteblob.DeleteBlob(product.BlobName);

                    //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;
                }

                var productService = new ProductCatalogService(repository);
                var result         = await productService.UpdateProduct(context.Message);

                var updatedEvent = new ProductUpdatedEvent(result);
                await context.RespondAsync(updatedEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }