Esempio n. 1
0
        public async Task Consume(ConsumeContext <IGetProductCommand> 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 result         = await productService.GetProduct(context.Message.ProductId);

                var getProductEvent = new ProductRetrievedEvent(result);

                if (getProductEvent.ProductDto != null)
                {
                    var phtoBlob  = new PhotoBlob(blobStorageSettings);
                    var byteArray = await phtoBlob.DownloadBlob(getProductEvent.ProductDto.BlobName);

                    getProductEvent.ProductDto.Photo = byteArray;
                }

                await context.RespondAsync(getProductEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Esempio n. 2
0
        public async Task Consume(ConsumeContext <IDeleteProductCommand> 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(Guid.Parse(context.Message.ProductId));
                if (product != null)
                {
                    var deleteblob = new PhotoBlob(blobStorageSettings);
                    await deleteblob.DeleteBlob(product.BlobName);
                }

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

                var deletedEvent = new ProductDeletedEvent(result);
                await context.RespondAsync(deletedEvent);
            }
            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;
            }
        }